import { createClient } from "https://esm.sh/@supabase/supabase-js@2.99.1";

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") {
    return new Response("ok", { headers: corsHeaders });
  }

  try {
    const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
    const serviceKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
    const supabase = createClient(supabaseUrl, serviceKey);

    const today = new Date().toISOString().split("T")[0];

    // Find all recurring expenses where next_date <= today
    const { data: recurring, error: fetchErr } = await supabase
      .from("billable_expenses")
      .select("*")
      .eq("is_recurring", true)
      .lte("recurring_next_date", today);

    if (fetchErr) throw fetchErr;
    if (!recurring || recurring.length === 0) {
      return new Response(JSON.stringify({ created: 0 }), {
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }

    let created = 0;

    for (const expense of recurring) {
      // Skip if past end date
      if (expense.recurring_end_date && expense.recurring_next_date > expense.recurring_end_date) {
        continue;
      }

      // Create the new expense entry
      const { error: insertErr } = await supabase.from("billable_expenses").insert({
        association_id: expense.association_id,
        date: expense.recurring_next_date,
        description: expense.description,
        amount: expense.amount,
        category: expense.category,
        billable_type: expense.billable_type,
        is_credit: expense.is_credit,
        credit_reason: expense.credit_reason,
        quantity: expense.quantity,
        unit_price: expense.unit_price,
        vendor_name: expense.vendor_name,
        address: expense.address,
        status: "pending",
        recurring_parent_id: expense.id,
        is_recurring: false,
      });

      if (insertErr) {
        console.error(`Failed to create recurring expense for ${expense.id}:`, insertErr);
        continue;
      }

      created++;

      // Advance the next date by one month
      const nextDate = new Date(expense.recurring_next_date + "T12:00:00");
      nextDate.setMonth(nextDate.getMonth() + 1);
      // Clamp day
      const day = expense.recurring_day || 1;
      const maxDay = new Date(nextDate.getFullYear(), nextDate.getMonth() + 1, 0).getDate();
      nextDate.setDate(Math.min(day, maxDay));
      const newNextDate = nextDate.toISOString().split("T")[0];

      await supabase
        .from("billable_expenses")
        .update({ recurring_next_date: newNextDate })
        .eq("id", expense.id);
    }

    return new Response(JSON.stringify({ created, processed: recurring.length }), {
      headers: { ...corsHeaders, "Content-Type": "application/json" },
    });
  } catch (err) {
    console.error("Error processing recurring expenses:", err);
    return new Response(JSON.stringify({ error: err.message }), {
      status: 500,
      headers: { ...corsHeaders, "Content-Type": "application/json" },
    });
  }
});
