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

/**
 * POST /appointments-checkin
 * Registrar check-in do paciente (chegou no consultório)
 * Adiciona automaticamente à fila virtual
 *
 * Body:
 * {
 *   appointment_id: uuid,
 *   check_in_method: 'manual' | 'qr_code' | 'nfc' | 'app' | 'kiosk'
 * }
 */

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 { appointment_id, check_in_method } = body;

    // Buscar sync info do usuário
    const syncRes = await mydb
      .from("user_sync")
      .select("*")
      .eq("local_user_id", auth.userId)
      .single();

    const externalUserId = syncRes.data?.external_user_id || null;

    // Registrar check-in no banco
    const checkinRes = await mydb
      .from("checkin_tracking")
      .insert({
        appointment_id,
        patient_id: auth.userId,
        check_in_method,
      })
      .select();

    if (checkinRes.error) {
      // Registrar falha
      await logUserAction(
        auth.userId,
        externalUserId,
        "appointment",
        "checkin_failed",
        `Falha no check-in do agendamento`,
        "appointment",
        appointment_id,
        null,
        null,
        "failed",
        checkinRes.error.message
      );
      return errorResponse(checkinRes.error.message);
    }

    // Buscar ou criar entrada na virtual_queue
    const queueRes = await mydb
      .from("virtual_queue")
      .select("*")
      .eq("appointment_id", appointment_id)
      .single();

    let position = 1;
    let isNewQueue = false;

    if (!queueRes.data) {
      // Criar entrada na fila
      const newQueueRes = await mydb
        .from("virtual_queue")
        .insert({
          appointment_id,
          patient_id: auth.userId,
          position: 1,
          status: "waiting",
          estimated_wait_minutes: 15,
        })
        .select()
        .single();

      if (newQueueRes.data) {
        position = newQueueRes.data.position;
        isNewQueue = true;
      }
    } else {
      position = queueRes.data.position;
    }

    // Registrar ação no novo sistema user_actions
    await logUserAction(
      auth.userId,
      externalUserId,
      "appointment",
      "checkin_completed",
      `Paciente realizou check-in no consultório (posição ${position} na fila)`,
      "appointment",
      appointment_id,
      null,
      {
        check_in_method,
        position,
        queue_id: queueRes.data?.id || null,
      }
    );

    // Manter audit_log para compatibilidade
    await mydb.from("audit_log").insert({
      user_id: auth.userId,
      action: "checkin_appointment",
      target_type: "appointment",
      target_id: appointment_id,
      payload: { check_in_method, position, queue_position: position },
    });

    return jsonResponse({
      success: true,
      appointment_id,
      checked_in_at: new Date().toISOString(),
      position_in_queue: position,
    });
  } catch (error: unknown) {
    console.error("[checkin]", error);
    const err = error as Error;
    return errorResponse(err.message, 500);
  }
});
