"use client";

import { useActionState } from "react";
import { FormField, FormTextarea } from "@/components/admin/FormField";
import { ImageUploadField } from "@/components/admin/ImageUploadField";
import type { TourFormState } from "@/app/(admin-group)/admin/tours/actions";
import type { Tour } from "@/generated/prisma/client";

interface TourFormProps {
  action: (state: TourFormState, formData: FormData) => Promise<TourFormState>;
  tour?: Tour;
  submitLabel: string;
}

export function TourForm({ action, tour, submitLabel }: TourFormProps) {
  const [state, formAction, isPending] = useActionState<TourFormState, FormData>(
    action,
    undefined
  );

  return (
    <form action={formAction} className="space-y-5">
      <FormField
        label="Judul"
        name="title"
        defaultValue={tour?.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={tour?.location ?? ""}
        />
        <FormField label="Harga" name="price" defaultValue={tour?.price ?? ""} />
        <FormField
          label="Durasi"
          name="duration"
          defaultValue={tour?.duration ?? ""}
          placeholder="cth. 4 Hari 3 Malam"
        />
        <FormField
          label="Badge"
          name="badge"
          defaultValue={tour?.badge ?? ""}
          placeholder="cth. Best Seller"
        />
      </div>

      <FormTextarea
        label="Ringkasan Singkat"
        name="excerpt"
        rows={2}
        defaultValue={tour?.excerpt ?? ""}
      />

      <FormTextarea
        label="Deskripsi Lengkap"
        name="description"
        rows={5}
        defaultValue={tour?.description ?? ""}
      />

      <ImageUploadField
        name="image"
        label="Gambar"
        defaultImageUrl={tour?.imageUrl}
      />

      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
        <FormField
          label="Urutan Tampil"
          name="sortOrder"
          type="number"
          defaultValue={tour?.sortOrder ?? 0}
        />
        <div className="flex items-center gap-2 pt-6">
          <input
            id="isFeatured"
            name="isFeatured"
            type="checkbox"
            defaultChecked={tour?.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>
  );
}
