import { Save } from "lucide-react";
import {
  Checkbox,
  Field,
  FormActions,
  FormShell,
  GhostLink,
  PageHeader,
  PrimaryButton,
  Textarea,
} from "@/components/admin/form";
import { saveService } from "@/lib/admin-actions";
import type { ServiceRow } from "@/lib/types";

const ICON_OPTIONS = [
  "Globe",
  "ShoppingCart",
  "Smartphone",
  "Code2",
  "Users",
  "TrendingUp",
  "Share2",
  "Megaphone",
  "Layers",
];

export function ServiceForm({ service }: { service?: ServiceRow }) {
  return (
    <div>
      <PageHeader
        title={service ? "Hizmeti düzenle" : "Yeni hizmet"}
        backHref="/admin/hizmetler"
      />
      <form action={saveService}>
        {service && <input type="hidden" name="id" value={service.$id} />}
        <FormShell>
          <div className="grid gap-5 md:grid-cols-2">
            <Field label="Başlık" name="title" required defaultValue={service?.title} />
            <Field label="Slug" name="slug" defaultValue={service?.slug} />
            <Field
              label="Sıra"
              name="order"
              type="number"
              defaultValue={service?.order ?? 0}
            />
            <label className="block">
              <span className="text-sm font-medium text-[var(--navy)]">İkon</span>
              <select
                name="icon"
                defaultValue={service?.icon ?? "Layers"}
                className="mt-1.5 w-full rounded-xl border border-[var(--border)] bg-white px-4 py-2.5 text-sm outline-none focus:border-[var(--sky)] focus:ring-2 focus:ring-[var(--sky)]/20"
              >
                {ICON_OPTIONS.map((i) => (
                  <option key={i} value={i}>
                    {i}
                  </option>
                ))}
              </select>
              <span className="mt-1 block text-xs text-[var(--muted)]">
                Lucide icon adı.
              </span>
            </label>
          </div>
          <div className="mt-5">
            <Textarea
              label="Açıklama"
              name="description"
              required
              defaultValue={service?.description}
              rows={4}
            />
          </div>
          <div className="mt-5">
            <Checkbox
              label="Öne çıkar (Anasayfada göster)"
              name="featured"
              defaultChecked={service?.featured ?? false}
            />
          </div>
          <FormActions>
            <GhostLink href="/admin/hizmetler">İptal</GhostLink>
            <PrimaryButton>
              <Save className="size-4" /> Kaydet
            </PrimaryButton>
          </FormActions>
        </FormShell>
      </form>
    </div>
  );
}
