import { useState, useEffect } from "react";
import { useParams, useNavigate, useLocation } from "react-router-dom";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { signalRService } from "../services/signalr";
import {
  Box,
  Typography,
  Paper,
  Grid,
  TextField,
  Button,
  Tabs,
  Tab,
  Chip,
  FormControl,
  InputLabel,
  Select,
  MenuItem,
  Table,
  TableBody,
  TableCell,
  TableContainer,
  TableHead,
  TableRow,
  IconButton,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  Autocomplete,
} from "@mui/material";
import { DatePicker, TimePicker } from "@mui/x-date-pickers";
import {
  Save as SaveIcon,
  ArrowBack as BackIcon,
  Add as AddIcon,
  Delete as DeleteIcon,
  ContentCopy as CopyIcon,
  Refresh as RefreshIcon,
  CheckCircle as ConfirmIcon,
  Print as PrintIcon,
} from "@mui/icons-material";
import dayjs from "dayjs";
import { eventiService } from "../services/eventiService";
import { lookupService } from "../services/lookupService";
import EventoCostiPanel from "../components/EventoCostiPanel";
import {
  Evento,
  StatoEvento,
  EventoDettaglioOspiti,
  EventoDettaglioPrelievo,
  EventoDettaglioRisorsa,
} from "../types";

interface TabPanelProps {
  children?: React.ReactNode;
  index: number;
  value: number;
}

function TabPanel(props: TabPanelProps) {
  const { children, value, index, ...other } = props;
  return (
    <div role="tabpanel" hidden={value !== index} {...other}>
      {value === index && <Box sx={{ py: 2 }}>{children}</Box>}
    </div>
  );
}

const getStatoInfo = (stato: StatoEvento) => {
  switch (stato) {
    case StatoEvento.Scheda:
      return { label: "Scheda Evento", color: "#CAE3FC", textColor: "#1976d2" };
    case StatoEvento.Preventivo:
      return { label: "Preventivo", color: "#ffffb8", textColor: "#ed6c02" };
    case StatoEvento.Confermato:
      return { label: "Confermato", color: "#b8ffb8", textColor: "#2e7d32" };
    default:
      return { label: "Nuovo", color: "#fafafa", textColor: "#666" };
  }
};

export default function EventoDetailPage() {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const location = useLocation();
  const queryClient = useQueryClient();
  const [tabValue, setTabValue] = useState(0);

  // Leggi la data passata dal calendario (se presente)
  const initialData = location.state?.dataEvento
    ? { dataEvento: location.state.dataEvento }
    : {};

  const [formData, setFormData] = useState<Partial<Evento>>(initialData);
  const [dialogOpen, setDialogOpen] = useState<string | null>(null);
  const [dialogData, setDialogData] = useState<any>({});
  const [hasChanges, setHasChanges] = useState(!!location.state?.dataEvento);

  const eventoId = parseInt(id || "0");
  const isNew = eventoId === 0 || isNaN(eventoId);

  const { data: evento, isLoading } = useQuery({
    queryKey: ["evento", eventoId],
    queryFn: () => eventiService.getById(eventoId),
    enabled: !isNew,
  });

  const { data: clienti = [] } = useQuery({
    queryKey: ["lookup", "clienti"],
    queryFn: () => lookupService.getClienti(),
  });

  const { data: locations = [] } = useQuery({
    queryKey: ["lookup", "location"],
    queryFn: () => lookupService.getLocation(),
  });

  const { data: tipiEvento = [] } = useQuery({
    queryKey: ["lookup", "tipi-evento"],
    queryFn: () => lookupService.getTipiEvento(),
  });

  const { data: tipiOspite = [] } = useQuery({
    queryKey: ["lookup", "tipi-ospite"],
    queryFn: () => lookupService.getTipiOspite(),
  });

  const { data: articoliLookup = [] } = useQuery({
    queryKey: ["lookup", "articoli"],
    queryFn: () => lookupService.getArticoli(),
  });

  const { data: risorseLookup = [] } = useQuery({
    queryKey: ["lookup", "risorse"],
    queryFn: () => lookupService.getRisorse(),
  });

  useEffect(() => {
    if (evento) {
      setFormData({});
    }
  }, [evento]);

  const createMutation = useMutation({
    mutationFn: (data: Partial<Evento>) => eventiService.create(data),
    onSuccess: (newEvento) => {
      navigate(`/eventi/${newEvento.id}`);
    },
  });

  const updateMutation = useMutation({
    mutationFn: async (data: Partial<Evento>) => {
      const result = await eventiService.update(eventoId, {
        ...evento,
        ...data,
      });
      return result;
    },
    onMutate: async (data) => {
      // Optimistic update - aggiorna subito la UI
      await queryClient.cancelQueries({ queryKey: ["evento", eventoId] });
      const previous = queryClient.getQueryData(["evento", eventoId]);
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        ...data,
      }));
      return { previous };
    },
    onError: (_err, _data, context) => {
      // Rollback in caso di errore
      if (context?.previous) {
        queryClient.setQueryData(["evento", eventoId], context.previous);
      }
    },
    onSuccess: () => {
      setHasChanges(false);
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const cambiaStatoMutation = useMutation({
    mutationFn: (stato: StatoEvento) =>
      eventiService.cambiaStato(eventoId, stato),
    onMutate: async (stato) => {
      await queryClient.cancelQueries({ queryKey: ["evento", eventoId] });
      const previous = queryClient.getQueryData(["evento", eventoId]);
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        stato,
      }));
      return { previous };
    },
    onError: (_err, _data, context) => {
      if (context?.previous) {
        queryClient.setQueryData(["evento", eventoId], context.previous);
      }
    },
    onSuccess: () => {
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const duplicaMutation = useMutation({
    mutationFn: () => eventiService.duplica(eventoId),
    onSuccess: (newEvento) => {
      navigate(`/eventi/${newEvento.id}`);
    },
  });

  const ricalcolaQuantitaMutation = useMutation({
    mutationFn: () => eventiService.ricalcolaQuantita(eventoId),
    onSuccess: (updatedEvento) => {
      queryClient.setQueryData(["evento", eventoId], updatedEvento);
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  // Mutations per dettagli - con update ottimistico
  const addOspiteMutation = useMutation({
    mutationFn: (data: Partial<EventoDettaglioOspiti>) =>
      eventiService.addOspite(eventoId, data),
    onSuccess: (newOspite) => {
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliOspiti: [...(old?.dettagliOspiti || []), newOspite],
      }));
      setDialogOpen(null);
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const deleteOspiteMutation = useMutation({
    mutationFn: (id: number) => eventiService.deleteOspite(eventoId, id),
    onMutate: async (id) => {
      await queryClient.cancelQueries({ queryKey: ["evento", eventoId] });
      const previous = queryClient.getQueryData(["evento", eventoId]);
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliOspiti:
          old?.dettagliOspiti?.filter((o: any) => o.id !== id) || [],
      }));
      return { previous };
    },
    onError: (_err, _id, context) => {
      if (context?.previous) {
        queryClient.setQueryData(["evento", eventoId], context.previous);
      }
    },
    onSuccess: () => {
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const addPrelievoMutation = useMutation({
    mutationFn: (data: Partial<EventoDettaglioPrelievo>) =>
      eventiService.addPrelievo(eventoId, data),
    onSuccess: (newPrelievo) => {
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliPrelievo: [...(old?.dettagliPrelievo || []), newPrelievo],
      }));
      setDialogOpen(null);
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const deletePrelievoMutation = useMutation({
    mutationFn: (id: number) => eventiService.deletePrelievo(eventoId, id),
    onMutate: async (id) => {
      await queryClient.cancelQueries({ queryKey: ["evento", eventoId] });
      const previous = queryClient.getQueryData(["evento", eventoId]);
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliPrelievo:
          old?.dettagliPrelievo?.filter((p: any) => p.id !== id) || [],
      }));
      return { previous };
    },
    onError: (_err, _id, context) => {
      if (context?.previous) {
        queryClient.setQueryData(["evento", eventoId], context.previous);
      }
    },
    onSuccess: () => {
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const addRisorsaMutation = useMutation({
    mutationFn: (data: Partial<EventoDettaglioRisorsa>) =>
      eventiService.addRisorsa(eventoId, data),
    onSuccess: (newRisorsa) => {
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliRisorse: [...(old?.dettagliRisorse || []), newRisorsa],
      }));
      setDialogOpen(null);
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  const deleteRisorsaMutation = useMutation({
    mutationFn: (id: number) => eventiService.deleteRisorsa(eventoId, id),
    onMutate: async (id) => {
      await queryClient.cancelQueries({ queryKey: ["evento", eventoId] });
      const previous = queryClient.getQueryData(["evento", eventoId]);
      queryClient.setQueryData(["evento", eventoId], (old: any) => ({
        ...old,
        dettagliRisorse:
          old?.dettagliRisorse?.filter((r: any) => r.id !== id) || [],
      }));
      return { previous };
    },
    onError: (_err, _id, context) => {
      if (context?.previous) {
        queryClient.setQueryData(["evento", eventoId], context.previous);
      }
    },
    onSuccess: () => {
      signalRService.notifyChange("eventi", "updated", { id: eventoId });
    },
  });

  if (isLoading && !isNew) {
    return <Typography>Caricamento...</Typography>;
  }

  const data = isNew ? formData : { ...evento, ...formData };
  const statoInfo = getStatoInfo(data.stato || StatoEvento.Scheda);

  const handleFieldChange = (field: string, value: any) => {
    setFormData((prev) => ({ ...prev, [field]: value }));
    setHasChanges(true);
  };

  const handleSave = () => {
    if (isNew) {
      createMutation.mutate(formData);
    } else {
      updateMutation.mutate(formData);
    }
  };

  const totaleOspiti = (evento?.dettagliOspiti || []).reduce(
    (sum, o) => sum + o.quantita,
    0,
  );

  return (
    <Box>
      {/* Header con colore stato */}
      <Paper
        sx={{
          p: 2,
          mb: 2,
          backgroundColor: statoInfo.color,
          borderLeft: `6px solid ${statoInfo.textColor}`,
        }}
      >
        <Box sx={{ display: "flex", alignItems: "center", gap: 2 }}>
          <IconButton
            onClick={() => navigate("/eventi")}
            sx={{ color: statoInfo.textColor }}
          >
            <BackIcon />
          </IconButton>
          <Box sx={{ flexGrow: 1 }}>
            <Typography
              variant="h5"
              sx={{ color: statoInfo.textColor, fontWeight: "bold" }}
            >
              {statoInfo.label}
            </Typography>
            <Typography variant="subtitle1">
              {data.codice || "Nuovo Evento"} -{" "}
              {data.descrizione || "Senza descrizione"}
            </Typography>
          </Box>
          <Box sx={{ display: "flex", gap: 1 }}>
            {!isNew && (
              <>
                <Button
                  variant="outlined"
                  startIcon={<CopyIcon />}
                  onClick={() => duplicaMutation.mutate()}
                  size="small"
                >
                  Duplica
                </Button>
                <Button
                  variant="outlined"
                  startIcon={<RefreshIcon />}
                  onClick={() => ricalcolaQuantitaMutation.mutate()}
                  size="small"
                >
                  Ricalcola Qta
                </Button>
                {data.stato !== StatoEvento.Confermato && (
                  <Button
                    variant="contained"
                    color="success"
                    startIcon={<ConfirmIcon />}
                    onClick={() =>
                      cambiaStatoMutation.mutate(StatoEvento.Confermato)
                    }
                    size="small"
                  >
                    Conferma
                  </Button>
                )}
              </>
            )}
            <Button
              variant="contained"
              startIcon={<SaveIcon />}
              onClick={handleSave}
              disabled={!hasChanges && !isNew}
            >
              Salva
            </Button>
            {!isNew && (
              <Button
                variant="outlined"
                startIcon={<PrintIcon />}
                onClick={() => {
                  window.open(
                    `http://localhost:5000/api/reports/evento/${eventoId}`,
                    "_blank",
                  );
                }}
              >
                Stampa PDF
              </Button>
            )}
          </Box>
        </Box>
      </Paper>

      {/* Info principali evento */}
      <Paper sx={{ p: 2, mb: 2 }}>
        <Grid container spacing={2}>
          {/* Prima riga: Data, Orari, Tipo */}
          <Grid size={{ xs: 12, md: 2 }}>
            <DatePicker
              label="Data Evento"
              value={data.dataEvento ? dayjs(data.dataEvento) : null}
              onChange={(date) =>
                handleFieldChange("dataEvento", date?.format("YYYY-MM-DD"))
              }
              slotProps={{
                textField: { fullWidth: true, size: "small", required: true },
              }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 1.5 }}>
            <TimePicker
              label="Ora Inizio"
              value={
                data.oraInizio ? dayjs(`2000-01-01T${data.oraInizio}`) : null
              }
              onChange={(time) =>
                handleFieldChange("oraInizio", time?.format("HH:mm:ss"))
              }
              slotProps={{ textField: { fullWidth: true, size: "small" } }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 1.5 }}>
            <TimePicker
              label="Ora Fine"
              value={data.oraFine ? dayjs(`2000-01-01T${data.oraFine}`) : null}
              onChange={(time) =>
                handleFieldChange("oraFine", time?.format("HH:mm:ss"))
              }
              slotProps={{ textField: { fullWidth: true, size: "small" } }}
            />
          </Grid>
          <Grid size={{ xs: 12, md: 3 }}>
            <FormControl fullWidth size="small">
              <InputLabel>Tipo Evento</InputLabel>
              <Select
                value={data.tipoEventoId || ""}
                label="Tipo Evento"
                onChange={(e) =>
                  handleFieldChange("tipoEventoId", e.target.value)
                }
              >
                {tipiEvento.map((t) => (
                  <MenuItem key={t.id} value={t.id}>
                    {t.descrizione}
                  </MenuItem>
                ))}
              </Select>
            </FormControl>
          </Grid>
          <Grid size={{ xs: 12, md: 4 }}>
            <TextField
              label="Descrizione Evento"
              fullWidth
              size="small"
              value={data.descrizione || ""}
              onChange={(e) => handleFieldChange("descrizione", e.target.value)}
              placeholder="es. Matrimonio Rossi-Bianchi"
            />
          </Grid>

          {/* Seconda riga: Cliente e Location */}
          <Grid size={{ xs: 12, md: 6 }}>
            <Autocomplete
              options={clienti}
              getOptionLabel={(option) => option.ragioneSociale || ""}
              value={clienti.find((c) => c.id === data.clienteId) || null}
              onChange={(_, newValue) =>
                handleFieldChange("clienteId", newValue?.id)
              }
              renderInput={(params) => (
                <TextField {...params} label="Cliente" size="small" fullWidth />
              )}
            />
          </Grid>
          <Grid size={{ xs: 12, md: 6 }}>
            <Autocomplete
              options={locations}
              getOptionLabel={(option) => option.nome || ""}
              value={locations.find((l) => l.id === data.locationId) || null}
              onChange={(_, newValue) =>
                handleFieldChange("locationId", newValue?.id)
              }
              renderInput={(params) => (
                <TextField
                  {...params}
                  label="Location"
                  size="small"
                  fullWidth
                />
              )}
            />
          </Grid>

          {/* Terza riga: Dati economici */}
          <Grid size={{ xs: 6, md: 2 }}>
            <TextField
              label="N. Ospiti Totale"
              type="number"
              fullWidth
              size="small"
              value={data.numeroOspiti || totaleOspiti || ""}
              onChange={(e) =>
                handleFieldChange(
                  "numeroOspiti",
                  parseInt(e.target.value) || undefined,
                )
              }
              InputProps={{ readOnly: totaleOspiti > 0 }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 2 }}>
            <TextField
              label="Costo a Persona"
              type="number"
              fullWidth
              size="small"
              value={data.costoPersona || ""}
              onChange={(e) =>
                handleFieldChange(
                  "costoPersona",
                  parseFloat(e.target.value) || undefined,
                )
              }
              InputProps={{
                startAdornment: <Typography sx={{ mr: 0.5 }}>€</Typography>,
              }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 2 }}>
            <TextField
              label="Costo Totale"
              type="number"
              fullWidth
              size="small"
              value={data.costoTotale || ""}
              onChange={(e) =>
                handleFieldChange(
                  "costoTotale",
                  parseFloat(e.target.value) || undefined,
                )
              }
              InputProps={{
                startAdornment: <Typography sx={{ mr: 0.5 }}>€</Typography>,
              }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 2 }}>
            <TextField
              label="Totale Acconti"
              fullWidth
              size="small"
              value={`€ ${(data.totaleAcconti || 0).toFixed(2)}`}
              InputProps={{ readOnly: true }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 2 }}>
            <TextField
              label="Saldo"
              fullWidth
              size="small"
              value={`€ ${(data.saldo || (data.costoTotale || 0) - (data.totaleAcconti || 0)).toFixed(2)}`}
              InputProps={{ readOnly: true }}
              sx={{
                "& input": {
                  color: (data.saldo || 0) > 0 ? "error.main" : "success.main",
                  fontWeight: "bold",
                },
              }}
            />
          </Grid>
          <Grid size={{ xs: 6, md: 2 }}>
            <FormControl fullWidth size="small">
              <InputLabel>Stato</InputLabel>
              <Select
                value={data.stato ?? StatoEvento.Scheda}
                label="Stato"
                onChange={(e) => {
                  if (!isNew) {
                    cambiaStatoMutation.mutate(e.target.value as StatoEvento);
                  } else {
                    handleFieldChange("stato", e.target.value);
                  }
                }}
              >
                <MenuItem value={StatoEvento.Scheda}>Scheda</MenuItem>
                <MenuItem value={StatoEvento.Preventivo}>Preventivo</MenuItem>
                <MenuItem value={StatoEvento.Confermato}>Confermato</MenuItem>
              </Select>
            </FormControl>
          </Grid>
        </Grid>
      </Paper>

      {/* Tabs per dettagli */}
      {!isNew && (
        <Paper sx={{ p: 2 }}>
          <Tabs
            value={tabValue}
            onChange={(_, v) => setTabValue(v)}
            sx={{ borderBottom: 1, borderColor: "divider" }}
          >
            <Tab label={`Ospiti (${evento?.dettagliOspiti?.length || 0})`} />
            <Tab
              label={`Lista Prelievo (${evento?.dettagliPrelievo?.length || 0})`}
            />
            <Tab label={`Risorse (${evento?.dettagliRisorse?.length || 0})`} />
            <Tab label="Costi" />
            <Tab label="Note" />
          </Tabs>

          {/* Tab Ospiti */}
          <TabPanel value={tabValue} index={0}>
            <Box
              sx={{ display: "flex", justifyContent: "space-between", mb: 2 }}
            >
              <Typography variant="subtitle2" color="textSecondary">
                Totale ospiti: <strong>{totaleOspiti}</strong>
              </Typography>
              <Button
                startIcon={<AddIcon />}
                variant="contained"
                size="small"
                onClick={() => {
                  setDialogData({});
                  setDialogOpen("ospite");
                }}
              >
                Aggiungi Tipo Ospite
              </Button>
            </Box>
            <TableContainer>
              <Table size="small">
                <TableHead>
                  <TableRow sx={{ backgroundColor: "grey.100" }}>
                    <TableCell>
                      <strong>Tipo Ospite</strong>
                    </TableCell>
                    <TableCell align="right">
                      <strong>Quantità</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Note</strong>
                    </TableCell>
                    <TableCell width={50}></TableCell>
                  </TableRow>
                </TableHead>
                <TableBody>
                  {evento?.dettagliOspiti?.map((o) => (
                    <TableRow key={o.id} hover>
                      <TableCell>{o.tipoOspite?.descrizione}</TableCell>
                      <TableCell align="right">
                        <Chip label={o.quantita} color="primary" size="small" />
                      </TableCell>
                      <TableCell>{o.note}</TableCell>
                      <TableCell>
                        <IconButton
                          size="small"
                          color="error"
                          onClick={() => deleteOspiteMutation.mutate(o.id)}
                        >
                          <DeleteIcon fontSize="small" />
                        </IconButton>
                      </TableCell>
                    </TableRow>
                  ))}
                  {(!evento?.dettagliOspiti ||
                    evento.dettagliOspiti.length === 0) && (
                    <TableRow>
                      <TableCell
                        colSpan={4}
                        align="center"
                        sx={{ py: 4, color: "text.secondary" }}
                      >
                        Nessun ospite aggiunto. Clicca "Aggiungi Tipo Ospite"
                        per iniziare.
                      </TableCell>
                    </TableRow>
                  )}
                </TableBody>
              </Table>
            </TableContainer>
          </TabPanel>

          {/* Tab Lista Prelievo */}
          <TabPanel value={tabValue} index={1}>
            <Box
              sx={{ display: "flex", justifyContent: "space-between", mb: 2 }}
            >
              <Typography variant="subtitle2" color="textSecondary">
                Articoli in lista:{" "}
                <strong>{evento?.dettagliPrelievo?.length || 0}</strong>
              </Typography>
              <Button
                startIcon={<AddIcon />}
                variant="contained"
                size="small"
                onClick={() => {
                  setDialogData({});
                  setDialogOpen("prelievo");
                }}
              >
                Aggiungi Articolo
              </Button>
            </Box>
            <TableContainer>
              <Table size="small">
                <TableHead>
                  <TableRow sx={{ backgroundColor: "grey.100" }}>
                    <TableCell>
                      <strong>Codice</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Articolo</strong>
                    </TableCell>
                    <TableCell align="right">
                      <strong>Qta Richiesta</strong>
                    </TableCell>
                    <TableCell align="right">
                      <strong>Qta Calcolata</strong>
                    </TableCell>
                    <TableCell align="right">
                      <strong>Qta Effettiva</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Note</strong>
                    </TableCell>
                    <TableCell width={50}></TableCell>
                  </TableRow>
                </TableHead>
                <TableBody>
                  {evento?.dettagliPrelievo?.map((p) => (
                    <TableRow key={p.id} hover>
                      <TableCell>
                        <Chip
                          label={p.articolo?.codice}
                          size="small"
                          variant="outlined"
                        />
                      </TableCell>
                      <TableCell>{p.articolo?.descrizione}</TableCell>
                      <TableCell align="right">
                        {p.qtaRichiesta || "-"}
                      </TableCell>
                      <TableCell align="right">
                        {p.qtaCalcolata?.toFixed(0) || "-"}
                      </TableCell>
                      <TableCell align="right">
                        {p.qtaEffettiva || "-"}
                      </TableCell>
                      <TableCell>{p.note}</TableCell>
                      <TableCell>
                        <IconButton
                          size="small"
                          color="error"
                          onClick={() => deletePrelievoMutation.mutate(p.id)}
                        >
                          <DeleteIcon fontSize="small" />
                        </IconButton>
                      </TableCell>
                    </TableRow>
                  ))}
                  {(!evento?.dettagliPrelievo ||
                    evento.dettagliPrelievo.length === 0) && (
                    <TableRow>
                      <TableCell
                        colSpan={7}
                        align="center"
                        sx={{ py: 4, color: "text.secondary" }}
                      >
                        Nessun articolo in lista. Clicca "Aggiungi Articolo" per
                        iniziare.
                      </TableCell>
                    </TableRow>
                  )}
                </TableBody>
              </Table>
            </TableContainer>
          </TabPanel>

          {/* Tab Risorse */}
          <TabPanel value={tabValue} index={2}>
            <Box
              sx={{ display: "flex", justifyContent: "space-between", mb: 2 }}
            >
              <Typography variant="subtitle2" color="textSecondary">
                Risorse assegnate:{" "}
                <strong>{evento?.dettagliRisorse?.length || 0}</strong>
              </Typography>
              <Button
                startIcon={<AddIcon />}
                variant="contained"
                size="small"
                onClick={() => {
                  setDialogData({});
                  setDialogOpen("risorsa");
                }}
              >
                Aggiungi Risorsa
              </Button>
            </Box>
            <TableContainer>
              <Table size="small">
                <TableHead>
                  <TableRow sx={{ backgroundColor: "grey.100" }}>
                    <TableCell>
                      <strong>Risorsa</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Ruolo</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Ora Inizio</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Ora Fine</strong>
                    </TableCell>
                    <TableCell>
                      <strong>Note</strong>
                    </TableCell>
                    <TableCell width={50}></TableCell>
                  </TableRow>
                </TableHead>
                <TableBody>
                  {evento?.dettagliRisorse?.map((r) => (
                    <TableRow key={r.id} hover>
                      <TableCell>
                        <strong>
                          {r.risorsa?.nome} {r.risorsa?.cognome}
                        </strong>
                      </TableCell>
                      <TableCell>{r.ruolo}</TableCell>
                      <TableCell>{r.oraInizio}</TableCell>
                      <TableCell>{r.oraFine}</TableCell>
                      <TableCell>{r.note}</TableCell>
                      <TableCell>
                        <IconButton
                          size="small"
                          color="error"
                          onClick={() => deleteRisorsaMutation.mutate(r.id)}
                        >
                          <DeleteIcon fontSize="small" />
                        </IconButton>
                      </TableCell>
                    </TableRow>
                  ))}
                  {(!evento?.dettagliRisorse ||
                    evento.dettagliRisorse.length === 0) && (
                    <TableRow>
                      <TableCell
                        colSpan={6}
                        align="center"
                        sx={{ py: 4, color: "text.secondary" }}
                      >
                        Nessuna risorsa assegnata. Clicca "Aggiungi Risorsa" per
                        iniziare.
                      </TableCell>
                    </TableRow>
                  )}
                </TableBody>
              </Table>
            </TableContainer>
          </TabPanel>

          {/* Tab Costi */}
          <TabPanel value={tabValue} index={3}>
            <EventoCostiPanel eventoId={eventoId} />
          </TabPanel>

          {/* Tab Note */}
          <TabPanel value={tabValue} index={4}>
            <Grid container spacing={2}>
              <Grid size={{ xs: 12, md: 6 }}>
                <TextField
                  label="Note Interne"
                  multiline
                  rows={4}
                  fullWidth
                  value={data.noteInterne || ""}
                  onChange={(e) =>
                    handleFieldChange("noteInterne", e.target.value)
                  }
                  helperText="Visibili solo internamente"
                />
              </Grid>
              <Grid size={{ xs: 12, md: 6 }}>
                <TextField
                  label="Note Cliente"
                  multiline
                  rows={4}
                  fullWidth
                  value={data.noteCliente || ""}
                  onChange={(e) =>
                    handleFieldChange("noteCliente", e.target.value)
                  }
                  helperText="Da comunicare al cliente"
                />
              </Grid>
              <Grid size={{ xs: 12, md: 6 }}>
                <TextField
                  label="Note Cucina"
                  multiline
                  rows={4}
                  fullWidth
                  value={data.noteCucina || ""}
                  onChange={(e) =>
                    handleFieldChange("noteCucina", e.target.value)
                  }
                  helperText="Istruzioni per la cucina"
                />
              </Grid>
              <Grid size={{ xs: 12, md: 6 }}>
                <TextField
                  label="Note Allestimento"
                  multiline
                  rows={4}
                  fullWidth
                  value={data.noteAllestimento || ""}
                  onChange={(e) =>
                    handleFieldChange("noteAllestimento", e.target.value)
                  }
                  helperText="Istruzioni per l'allestimento"
                />
              </Grid>
            </Grid>
          </TabPanel>
        </Paper>
      )}

      {/* Dialog Ospite */}
      <Dialog
        open={dialogOpen === "ospite"}
        onClose={() => setDialogOpen(null)}
        maxWidth="xs"
        fullWidth
      >
        <DialogTitle>Aggiungi Tipo Ospite</DialogTitle>
        <DialogContent>
          <Box sx={{ display: "flex", flexDirection: "column", gap: 2, mt: 1 }}>
            <FormControl fullWidth>
              <InputLabel>Tipo Ospite</InputLabel>
              <Select
                value={dialogData.tipoOspiteId || ""}
                label="Tipo Ospite"
                onChange={(e) =>
                  setDialogData({ ...dialogData, tipoOspiteId: e.target.value })
                }
              >
                {tipiOspite.map((t) => (
                  <MenuItem key={t.id} value={t.id}>
                    {t.descrizione}
                  </MenuItem>
                ))}
              </Select>
            </FormControl>
            <TextField
              label="Quantità"
              type="number"
              fullWidth
              value={dialogData.quantita || ""}
              onChange={(e) =>
                setDialogData({
                  ...dialogData,
                  quantita: parseInt(e.target.value),
                })
              }
            />
            <TextField
              label="Note"
              fullWidth
              multiline
              rows={2}
              value={dialogData.note || ""}
              onChange={(e) =>
                setDialogData({ ...dialogData, note: e.target.value })
              }
            />
          </Box>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setDialogOpen(null)}>Annulla</Button>
          <Button
            variant="contained"
            onClick={() => addOspiteMutation.mutate(dialogData)}
          >
            Aggiungi
          </Button>
        </DialogActions>
      </Dialog>

      {/* Dialog Prelievo */}
      <Dialog
        open={dialogOpen === "prelievo"}
        onClose={() => setDialogOpen(null)}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>Aggiungi Articolo alla Lista</DialogTitle>
        <DialogContent>
          <Box sx={{ display: "flex", flexDirection: "column", gap: 2, mt: 1 }}>
            <Autocomplete
              options={articoliLookup}
              getOptionLabel={(option) =>
                `${option.codice} - ${option.descrizione}`
              }
              onChange={(_, newValue) =>
                setDialogData({ ...dialogData, articoloId: newValue?.id })
              }
              renderInput={(params) => (
                <TextField {...params} label="Articolo" fullWidth />
              )}
            />
            <TextField
              label="Quantità Richiesta"
              type="number"
              fullWidth
              value={dialogData.qtaRichiesta || ""}
              onChange={(e) =>
                setDialogData({
                  ...dialogData,
                  qtaRichiesta: parseFloat(e.target.value),
                })
              }
            />
            <TextField
              label="Note"
              fullWidth
              multiline
              rows={2}
              value={dialogData.note || ""}
              onChange={(e) =>
                setDialogData({ ...dialogData, note: e.target.value })
              }
            />
          </Box>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setDialogOpen(null)}>Annulla</Button>
          <Button
            variant="contained"
            onClick={() => addPrelievoMutation.mutate(dialogData)}
          >
            Aggiungi
          </Button>
        </DialogActions>
      </Dialog>

      {/* Dialog Risorsa */}
      <Dialog
        open={dialogOpen === "risorsa"}
        onClose={() => setDialogOpen(null)}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>Aggiungi Risorsa</DialogTitle>
        <DialogContent>
          <Box sx={{ display: "flex", flexDirection: "column", gap: 2, mt: 1 }}>
            <Autocomplete
              options={risorseLookup}
              getOptionLabel={(option) =>
                `${option.nome} ${option.cognome || ""} - ${option.tipo || ""}`
              }
              onChange={(_, newValue) =>
                setDialogData({ ...dialogData, risorsaId: newValue?.id })
              }
              renderInput={(params) => (
                <TextField {...params} label="Risorsa" fullWidth />
              )}
            />
            <TextField
              label="Ruolo"
              fullWidth
              value={dialogData.ruolo || ""}
              onChange={(e) =>
                setDialogData({ ...dialogData, ruolo: e.target.value })
              }
              placeholder="es. Cameriere, Cuoco, etc."
            />
            <Grid container spacing={2}>
              <Grid size={{ xs: 6 }}>
                <TimePicker
                  label="Ora Inizio"
                  value={
                    dialogData.oraInizio
                      ? dayjs(`2000-01-01T${dialogData.oraInizio}`)
                      : null
                  }
                  onChange={(time) =>
                    setDialogData({
                      ...dialogData,
                      oraInizio: time?.format("HH:mm:ss"),
                    })
                  }
                  slotProps={{ textField: { fullWidth: true } }}
                />
              </Grid>
              <Grid size={{ xs: 6 }}>
                <TimePicker
                  label="Ora Fine"
                  value={
                    dialogData.oraFine
                      ? dayjs(`2000-01-01T${dialogData.oraFine}`)
                      : null
                  }
                  onChange={(time) =>
                    setDialogData({
                      ...dialogData,
                      oraFine: time?.format("HH:mm:ss"),
                    })
                  }
                  slotProps={{ textField: { fullWidth: true } }}
                />
              </Grid>
            </Grid>
            <TextField
              label="Note"
              fullWidth
              multiline
              rows={2}
              value={dialogData.note || ""}
              onChange={(e) =>
                setDialogData({ ...dialogData, note: e.target.value })
              }
            />
          </Box>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setDialogOpen(null)}>Annulla</Button>
          <Button
            variant="contained"
            onClick={() => addRisorsaMutation.mutate(dialogData)}
          >
            Aggiungi
          </Button>
        </DialogActions>
      </Dialog>
    </Box>
  );
}
