// app/api/staff/facturation/upload-pdf/route.ts
import { NextResponse } from "next/server";
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
import { cookies } from "next/headers";

export const dynamic = 'force-dynamic';
export const revalidate = 0;
export const runtime = 'nodejs';

async function isStaffUser(supabase: any, userId: string): Promise<boolean> {
  try {
    const { data: staffRow } = await supabase
      .from('staff_users')
      .select('is_staff')
      .eq('user_id', userId)
      .maybeSingle();
    return !!staffRow?.is_staff;
  } catch {
    return false;
  }
}

function slugify(text: string): string {
  return text
    .toLowerCase()
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '') // Supprimer les accents
    .replace(/[^a-z0-9 -]/g, '') // Supprimer les caractères spéciaux
    .replace(/\s+/g, '-') // Remplacer les espaces par des tirets
    .replace(/-+/g, '-') // Éviter les tirets multiples
    .trim();
}

// POST - Upload PDF temporaire pour une nouvelle facture
export async function POST(req: Request) {
  try {
    const supabase = createRouteHandlerClient({ cookies });
    const { data: { session } } = await supabase.auth.getSession();
    if (!session) return NextResponse.json({ error: 'unauthorized' }, { status: 401 });

    // Vérifier que l'utilisateur est staff
    const isStaff = await isStaffUser(supabase, session.user.id);
    if (!isStaff) {
      return NextResponse.json({ error: 'forbidden', message: 'Staff access required' }, { status: 403 });
    }

    const formData = await req.formData();
    const file = formData.get('pdf') as File;
    const orgId = formData.get('org_id') as string;
    const invoiceNumber = formData.get('invoice_number') as string;
    
    if (!file) {
      return NextResponse.json({ error: 'no_file_provided' }, { status: 400 });
    }

    if (file.type !== 'application/pdf') {
      return NextResponse.json({ error: 'invalid_file_type', message: 'Only PDF files are allowed' }, { status: 400 });
    }

    if (!orgId || !invoiceNumber) {
      return NextResponse.json({ error: 'missing_parameters', message: 'org_id and invoice_number are required' }, { status: 400 });
    }

    // Récupérer le nom de l'organisation
    const { data: org, error: orgError } = await supabase
      .from('organizations')
      .select('structure_api')
      .eq('id', orgId)
      .single();

    if (orgError || !org) {
      return NextResponse.json({ error: 'organization_not_found' }, { status: 404 });
    }

    // Générer le chemin S3
    const clientSlug = slugify(org.structure_api);
    const s3Key = `invoices/${clientSlug}/${invoiceNumber}.pdf`;

    // Upload vers S3
    const { S3Client, PutObjectCommand } = await import("@aws-sdk/client-s3");
    const client = new S3Client({ 
      region: process.env.AWS_REGION || 'eu-west-3' 
    });

    const buffer = Buffer.from(await file.arrayBuffer());
    
    const uploadCommand = new PutObjectCommand({
      Bucket: (process.env.AWS_S3_BUCKET || 'odentas-docs').trim(),
      Key: s3Key,
      Body: buffer,
      ContentType: 'application/pdf',
      // Retiré Content-Disposition pour permettre l'ouverture dans le navigateur
    });

    await client.send(uploadCommand);

    return NextResponse.json({
      message: 'PDF uploaded successfully',
      s3_key: s3Key
    });
  } catch (error) {
    console.error('[upload-pdf] Error:', error);
    const message = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: 'internal_server_error', message }, { status: 500 });
  }
}