"use client";

import { Bell, Play } from "lucide-react";
import { toast } from "sonner";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { useCruiseData } from "@/hooks/use-cruise-data";
import { getAutomationLog, logAutomation } from "@/lib/cruise-store";
import type { AutomationType } from "@/types/cruise";

const TRIGGER_TYPES: { type: AutomationType; label: string; subject: string }[] = [
  { type: "booking_confirmation", label: "Booking confirmation", subject: "Manual booking confirmation test" },
  { type: "payment_reminder", label: "Payment reminder", subject: "Upcoming payment reminder" },
  { type: "balance_due_reminder", label: "Balance due", subject: "Final balance due soon" },
  { type: "failed_payment", label: "Failed payment", subject: "Payment failed — action required" },
  { type: "booking_update", label: "Booking update", subject: "Your booking has been updated" },
  { type: "marketing_upsell", label: "Marketing upsell", subject: "Exclusive add-ons for your cruise" },
  { type: "cruise_line_confirmation", label: "Cruise line confirmation", subject: "Cruise line booking confirmed" },
];

export default function AutomationsPage() {
  const { data: log, refresh } = useCruiseData(getAutomationLog);

  const triggerManual = (type: AutomationType, subject: string) => {
    logAutomation(type, "staff@cruisebooking.com", subject, undefined, undefined, "sent");
    toast.success(`${type} automation logged.`);
    refresh();
  };

  return (
    <div className="flex flex-col gap-6">
      <div>
        <h1 className="font-semibold text-xl">Automations</h1>
        <p className="text-muted-foreground text-sm">Email log and manual triggers</p>
      </div>

      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2 text-base">
            <Bell className="size-4" />
            Manual triggers
          </CardTitle>
        </CardHeader>
        <CardContent className="grid gap-2 sm:grid-cols-2 lg:grid-cols-3">
          {TRIGGER_TYPES.map(({ type, label, subject }) => (
            <Button key={type} variant="outline" size="sm" className="justify-start" onClick={() => triggerManual(type, subject)}>
              <Play className="mr-2 size-3.5" />
              {label}
            </Button>
          ))}
        </CardContent>
      </Card>

      <Card>
        <CardHeader>
          <CardTitle className="text-base">Automation log</CardTitle>
          <CardDescription>{log.length} entries</CardDescription>
        </CardHeader>
        <CardContent className="p-0">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>Type</TableHead>
                <TableHead>Recipient</TableHead>
                <TableHead>Subject</TableHead>
                <TableHead>Status</TableHead>
                <TableHead>Date</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {log.slice(0, 50).map((entry) => (
                <TableRow key={entry.id}>
                  <TableCell>
                    <Badge variant="outline">{entry.type.replace(/_/g, " ")}</Badge>
                  </TableCell>
                  <TableCell className="max-w-[140px] truncate">{entry.recipient}</TableCell>
                  <TableCell className="max-w-[200px] truncate">{entry.subject}</TableCell>
                  <TableCell>
                    <Badge variant={entry.status === "failed" ? "destructive" : "secondary"}>{entry.status}</Badge>
                  </TableCell>
                  <TableCell className="text-muted-foreground text-xs">
                    {new Date(entry.createdAt).toLocaleString()}
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </CardContent>
      </Card>
    </div>
  );
}
