"use client";

import { useActionState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { Loader2, Save } from "lucide-react";
import { toast } from "sonner";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetTitle,
} from "@/components/ui/sheet";
import { Textarea } from "@/components/ui/textarea";
import {
  createInvoiceAction,
  updateInvoiceAction,
} from "@/lib/appwrite/invoice-actions";
import { initialInvoiceState } from "@/lib/appwrite/invoice-types";

import type { Customer, InvoiceRow } from "./types";

type Props = {
  open: boolean;
  onOpenChange: (v: boolean) => void;
  invoice?: InvoiceRow | null;
  customers: Customer[];
};

function isoToDate(iso: string): string {
  if (!iso) return "";
  return iso.slice(0, 10);
}

export function InvoiceFormSheet({ open, onOpenChange, invoice, customers }: Props) {
  const isEdit = Boolean(invoice);
  const action = isEdit ? updateInvoiceAction : createInvoiceAction;
  const [state, formAction, isPending] = useActionState(action, initialInvoiceState);
  const router = useRouter();

  useEffect(() => {
    if (state.ok) {
      toast.success(isEdit ? "Fatura güncellendi." : "Fatura oluşturuldu.");
      onOpenChange(false);
      if (!isEdit && state.invoiceId) {
        router.push(`/invoices/${state.invoiceId}`);
      }
    } else if (state.error) {
      toast.error(state.error);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [state]);

  const today = new Date().toISOString().slice(0, 10);
  const inThirty = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
    .toISOString()
    .slice(0, 10);

  return (
    <Sheet open={open} onOpenChange={onOpenChange}>
      <SheetContent className="flex w-full flex-col gap-0 p-0 sm:max-w-xl">
        <SheetHeader className="border-b px-6 py-4">
          <SheetTitle>{isEdit ? "Faturayı düzenle" : "Yeni fatura"}</SheetTitle>
          <SheetDescription>
            {isEdit
              ? "Fatura bilgilerini güncelleyin. Kalem eklemek için fatura detayına gidin."
              : "Faturayı oluşturun, ardından detay sayfasında kalemleri ekleyin. Numara otomatik üretilir."}
          </SheetDescription>
        </SheetHeader>

        <form action={formAction} className="flex flex-1 flex-col">
          {isEdit && invoice && <input type="hidden" name="id" value={invoice.id} />}

          <div className="flex-1 space-y-4 overflow-y-auto px-6 py-5">
            <div className="grid gap-2">
              <Label htmlFor="customerId">Müşteri *</Label>
              <Select
                name="customerId"
                defaultValue={invoice?.customerId ?? ""}
                disabled={customers.length === 0}
              >
                <SelectTrigger id="customerId">
                  <SelectValue placeholder="Müşteri seçin" />
                </SelectTrigger>
                <SelectContent>
                  {customers.map((c) => (
                    <SelectItem key={c.id} value={c.id}>
                      {c.name}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
              {state.fieldErrors?.customerId && (
                <p className="text-destructive text-xs">{state.fieldErrors.customerId}</p>
              )}
            </div>

            <div className="grid gap-4 md:grid-cols-2">
              <div className="grid gap-2">
                <Label htmlFor="issueDate">Düzenleme tarihi *</Label>
                <Input
                  id="issueDate"
                  name="issueDate"
                  type="date"
                  defaultValue={isoToDate(invoice?.issueDate ?? today)}
                  required
                />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="dueDate">Vade tarihi *</Label>
                <Input
                  id="dueDate"
                  name="dueDate"
                  type="date"
                  defaultValue={isoToDate(invoice?.dueDate ?? inThirty)}
                  required
                />
              </div>
            </div>

            <div className="grid gap-2">
              <Label htmlFor="status">Durum</Label>
              <Select name="status" defaultValue={invoice?.status ?? "draft"}>
                <SelectTrigger id="status">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="draft">Taslak</SelectItem>
                  <SelectItem value="sent">Gönderildi</SelectItem>
                  <SelectItem value="paid">Ödendi</SelectItem>
                  <SelectItem value="overdue">Gecikmiş</SelectItem>
                  <SelectItem value="cancelled">İptal</SelectItem>
                </SelectContent>
              </Select>
              <p className="text-muted-foreground text-xs">
                &ldquo;Ödendi&rdquo; seçildiğinde finans modülüne otomatik gelir kaydı düşer.
                Durum geri alınırsa kayıt silinir.
              </p>
            </div>

            <div className="grid gap-2">
              <Label htmlFor="notes">Notlar</Label>
              <Textarea
                id="notes"
                name="notes"
                rows={4}
                defaultValue={invoice?.notes ?? ""}
                placeholder="Faturada görünecek not, ödeme talimatları, vb."
              />
            </div>
          </div>

          <SheetFooter className="border-t bg-muted/30 px-6 pt-4">
            <div className="flex w-full justify-end gap-2">
              <Button
                type="button"
                variant="outline"
                onClick={() => onOpenChange(false)}
                disabled={isPending}
              >
                Vazgeç
              </Button>
              <Button type="submit" disabled={isPending || customers.length === 0}>
                {isPending ? (
                  <>
                    <Loader2 className="size-4 animate-spin" />
                    Kaydediliyor...
                  </>
                ) : (
                  <>
                    <Save className="size-4" />
                    {isEdit ? "Güncelle" : "Oluştur"}
                  </>
                )}
              </Button>
            </div>
          </SheetFooter>
        </form>
      </SheetContent>
    </Sheet>
  );
}
