"use client";

import { useActionState, useEffect } from "react";
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 {
  createCustomerAction,
  updateCustomerAction,
} from "@/lib/appwrite/customer-actions";
import { initialCustomerState } from "@/lib/appwrite/customer-types";
import type { CustomerRow } from "./types";

type Props = {
  open: boolean;
  onOpenChange: (v: boolean) => void;
  customer?: CustomerRow | null;
};

export function CustomerFormSheet({ open, onOpenChange, customer }: Props) {
  const isEdit = Boolean(customer);
  const action = isEdit ? updateCustomerAction : createCustomerAction;
  const [state, formAction, isPending] = useActionState(action, initialCustomerState);

  useEffect(() => {
    if (state.ok) {
      toast.success(isEdit ? "Müşteri güncellendi." : "Müşteri eklendi.");
      onOpenChange(false);
    } else if (state.error) {
      toast.error(state.error);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [state]);

  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 ? "Müşteriyi düzenle" : "Yeni müşteri"}</SheetTitle>
          <SheetDescription>
            {isEdit
              ? "Müşteri bilgilerini güncelleyin."
              : "Yeni bir müşteri ekleyin. * işaretli alanlar zorunludur."}
          </SheetDescription>
        </SheetHeader>

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

          <div className="flex-1 space-y-4 overflow-y-auto px-6 py-5">
            <div className="grid gap-2">
              <Label htmlFor="name">Ad / Şirket adı *</Label>
              <Input
                id="name"
                name="name"
                defaultValue={customer?.name ?? ""}
                placeholder="Örn. Acme Yazılım Ltd."
                required
              />
              {state.fieldErrors?.name && (
                <p className="text-destructive text-xs">{state.fieldErrors.name}</p>
              )}
            </div>

            <div className="grid gap-4 md:grid-cols-2">
              <div className="grid gap-2">
                <Label htmlFor="email">Email</Label>
                <Input
                  id="email"
                  name="email"
                  type="email"
                  defaultValue={customer?.email ?? ""}
                  placeholder="info@acme.com"
                />
                {state.fieldErrors?.email && (
                  <p className="text-destructive text-xs">{state.fieldErrors.email}</p>
                )}
              </div>
              <div className="grid gap-2">
                <Label htmlFor="phone">Telefon</Label>
                <Input
                  id="phone"
                  name="phone"
                  type="tel"
                  defaultValue={customer?.phone ?? ""}
                  placeholder="+90 555 123 45 67"
                />
              </div>
            </div>

            <div className="grid gap-4 md:grid-cols-2">
              <div className="grid gap-2">
                <Label htmlFor="taxId">Vergi numarası</Label>
                <Input
                  id="taxId"
                  name="taxId"
                  defaultValue={customer?.taxId ?? ""}
                  placeholder="1234567890"
                  inputMode="numeric"
                />
              </div>
              <div className="grid gap-2">
                <Label htmlFor="status">Durum</Label>
                <Select name="status" defaultValue={customer?.status ?? "active"}>
                  <SelectTrigger id="status">
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="active">Aktif</SelectItem>
                    <SelectItem value="passive">Pasif</SelectItem>
                  </SelectContent>
                </Select>
              </div>
            </div>

            <div className="grid gap-2">
              <Label htmlFor="address">Adres</Label>
              <Textarea
                id="address"
                name="address"
                rows={2}
                defaultValue={customer?.address ?? ""}
                placeholder="Açık adres"
              />
            </div>

            <div className="grid gap-2">
              <Label htmlFor="notes">Notlar</Label>
              <Textarea
                id="notes"
                name="notes"
                rows={4}
                defaultValue={customer?.notes ?? ""}
                placeholder="Müşteriye özel notlar"
              />
            </div>
          </div>

          <SheetFooter className="border-t bg-muted/30 px-6 py-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}>
                {isPending ? (
                  <>
                    <Loader2 className="size-4 animate-spin" />
                    Kaydediliyor...
                  </>
                ) : (
                  <>
                    <Save className="size-4" />
                    {isEdit ? "Güncelle" : "Kaydet"}
                  </>
                )}
              </Button>
            </div>
          </SheetFooter>
        </form>
      </SheetContent>
    </Sheet>
  );
}
