"use client";

import { useActionState } from "react";
import { FormField, FormTextarea } from "@/components/admin/FormField";
import { ImageUploadField } from "@/components/admin/ImageUploadField";
import type { FlightFormState } from "@/app/(admin-group)/admin/flights/actions";
import type { Flight } from "@/generated/prisma/client";

interface FlightFormProps {
  action: (
    state: FlightFormState,
    formData: FormData
  ) => Promise<FlightFormState>;
  flight?: Flight;
  submitLabel: string;
}

export function FlightForm({ action, flight, submitLabel }: FlightFormProps) {
  const [state, formAction, isPending] = useActionState<
    FlightFormState,
    FormData
  >(action, undefined);

  return (
    <form action={formAction} className="space-y-5">
      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
        <FormField
          label="Maskapai"
          name="airline"
          defaultValue={flight?.airline}
          error={state?.errors?.airline?.[0]}
          required
        />
        <FormField
          label="Nomor Penerbangan"
          name="flightNumber"
          defaultValue={flight?.flightNumber ?? ""}
          placeholder="cth. GA-410"
        />
        <FormField
          label="Kota Asal"
          name="origin"
          defaultValue={flight?.origin}
          error={state?.errors?.origin?.[0]}
          required
        />
        <FormField
          label="Kota Tujuan"
          name="destination"
          defaultValue={flight?.destination}
          error={state?.errors?.destination?.[0]}
          required
        />
        <FormField
          label="Info Keberangkatan"
          name="departureInfo"
          defaultValue={flight?.departureInfo ?? ""}
          placeholder="cth. Setiap hari, 08:00 WIB"
        />
        <FormField
          label="Harga"
          name="price"
          defaultValue={flight?.price ?? ""}
        />
        <FormField
          label="Badge"
          name="badge"
          defaultValue={flight?.badge ?? ""}
          placeholder="cth. Promo"
        />
      </div>

      <FormTextarea
        label="Ringkasan Singkat"
        name="excerpt"
        rows={2}
        defaultValue={flight?.excerpt ?? ""}
      />

      <FormTextarea
        label="Deskripsi Lengkap"
        name="description"
        rows={5}
        defaultValue={flight?.description ?? ""}
      />

      <ImageUploadField
        name="image"
        label="Gambar"
        defaultImageUrl={flight?.imageUrl}
      />

      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
        <FormField
          label="Urutan Tampil"
          name="sortOrder"
          type="number"
          defaultValue={flight?.sortOrder ?? 0}
        />
        <div className="flex items-center gap-2 pt-6">
          <input
            id="isFeatured"
            name="isFeatured"
            type="checkbox"
            defaultChecked={flight?.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>
  );
}
