import { auth } from '@/auth';
import { successResponse, errorResponse } from '@/lib/utils/apiResponse';
import {
  buildUserDriveFolderName,
  explainGoogleDriveError,
  uploadToDrive,
} from '@/lib/utils/googleDrive';
import { finalizeUserMediaUpload, MEDIA_KIND_USER_SIGNATURE } from '@/lib/media/userScopedMedia';
import { getEffectiveGoogleDriveRootFolderId } from '@/lib/utils/globalSettings';

/** Signature image for print templates (PNG recommended for transparency). */
export async function POST(req: Request) {
  const session = await auth();
  if (!session?.user?.id) return errorResponse('Unauthorized', 401);

  const companyId = session.user.activeCompanyId;
  if (!companyId) {
    return errorResponse('Select an active company before uploading', 400);
  }

  try {
    const formData = await req.formData();
    const file = formData.get('file') as File | null;

    if (!file) return errorResponse('File is required', 400);

    const allowedMimes = ['image/jpeg', 'image/png', 'image/webp'];
    if (!allowedMimes.includes(file.type)) {
      return errorResponse('Only JPEG, PNG, or WebP images are allowed', 400);
    }

    if (file.size > 3 * 1024 * 1024) {
      return errorResponse('Signature file must not exceed 3 MB', 400);
    }

    const userId = session.user.id;
    const rootFolderId = await getEffectiveGoogleDriveRootFolderId();
    if (!rootFolderId) return errorResponse('Google Drive folder not configured', 500);
    const buffer = Buffer.from(await file.arrayBuffer());
    const ext =
      file.type === 'image/png' ? 'png' : file.type === 'image/webp' ? 'webp' : 'jpg';
    const { viewerUrl } = await uploadToDrive(
      buffer,
      `user-${userId}-signature-${Date.now()}.${ext}`,
      file.type,
      {
        companyId,
        rootFolderId,
        scope: 'global',
        folderPath: [
          { key: 'drive-folder:users-root', name: 'Users' },
          {
            key: `drive-folder:user:${userId}`,
            name: buildUserDriveFolderName(session.user.name ?? 'User', userId),
          },
        ],
      },
    );

    const { displayUrl } = await finalizeUserMediaUpload({
      userId,
      companyId,
      kind: MEDIA_KIND_USER_SIGNATURE,
      newFileUrl: viewerUrl,
      mimeType: file.type,
      fileName: `user-${userId}-signature.${ext}`,
      bytes: file.size,
      uploadedById: userId,
    });

    return successResponse({ url: displayUrl });
  } catch (err: unknown) {
    const message = explainGoogleDriveError(err);
    console.error('User signature upload error:', message);
    return errorResponse(message, 500);
  }
}
