import { corsHeaders, handleCors } from "../_shared/cors.ts";
import { verifyAdmin, AuthError } from "../_shared/auth.ts";
import { createServiceClient } from "../_shared/supabase-client.ts";
import { classifyGenres } from "../_shared/youtube-client.ts";

Deno.serve(async (req: Request) => {
  const corsResponse = handleCors(req);
  if (corsResponse) return corsResponse;

  try {
    if (req.method !== "POST") {
      return new Response(JSON.stringify({ error: "Method not allowed" }), {
        status: 405,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }

    await verifyAdmin(req);

    const { jobId, force = false } = await req.json();
    if (!jobId || typeof jobId !== "string") {
      return new Response(JSON.stringify({ error: "jobId is required" }), {
        status: 400,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }

    const supabase = createServiceClient();

    // Verify job exists
    const { data: job, error: jobError } = await supabase
      .from("download_jobs")
      .select("id")
      .eq("id", jobId)
      .single();

    if (jobError || !job) {
      return new Response(JSON.stringify({ error: "Job not found" }), {
        status: 404,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }

    // Fetch items — if force=false, only those with null genre
    let query = supabase
      .from("download_items")
      .select("id, video_id, title")
      .eq("job_id", jobId);

    if (!force) {
      query = query.is("genre", null);
    }

    const { data: items, error: itemsError } = await query;

    if (itemsError) {
      throw new Error(`Failed to fetch items: ${itemsError.message}`);
    }

    if (!items || items.length === 0) {
      return new Response(
        JSON.stringify({ classified: 0, skipped: 0, message: "No items to classify" }),
        {
          status: 200,
          headers: { ...corsHeaders, "Content-Type": "application/json" },
        }
      );
    }

    // Call sidecar to classify genres
    const genres = await classifyGenres(
      items.map((item) => ({
        videoId: item.video_id,
        title: item.title ?? "",
        author: null,
      }))
    );

    // Update each item's genre in the database
    let classified = 0;
    let skipped = 0;

    for (const item of items) {
      const genre = genres[item.video_id];
      if (genre) {
        const { error: updateError } = await supabase
          .from("download_items")
          .update({ genre })
          .eq("id", item.id);

        if (updateError) {
          console.error(`Failed to update genre for ${item.video_id}:`, updateError.message);
          skipped++;
        } else {
          classified++;
        }
      } else {
        skipped++;
      }
    }

    return new Response(
      JSON.stringify({ classified, skipped }),
      {
        status: 200,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      }
    );
  } catch (error) {
    const status = error instanceof AuthError ? error.status : 500;
    const message =
      error instanceof Error ? error.message : "Unknown error occurred";

    return new Response(JSON.stringify({ error: message }), {
      status,
      headers: { ...corsHeaders, "Content-Type": "application/json" },
    });
  }
});
