"use client";

import { useState } from "react";
import { apiFetch, ApiError, toFormData } from "@/lib/api";
import { useList } from "@/lib/useResource";
import { useToast } from "@/components/toast";
import { DataTable, type Column } from "@/components/DataTable";
import {
  Button,
  ConfirmDialog,
  Field,
  Input,
  Modal,
  PageHeader,
  Select,
  Textarea,
} from "@/components/ui";
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
import { ImagePicker } from "@/components/ImagePicker";
import { MediaPreview } from "@/components/MediaPreview";
import { pickUrl, IMAGE_KEYS } from "@/lib/media";
import { toFa } from "@/lib/utils";

interface SliderAction {
  path?: string;
  type?: string;
}

interface Slider {
  id: number;
  title?: string;
  description?: string;
  url?: string;
  action?: SliderAction;
  image_id?: number | null;
  image?: unknown;
}

const ACTION_TYPES = [
  { value: "navigation", label: "صفحه (navigation)" },
  { value: "link", label: "لینک (link)" },
];

export default function SlidersPage() {
  const { data, loading, error, reload } = useList<Slider>("/slider");
  const toast = useToast();

  const [editing, setEditing] = useState<Slider | null>(null);
  const [open, setOpen] = useState(false);
  const [title, setTitle] = useState("");
  const [description, setDescription] = useState("");
  const [url, setUrl] = useState("");
  const [actionPath, setActionPath] = useState("");
  const [actionType, setActionType] = useState("navigation");
  const [imageId, setImageId] = useState<number | null>(null);
  const [imageFile, setImageFile] = useState<File | null>(null);
  const [saving, setSaving] = useState(false);

  const [deleting, setDeleting] = useState<Slider | null>(null);
  const [removing, setRemoving] = useState(false);

  function openCreate() {
    setEditing(null);
    setTitle("");
    setDescription("");
    setUrl("");
    setActionPath("");
    setActionType("navigation");
    setImageId(null);
    setImageFile(null);
    setOpen(true);
  }
  function openEdit(row: Slider) {
    setEditing(row);
    setTitle(row.title ?? "");
    setDescription(row.description ?? "");
    setUrl(row.url ?? "");
    setActionPath(row.action?.path ?? "");
    setActionType(row.action?.type ?? "navigation");
    setImageId(row.image_id ?? null);
    setImageFile(null);
    setOpen(true);
  }

  async function save(e: React.FormEvent) {
    e.preventDefault();
    setSaving(true);
    try {
      // Multipart for both (so an image file can be uploaded). Bracket keys
      // build the nested action object; update uses POST /slider/:id.
      const body = toFormData({
        title,
        description,
        url,
        "action[path]": actionPath,
        "action[type]": actionType,
        image_id: imageId,
        image: imageFile,
      });
      if (editing) {
        await apiFetch(`/slider/${editing.id}`, { method: "POST", body });
        toast.success("اسلایدر ویرایش شد.");
      } else {
        await apiFetch("/slider", { method: "POST", body });
        toast.success("اسلایدر افزوده شد.");
      }
      setOpen(false);
      reload();
    } catch (err) {
      toast.error(err instanceof ApiError ? err.message : "خطا در ذخیره‌سازی");
    } finally {
      setSaving(false);
    }
  }

  async function confirmDelete() {
    if (!deleting) return;
    setRemoving(true);
    try {
      await apiFetch(`/slider/${deleting.id}`, { method: "DELETE" });
      toast.success("اسلایدر حذف شد.");
      setDeleting(null);
      reload();
    } catch (err) {
      toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
    } finally {
      setRemoving(false);
    }
  }

  const columns: Column<Slider>[] = [
    { key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
    {
      key: "image",
      header: "تصویر",
      className: "w-20",
      render: (r) => (
        <MediaPreview kind="image" src={pickUrl(r, IMAGE_KEYS)} label={r.title} />
      ),
    },
    { key: "title", header: "عنوان", render: (r) => r.title ?? "—" },
    {
      key: "description",
      header: "توضیحات",
      render: (r) => r.description ?? "—",
    },
    {
      key: "url",
      header: "آدرس",
      render: (r) =>
        r.url ? (
          <span dir="ltr" className="block truncate">
            {r.url}
          </span>
        ) : (
          "—"
        ),
    },
    {
      key: "action_type",
      header: "نوع اکشن",
      render: (r) => r.action?.type ?? "—",
      className: "w-32",
    },
  ];

  return (
    <div>
      <PageHeader
        title="اسلایدرها"
        subtitle="مدیریت اسلایدرهای صفحه اصلی"
        action={
          <Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
            اسلایدر جدید
          </Button>
        }
      />

      <DataTable
        columns={columns}
        rows={data}
        loading={loading}
        error={error}
        onRetry={reload}
        emptyMessage="هنوز اسلایدری ثبت نشده است."
        emptyAction={
          <Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
            اسلایدر جدید
          </Button>
        }
        actions={(row) => (
          <>
            <Button
              variant="ghost"
              onClick={() => openEdit(row)}
              aria-label="ویرایش"
            >
              <EditIcon className="h-4 w-4" />
            </Button>
            <Button
              variant="ghost"
              onClick={() => setDeleting(row)}
              aria-label="حذف"
              className="text-danger"
            >
              <TrashIcon className="h-4 w-4" />
            </Button>
          </>
        )}
      />

      <Modal
        open={open}
        onClose={() => setOpen(false)}
        title={editing ? "ویرایش اسلایدر" : "اسلایدر جدید"}
        footer={
          <>
            <Button variant="secondary" onClick={() => setOpen(false)}>
              انصراف
            </Button>
            <Button form="slider-form" type="submit" loading={saving}>
              ذخیره
            </Button>
          </>
        }
      >
        <form id="slider-form" onSubmit={save} className="flex flex-col gap-4">
          <Field label="عنوان" required>
            <Input
              value={title}
              onChange={(e) => setTitle(e.target.value)}
              placeholder="مثلاً مدیتیشن صبحگاهی"
              required
              autoFocus
            />
          </Field>

          <Field label="تصویر">
            <ImagePicker
              imageId={imageId}
              onPickId={setImageId}
              file={imageFile}
              onPickFile={setImageFile}
              existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
            />
          </Field>

          <Field label="توضیحات">
            <Textarea
              value={description}
              onChange={(e) => setDescription(e.target.value)}
              placeholder="توضیح کوتاه درباره اسلایدر"
            />
          </Field>

          <Field label="آدرس (URL)">
            <Input
              value={url}
              onChange={(e) => setUrl(e.target.value)}
              placeholder="https://"
              dir="ltr"
            />
          </Field>

          <Field label="مسیر اکشن">
            <Input
              value={actionPath}
              onChange={(e) => setActionPath(e.target.value)}
              placeholder="/screen/path"
              dir="ltr"
            />
          </Field>

          <Field label="نوع اکشن">
            <Select
              value={actionType}
              onChange={(e) => setActionType(e.target.value)}
            >
              {ACTION_TYPES.map((t) => (
                <option key={t.value} value={t.value}>
                  {t.label}
                </option>
              ))}
            </Select>
          </Field>
        </form>
      </Modal>

      <ConfirmDialog
        open={!!deleting}
        message={`آیا از حذف «${deleting?.title}» مطمئن هستید؟`}
        loading={removing}
        onConfirm={confirmDelete}
        onClose={() => setDeleting(null)}
      />
    </div>
  );
}
