"use client";

import type React from "react";
import { useState, useEffect } from "react";
import DoctorLayout from "@/components/doctor-layout"; 
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Clock, Calendar, MapPin, Phone, User, X, RefreshCw } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { toast } from "sonner";

const APPOINTMENTS_STORAGE_KEY = "clinic-appointments";

// Tipagem da consulta (mantida)
interface LocalStorageAppointment {
    id: number;
    patientName: string;
    doctor: string;       
    specialty: string;
    date: string;
    time: string;
    status: "agendada" | "confirmada" | "cancelada" | "realizada";
    location: string;
    phone: string;
}

// SIMULAÇÃO DO MÉDICO LOGADO - Mantemos apenas para informação no cabeçalho
// O NOME AQUI NÃO ESTÁ MAIS SENDO USADO PARA FILTRAR
const LOGGED_IN_DOCTOR_NAME = "Dr. João Silva (TODAS AS CONSULTAS)"; 

// --- COMPONENTE PRINCIPAL ---

export default function DoctorAppointmentsPage() {
    const [appointments, setAppointments] = useState<LocalStorageAppointment[]>([]);
    const [isLoading, setIsLoading] = useState(true);

    useEffect(() => {
        loadAppointments();
    }, []);

    const loadAppointments = () => {
        setIsLoading(true);
        try {
            const storedAppointmentsRaw = localStorage.getItem(APPOINTMENTS_STORAGE_KEY);
            const allAppointments: LocalStorageAppointment[] = storedAppointmentsRaw ? JSON.parse(storedAppointmentsRaw) : [];

            // ***** MUDANÇA IMPLEMENTADA *****
            // Removemos a filtragem para que todas as consultas sejam exibidas
            const appointmentsToShow = allAppointments;

            // 2. Ordena por Data e Hora
            appointmentsToShow.sort((a, b) => {
                const dateTimeA = new Date(`${a.date}T${a.time}:00`);
                const dateTimeB = new Date(`${b.date}T${b.time}:00`);
                return dateTimeA.getTime() - dateTimeB.getTime();
            });

            setAppointments(appointmentsToShow); // Exibe a lista completa (e ordenada)
            toast.success("Agenda atualizada com sucesso!");
        } catch (error) {
            console.error("Erro ao carregar a agenda do LocalStorage:", error);
            toast.error("Não foi possível carregar sua agenda.");
        } finally {
            setIsLoading(false);
        }
    };

    // Função utilitária (mantida)
    const getStatusVariant = (status: LocalStorageAppointment['status']) => {
        switch (status) {
            case "confirmada":
            case "agendada":
                return "default";
            case "realizada":
                return "secondary";
            case "cancelada":
                return "destructive";
            default:
                return "outline";
        }
    };

    const handleCancel = (id: number) => {
        const storedAppointmentsRaw = localStorage.getItem(APPOINTMENTS_STORAGE_KEY);
        const allAppointments: LocalStorageAppointment[] = storedAppointmentsRaw ? JSON.parse(storedAppointmentsRaw) : [];

        const updatedAppointments = allAppointments.map(app => 
            app.id === id ? { ...app, status: "cancelada" as const } : app
        );

        localStorage.setItem(APPOINTMENTS_STORAGE_KEY, JSON.stringify(updatedAppointments));
        loadAppointments(); // Recarrega a lista
        toast.info(`Consulta cancelada com sucesso.`);
    };

    const handleReSchedule = (id: number) => {
        toast.info(`Reagendamento da Consulta ID: ${id}. Navegar para a página de agendamento.`);
    };

    return (
        <DoctorLayout>
            <div className="space-y-6">
                <div>
                    <h1 className="text-3xl font-bold text-gray-900">Minhas Consultas (Agenda Completa)</h1>
                    <p className="text-gray-600">Todas as consultas agendadas, para qualquer médico, são exibidas aqui ({LOGGED_IN_DOCTOR_NAME})</p>
                </div>

                <div className="flex justify-end">
                    <Button onClick={loadAppointments} disabled={isLoading} variant="outline" size="sm">
                        <RefreshCw className={`mr-2 h-4 w-4 ${isLoading ? 'animate-spin' : ''}`} />
                        Atualizar Agenda
                    </Button>
                </div>

                <div className="space-y-4">
                    {isLoading ? (
                        <p className="text-center text-lg text-gray-500">Carregando a agenda...</p>
                    ) : appointments.length === 0 ? (
                        <p className="text-center text-lg text-gray-500">Nenhuma consulta agendada no sistema.</p>
                    ) : (
                        appointments.map((appointment) => {
                            const showActions = appointment.status === "agendada" || appointment.status === "confirmada";

                            return (
                                <Card key={appointment.id} className="shadow-lg">
                                    <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                                        {/* NOME DO PACIENTE */}
                                        <CardTitle className="text-xl font-semibold flex items-center">
                                            <User className="mr-2 h-5 w-5 text-blue-600" />
                                            {appointment.patientName}
                                        </CardTitle>
                                        {/* STATUS DA CONSULTA */}
                                        <Badge variant={getStatusVariant(appointment.status)} className="uppercase">
                                            {appointment.status}
                                        </Badge>
                                    </CardHeader>

                                    <CardContent className="grid md:grid-cols-3 gap-4 pt-4">
                                        {/* Coluna de Detalhes */}
                                        <div className="space-y-3">
                                            <div className="flex items-center text-sm text-gray-700">
                                                <User className="mr-2 h-4 w-4 text-gray-500" />
                                                <span className="font-semibold">Médico:</span> {appointment.doctor}
                                            </div>
                                            <div className="flex items-center text-sm text-gray-700">
                                                <Calendar className="mr-2 h-4 w-4 text-gray-500" />
                                                {new Date(appointment.date).toLocaleDateString("pt-BR", { timeZone: "UTC" })}
                                            </div>
                                            <div className="flex items-center text-sm text-gray-700">
                                                <Clock className="mr-2 h-4 w-4 text-gray-500" />
                                                {appointment.time}
                                            </div>
                                        </div>

                                        {/* Coluna 2: Local e Contato */}
                                        <div className="space-y-3">
                                            <div className="flex items-center text-sm text-gray-700">
                                                <MapPin className="mr-2 h-4 w-4 text-gray-500" />
                                                {appointment.location}
                                            </div>
                                            <div className="flex items-center text-sm text-gray-700">
                                                <Phone className="mr-2 h-4 w-4 text-gray-500" />
                                                {appointment.phone || "N/A"} 
                                            </div>
                                        </div>

                                        {/* COLUNA 3: Ações (Botões) */}
                                        <div className="flex flex-col justify-center items-end">
                                            {showActions && (
                                                <div className="flex space-x-2">
                                                    <Button 
                                                        variant="outline" 
                                                        size="sm"
                                                        onClick={() => handleReSchedule(appointment.id)}
                                                    >
                                                        <RefreshCw className="mr-2 h-4 w-4" />
                                                        Reagendar
                                                    </Button>
                                                    <Button 
                                                        variant="destructive" 
                                                        size="sm"
                                                        onClick={() => handleCancel(appointment.id)}
                                                    >
                                                        <X className="mr-2 h-4 w-4" />
                                                        Cancelar
                                                    </Button>
                                                </div>
                                            )}
                                        </div>
                                    </CardContent>
                                </Card>
                            );
                        })
                    )}
                </div>
            </div>
        </DoctorLayout>
    );
}