import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { externalRest } from "../../lib/externalSupabase.ts";
import { mydb } from "../../lib/mySupabase.ts";
import { corsHeaders, jsonResponse, errorResponse } from "../../lib/utils.ts";
import { validateAuth, hasPermission } from "../../lib/auth.ts";

/**
 * POST /appointments/suggest-slot
 * Sugerir slots automaticamente com base em disponibilidade e preferências
 * 
 * Body:
 * {
 *   patient_id: uuid,
 *   doctor_id: uuid,
 *   specialty?: string,
 *   preferred_dates?: date[],
 *   preferred_times?: string[]
 * }
 * 
 * Returns:
 * {
 *   suggestions: [{
 *     suggested_datetime: timestamptz,
 *     score: decimal,
 *     reason: string,
 *     expires_at: timestamptz
 *   }]
 * }
 */

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

  try {
    const auth = await validateAuth(req);
    if (!auth) {
      return errorResponse("Não autorizado", 401);
    }

    if (req.method !== "POST") {
      return errorResponse("Method not allowed", 405);
    }

    const body = await req.json();
    const { patient_id, doctor_id, preferred_dates, preferred_times } = body;

    // Paciente só pode sugerir para si mesmo
    if (patient_id !== auth.userId && !hasPermission(auth.role, ["admin", "secretary"])) {
      return errorResponse("Sem permissão", 403);
    }

    // Buscar disponibilidades do médico no Supabase externo
    const availRes = await externalRest(
      "/rest/v1/doctor_availability",
      "GET",
      undefined,
      { doctor_id: `eq.${doctor_id}`, active: "eq.true" }
    );

    if (availRes.status >= 400) {
      return errorResponse("Falha ao buscar disponibilidades");
    }

    const availabilities = availRes.data || [];

    // Gerar sugestões com score baseado em preferências
    const suggestions = availabilities
      .map((avail: any) => {
        let score = 5; // score base

        // Aumentar score se corresponde com datas preferidas
        if (preferred_dates?.includes(avail.date)) {
          score += 2;
        }

        // Aumentar score se corresponde com horários preferidos
        if (preferred_times?.includes(avail.start_time)) {
          score += 3;
        }

        return {
          suggested_datetime: `${avail.date}T${avail.start_time}`,
          score: Math.min(score / 10, 1),
          reason: "Baseado em sua disponibilidade",
          expires_at: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(),
        };
      })
      .sort((a: any, b: any) => b.score - a.score)
      .slice(0, 5); // Top 5 sugestões

    // Salvar sugestões no banco
    for (const suggestion of suggestions) {
      await mydb.from("slot_suggestions").insert({
        patient_id,
        doctor_id,
        suggested_datetime: suggestion.suggested_datetime,
        score: suggestion.score,
        reason: suggestion.reason,
        expires_at: suggestion.expires_at,
      });
    }

    // Audit log
    await mydb.from("audit_log").insert({
      user_id: auth.userId,
      action: "suggest_slots",
      target_type: "appointment",
      payload: { patient_id, doctor_id, count: suggestions.length },
    });

    return jsonResponse({ suggestions }, 200);
  } catch (error) {
    console.error("[suggest-slot]", error);
    return errorResponse(error.message, 500);
  }
});
