"use client";

import type { ResourceConfig } from "@/components/admin/AdminResource";
import { UserActions } from "@/components/admin/UserActions";
import { UserProfileEdit } from "@/components/admin/UserProfileEdit";

const badge = (ok: boolean, yes: string, no: string) =>
  ok ? (
    <span className="rounded bg-emerald-500/15 px-1.5 py-0.5 text-[11px] text-emerald-300">{yes}</span>
  ) : (
    <span className="rounded bg-gray-500/15 px-1.5 py-0.5 text-[11px] text-gray-400">{no}</span>
  );

const banAction = (row: Record<string, unknown>, reload: () => void) => {
  const banned = !!row.ban_account;
  return (
    <button
      className={
        banned
          ? "rounded-lg border border-emerald-500/30 px-3 py-1.5 text-xs text-emerald-300 hover:bg-emerald-500/10"
          : "rounded-lg border border-red-500/30 px-3 py-1.5 text-xs text-red-300 hover:bg-red-500/10"
      }
      onClick={async () => {
        const reason = banned ? "" : prompt("دلیل مسدودسازی؟") ?? "";
        if (!banned && !reason) return;
        const res = await fetch(`/api/admin/resource/users/${row.id}/ban`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ reason: banned ? "unban" : reason, unbanned: banned }),
        });
        if (res.ok) reload();
      }}
    >
      {banned ? "رفع مسدودی" : "مسدود"}
    </button>
  );
};

export const categoriesConfig: ResourceConfig = {
  title: "دسته‌بندی‌ها",
  description: "دسته‌بندی‌های مورد استفاده در قالب‌ها و سایت عمومی.",
  basePath: "categories",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "image_url", label: "تصویر", type: "image" },
    { key: "icon", label: "آیکون", type: "image" },
    { key: "name", label: "نام" },
    { key: "slug", label: "اسلاگ" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "مخفی") },
    { key: "sort", label: "ترتیب" },
  ],
  fields: [
    { key: "name", label: "نام", required: true },
    { key: "slug", label: "اسلاگ (نشانی)", required: true },
    { key: "description", label: "توضیحات / محتوا", type: "richtext" },
    { key: "image_url", label: "تصویر", type: "image" },
    { key: "icon", label: "آیکون (کد SVG یا نام آیکون)", type: "textarea" },
    // SEO
    { key: "meta_title", label: "سئو · عنوان متا" },
    { key: "meta_description", label: "سئو · توضیحات متا", type: "textarea" },
    { key: "meta_keywords", label: "سئو · کلمات کلیدی (با کاما)" },
    { key: "bot_follow", label: "اجازه دنبال‌کردن توسط موتورهای جستجو", type: "checkbox", defaultValue: true },
    { key: "sort", label: "ترتیب نمایش", type: "number", defaultValue: 0 },
    { key: "is_active", label: "فعال (نمایش در سایت)", type: "checkbox", defaultValue: true },
  ],
};

export const tagsConfig: ResourceConfig = {
  title: "برچسب‌ها",
  description: "برچسب‌های کلیدواژه برای قالب‌ها و محتوا.",
  basePath: "tags",
  listQuery: "pageSize=500&page_size=500",
  listKey: "items",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "name", label: "نام" },
    { key: "slug", label: "اسلاگ" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "مخفی") },
  ],
  fields: [
    { key: "name", label: "نام", required: true },
    { key: "latin_name", label: "نام لاتین" },
    { key: "slug", label: "اسلاگ (نشانی)", required: true },
    { key: "is_active", label: "فعال", type: "checkbox", defaultValue: true },
  ],
};

export const fontsConfig: ResourceConfig = {
  title: "فونت‌ها",
  description: "فونت‌های در دسترس در ویرایشگرهای استودیو.",
  basePath: "fonts",
  listQuery: "pageSize=500&page_size=500",
  listKey: "items",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "name", label: "نام" },
    { key: "family", label: "خانواده" },
    { key: "weight", label: "وزن" },
    { key: "style", label: "سبک" },
  ],
  fields: [
    { key: "name", label: "نام", required: true },
    { key: "original_name", label: "نام اصلی" },
    { key: "system_name", label: "نام سیستمی" },
    { key: "family", label: "خانواده فونت" },
    { key: "weight", label: "وزن", type: "number" },
    { key: "style", label: "سبک" },
  ],
};

export const musicConfig: ResourceConfig = {
  title: "موسیقی",
  description: "ترک‌های صوتی موجود در کتابخانهٔ موسیقی استودیو.",
  basePath: "music",
  listQuery: "pageSize=500&page_size=500",
  listKey: "items",
  canCreate: true,
  canEdit: false,
  canDelete: true,
  columns: [
    { key: "name", label: "نام" },
    { key: "genre", label: "ژانر" },
    { key: "mood", label: "حال‌وهوا" },
    { key: "duration_sec", label: "مدت (ثانیه)" },
    { key: "is_premium", label: "ویژه", render: (r) => badge(!!r.is_premium, "ویژه", "رایگان") },
  ],
  fields: [
    { key: "name", label: "نام", required: true },
    { key: "caption", label: "عنوان" },
    { key: "keywords", label: "کلمات کلیدی" },
    { key: "url", label: "فایل صوتی", type: "file", required: true },
    { key: "duration_sec", label: "مدت (ثانیه)", type: "number", required: true },
    { key: "bpm", label: "ضرب (BPM)", type: "number" },
    { key: "genre", label: "ژانر" },
    { key: "mood", label: "حال‌وهوا" },
    { key: "is_premium", label: "ویژه", type: "checkbox" },
  ],
};

export const blogsConfig: ResourceConfig = {
  title: "مقالات بلاگ",
  description: "مقالات سیستم مدیریت محتوا (تولیدشده توسط هوش مصنوعی نیز).",
  basePath: "blogs",
  listQuery: "pageSize=500&page_size=500",
  listKey: "items",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "title", label: "عنوان" },
    { key: "slug", label: "اسلاگ" },
    { key: "is_published", label: "انتشار", render: (r) => badge(!!r.is_published, "منتشرشده", "پیش‌نویس") },
    { key: "view_count", label: "بازدید" },
  ],
  fields: [
    { key: "title", label: "عنوان", required: true },
    { key: "slug", label: "اسلاگ (نشانی)", required: true },
    { key: "short_description", label: "توضیح کوتاه", type: "textarea" },
    { key: "content", label: "محتوا", type: "richtext", required: true },
    { key: "meta_title", label: "عنوان متا" },
    { key: "meta_description", label: "توضیحات متا", type: "textarea" },
    { key: "meta_keywords", label: "کلمات کلیدی متا" },
    { key: "is_published", label: "منتشرشده", type: "checkbox" },
    { key: "include_in_site_map", label: "نمایش در نقشهٔ سایت", type: "checkbox", defaultValue: true },
  ],
};

export const learnConfig: ResourceConfig = {
  title: "مقالات آموزشی",
  description: "آموزش‌ها و راهنماهای گام‌به‌گام که در بخش «آموزش» سایت نمایش داده می‌شوند.",
  basePath: "blogs",
  listQuery: "kind=Learn&pageSize=500&page_size=500",
  listKey: "items",
  fixedValues: { kind: "Learn" },
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "title", label: "عنوان" },
    { key: "slug", label: "اسلاگ" },
    { key: "is_published", label: "انتشار", render: (r) => badge(!!r.is_published, "منتشرشده", "پیش‌نویس") },
    { key: "view_count", label: "بازدید" },
  ],
  fields: [
    { key: "title", label: "عنوان", required: true },
    { key: "slug", label: "اسلاگ (نشانی)", required: true },
    { key: "short_description", label: "توضیح کوتاه", type: "textarea" },
    { key: "content", label: "محتوا", type: "richtext", required: true },
    { key: "cover", label: "تصویر شاخص", type: "image" },
    { key: "meta_title", label: "عنوان متا" },
    { key: "meta_description", label: "توضیحات متا", type: "textarea" },
    { key: "meta_keywords", label: "کلمات کلیدی متا" },
    { key: "is_published", label: "منتشرشده", type: "checkbox", defaultValue: true },
    { key: "include_in_site_map", label: "نمایش در نقشهٔ سایت", type: "checkbox", defaultValue: true },
  ],
};

export const pagesConfig: ResourceConfig = {
  title: "برگه‌های سایت",
  description: "برگه‌های ثابت مانند «دربارهٔ ما»، «حریم خصوصی» و «شرایط استفاده». اسلاگ باید دقیقاً با نشانی برگه یکی باشد: about، contact، careers، privacy، terms، cookies، help.",
  basePath: "blogs",
  listQuery: "kind=Page&pageSize=500&page_size=500",
  listKey: "items",
  fixedValues: { kind: "Page", is_published: true, include_in_site_map: true },
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "title", label: "عنوان" },
    { key: "slug", label: "اسلاگ (نشانی برگه)" },
    { key: "is_published", label: "انتشار", render: (r) => badge(!!r.is_published, "منتشرشده", "پیش‌نویس") },
  ],
  fields: [
    { key: "title", label: "عنوان", required: true },
    { key: "slug", label: "اسلاگ (مثلاً about یا privacy)", required: true },
    { key: "content", label: "محتوای برگه", type: "richtext", required: true },
    { key: "meta_title", label: "عنوان متا" },
    { key: "meta_description", label: "توضیحات متا", type: "textarea" },
    { key: "is_published", label: "منتشرشده", type: "checkbox", defaultValue: true },
  ],
};

export const slidesConfig: ResourceConfig = {
  title: "اسلایدهای صفحه اصلی",
  description: "اسلایدهای هیرو/تبلیغاتی در صفحهٔ اصلی.",
  basePath: "slides",
  canDelete: true,
  columns: [
    { key: "image", label: "تصویر", type: "image" },
    { key: "title", label: "عنوان" },
    { key: "slide_type", label: "نوع" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "مخفی") },
  ],
};

export const homeEventsConfig: ResourceConfig = {
  title: "رویدادهای صفحه اصلی",
  description: "بنرهای رویداد تبلیغاتی در بخش هیرو صفحهٔ اصلی.",
  basePath: "home-events",
  listQuery: "includeInactive=true",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "image", label: "تصویر", type: "image" },
    { key: "title", label: "عنوان" },
    { key: "badge", label: "نشان" },
    { key: "sort", label: "ترتیب" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "مخفی") },
  ],
  fields: [
    { key: "title", label: "عنوان", required: true },
    { key: "subtitle", label: "زیرعنوان" },
    { key: "description", label: "توضیحات", type: "textarea" },
    { key: "badge", label: "متن نشان" },
    { key: "image", label: "تصویر", type: "image" },
    { key: "button_text", label: "متن دکمه" },
    { key: "button_url", label: "نشانی دکمه" },
    { key: "color", label: "رنگ متن (hex)" },
    { key: "background_color", label: "رنگ پس‌زمینه (hex)" },
    { key: "sort", label: "ترتیب", type: "number" },
    { key: "is_active", label: "فعال", type: "checkbox" },
  ],
};

export const commentsConfig: ResourceConfig = {
  title: "نظرات",
  description: "مدیریت نظرات کاربران روی مقالات و قالب‌ها.",
  basePath: "comments",
  listQuery: "pageSize=500&page_size=500",
  listKey: "data",
  canCreate: false,
  canEdit: false,
  canDelete: true,
  columns: [
    { key: "author_name", label: "نویسنده" },
    { key: "content", label: "نظر" },
    { key: "is_approved", label: "تأیید", render: (r) => badge(!!r.is_approved, "تأییدشده", "در انتظار") },
  ],
  rowActions: (row, reload) => {
    const id = String(row.id);
    const approve = async (val: boolean) => {
      await fetch(`/api/admin/resource/comments/${id}/approve?approve=${val}`, { method: "PATCH" });
      reload?.();
    };
    return (
      <button
        className="rounded-lg border border-[#262b40] px-3 py-1.5 text-xs text-gray-300 hover:bg-[#161a2e]"
        onClick={() => approve(!row.is_approved)}
      >
        {row.is_approved ? "لغو تأیید" : "تأیید"}
      </button>
    );
  },
};

export const routesConfig: ResourceConfig = {
  title: "مسیرهای داخلی",
  description: "مسیرها / لینک‌های ویژهٔ داخلی (اسلاگ، تصویر، اولویت).",
  basePath: "routes",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "image", label: "تصویر", type: "image" },
    { key: "name", label: "نام" },
    { key: "slug", label: "اسلاگ" },
    { key: "priority", label: "اولویت" },
  ],
  fields: [
    { key: "slug", label: "اسلاگ (مسیر)", required: true },
    { key: "name", label: "نام" },
    { key: "image", label: "تصویر", type: "image" },
    { key: "priority", label: "اولویت", type: "number" },
  ],
};

export const usersConfig: ResourceConfig = {
  title: "کاربران",
  description: "حساب‌های این مجموعه. مسدودسازی یا مدیریت در زیر.",
  basePath: "users",
  listQuery: "pageSize=500&page_size=500",
  listKey: "data",
  columns: [
    { key: "email", label: "ایمیل" },
    { key: "full_name", label: "نام" },
    { key: "is_admin", label: "مدیر", render: (r) => badge(!!r.is_admin, "مدیر", "—") },
    { key: "register_mode", label: "منبع" },
    { key: "ban_account", label: "وضعیت", render: (r) => badge(!r.ban_account, "فعال", "مسدود") },
  ],
  rowActions: (row, reload) => (
    <>
      <UserProfileEdit row={row} reload={reload} />
      <UserActions row={row} reload={reload} />
      {banAction(row, reload)}
    </>
  ),
};

export const plansConfig: ResourceConfig = {
  title: "پلن‌ها",
  description: "پلن‌های اشتراک (نمایش فقط‌خواندنی).",
  basePath: "plans",
  listKey: "data",
  columns: [
    { key: "code", label: "کد" },
    { key: "name", label: "نام" },
    { key: "price_minor", label: "قیمت (ریال)" },
    { key: "billing_period", label: "دوره" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "غیرفعال") },
  ],
};

export const discountsConfig: ResourceConfig = {
  title: "تخفیف‌ها",
  description: "کدهای تخفیف / کوپن. ساخت، ویرایش و حذف.",
  basePath: "discounts",
  listQuery: "pageSize=500&page_size=500",
  listKey: "data",
  canCreate: true,
  canEdit: true,
  canDelete: true,
  columns: [
    { key: "code", label: "کد" },
    { key: "kind", label: "نوع" },
    { key: "value", label: "مقدار" },
    { key: "used_count", label: "استفاده‌شده" },
    { key: "max_use_count", label: "حداکثر استفاده" },
    { key: "is_active", label: "وضعیت", render: (r) => badge(!!r.is_active, "فعال", "غیرفعال") },
    { key: "expires_at", label: "انقضا" },
  ],
  fields: [
    { key: "name", label: "نام", required: true },
    { key: "code", label: "کد", required: true },
    {
      key: "kind", label: "نوع", type: "select", required: true,
      options: [
        { value: "Percentage", label: "درصدی (٪)" },
        { value: "FixedAmount", label: "مبلغ ثابت" },
        { value: "FreeMonths", label: "ماه رایگان" },
        { value: "RenderCredits", label: "اعتبار رندر" },
      ],
      defaultValue: "Percentage",
    },
    { key: "value", label: "مقدار", type: "number", required: true },
    { key: "max_use_count", label: "حداکثر دفعات استفاده (خالی = نامحدود)", type: "number" },
    { key: "expires_at", label: "تاریخ انقضا (ISO، اختیاری)", placeholder: "2026-12-31T00:00:00Z" },
    { key: "is_active", label: "فعال", type: "checkbox", defaultValue: true },
  ],
};
