import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

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

serve(async (req) => {
  // Handle CORS preflight requests
  if (req.method === "OPTIONS") {
    return new Response(null, {
      headers: corsHeaders,
      status: 204
    });
  }

  try {
    // Create a Supabase client with the Auth context of the logged in user
    const supabaseClient = createClient(
      Deno.env.get("SUPABASE_URL") ?? "",
      Deno.env.get("SUPABASE_ANON_KEY") ?? "",
      {
        global: {
          headers: {
            Authorization: req.headers.get("Authorization") ?? ""
          }
        }
      }
    );

    // Verify the user is logged in
    const { data: { user }, error: userError } = await supabaseClient.auth.getUser();
    if (userError || !user) {
      console.error("Unauthorized request:", userError);
      return new Response(JSON.stringify({
        error: "Unauthorized"
      }), {
        status: 401,
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders
        }
      });
    }

    // Get the school_id of the logged in user
    const { data: schoolId, error: schoolIdError } = await supabaseClient.rpc("get_user_school_id");
    if (schoolIdError || !schoolId) {
      console.error("Could not determine school ID:", schoolIdError);
      return new Response(JSON.stringify({
        error: "Could not determine school ID"
      }), {
        status: 400,
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders
        }
      });
    }

    console.log("Processing request for school ID:", schoolId);

    // Parse the request body
    const requestBody = await req.json();
    const { method, email } = requestBody;

    console.log("Request body:", { method, email });

    // Generate a random code
    const generateCode = () => {
      const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // Removed confusing characters
      let result = "";
      for (let i = 0; i < 8; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
      }
      return result;
    };

    // Handle invite by code
    if (method === "code") {
      console.log("Generating invitation code");
      const inviteCode = generateCode();
      console.log("Generated invite code:", inviteCode);

      // Insert into student_invites table - simple version without teacher_id
      const { data: inviteData, error: inviteError } = await supabaseClient
        .from("student_invites")
        .insert({
          code: inviteCode,
          school_id: schoolId,
          status: "pending"
        })
        .select();

      if (inviteError) {
        console.error("Error creating invitation:", inviteError);
        return new Response(JSON.stringify({
          error: "Failed to create invitation",
          details: inviteError.message
        }), {
          status: 500,
          headers: {
            "Content-Type": "application/json",
            ...corsHeaders
          }
        });
      }

      return new Response(JSON.stringify({
        message: "Student invite code generated successfully",
        code: inviteCode
      }), {
        status: 200,
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders
        }
      });
    } 
    // Handle invite by email
    else if (method === "email" && email) {
      console.log("Creating email invitation for:", email);
      const inviteCode = generateCode();

      // Create an invitation record
      const { data: inviteData, error: inviteError } = await supabaseClient
        .from("student_invites")
        .insert({
          code: inviteCode,
          email: email,
          school_id: schoolId,
          status: "pending"
        })
        .select();

      if (inviteError) {
        console.error("Error creating invitation:", inviteError);
        return new Response(JSON.stringify({
          error: "Failed to create invitation",
          details: inviteError.message
        }), {
          status: 500,
          headers: {
            "Content-Type": "application/json",
            ...corsHeaders
          }
        });
      }

      console.log(`Invite code ${inviteCode} created for email ${email}`);
      return new Response(JSON.stringify({
        message: "Student invitation created successfully",
        code: inviteCode,
        email: email
      }), {
        status: 200,
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders
        }
      });
    } 
    // Handle invalid requests
    else {
      console.error("Invalid request parameters");
      return new Response(JSON.stringify({
        error: "Invalid request parameters",
        received: { method, email }
      }), {
        status: 400,
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders
        }
      });
    }
  } catch (error) {
    console.error("Error processing request:", error);
    return new Response(JSON.stringify({
      error: "Internal server error",
      details: error?.message || "Unknown error"
    }), {
      status: 500,
      headers: {
        "Content-Type": "application/json",
        ...corsHeaders
      }
    });
  }
});