"use client";

import { useActionState } from "react";
import { FormField, FormTextarea } from "@/components/admin/FormField";
import { ImageUploadField } from "@/components/admin/ImageUploadField";
import type { EventFormState } from "@/app/(admin-group)/admin/events/actions";
import type { Event } from "@/generated/prisma/client";

interface EventFormProps {
  action: (
    state: EventFormState,
    formData: FormData
  ) => Promise<EventFormState>;
  event?: Event;
  submitLabel: string;
}

export function EventForm({ action, event, submitLabel }: EventFormProps) {
  const [state, formAction, isPending] = useActionState<
    EventFormState,
    FormData
  >(action, undefined);

  return (
    <form action={formAction} className="space-y-5">
      <FormField
        label="Judul"
        name="title"
        defaultValue={event?.title}
        error={state?.errors?.title?.[0]}
        required
      />

      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
        <FormField
          label="Lokasi"
          name="location"
          defaultValue={event?.location ?? ""}
        />
        <FormField
          label="Tanggal"
          name="date"
          defaultValue={event?.date ?? ""}
          placeholder="cth. 15 Agustus 2026"
        />
        <FormField
          label="Harga"
          name="price"
          defaultValue={event?.price ?? ""}
        />
        <FormField
          label="Badge"
          name="badge"
          defaultValue={event?.badge ?? ""}
          placeholder="cth. Populer"
        />
      </div>

      <FormTextarea
        label="Ringkasan Singkat"
        name="excerpt"
        rows={2}
        defaultValue={event?.excerpt ?? ""}
      />

      <FormTextarea
        label="Deskripsi Lengkap"
        name="description"
        rows={5}
        defaultValue={event?.description ?? ""}
      />

      <ImageUploadField
        name="image"
        label="Gambar"
        defaultImageUrl={event?.imageUrl}
      />

      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
        <FormField
          label="Urutan Tampil"
          name="sortOrder"
          type="number"
          defaultValue={event?.sortOrder ?? 0}
        />
        <div className="flex items-center gap-2 pt-6">
          <input
            id="isFeatured"
            name="isFeatured"
            type="checkbox"
            defaultChecked={event?.isFeatured ?? false}
            className="h-4 w-4 rounded border-gray-300 text-merah-ranata focus:ring-merah-ranata"
          />
          <label htmlFor="isFeatured" className="text-sm text-heading">
            Tampilkan sebagai unggulan
          </label>
        </div>
      </div>

      {state?.message && (
        <p className="rounded-lg bg-red-50 px-3 py-2 text-sm text-red-600">
          {state.message}
        </p>
      )}

      <button
        type="submit"
        disabled={isPending}
        className="rounded-full bg-merah-ranata px-6 py-2.5 text-sm font-semibold text-white transition hover:bg-merah-hover disabled:opacity-60"
      >
        {isPending ? "Menyimpan..." : submitLabel}
      </button>
    </form>
  );
}
