import { z } from "zod";
import { crmApi } from "@/modules/crm/services/crm-api";
import { createCrmListPage } from "@/modules/crm/components/CrmListCrudPage";
import type { Pipeline } from "@/modules/crm/types";

const schema = z.object({
  name: z.string().min(1),
  code: z.string().min(1),
  description: z.string().optional(),
  is_default: z.boolean().optional(),
});

export default createCrmListPage<Pipeline>({
  title: "قیف‌های فروش",
  description: "تعریف pipeline و مراحل فروش.",
  breadcrumb: "قیف‌ها",
  resourceKey: "pipelines",
  createSchema: schema,
  editSchema: schema.partial(),
  createDefaults: { name: "", code: "", description: "", is_default: false },
  fields: [
    { name: "name", label: "نام" },
    { name: "code", label: "کد", createOnly: true },
    { name: "description", label: "توضیحات", type: "textarea" },
  ],
  columns: [
    { key: "code", header: "کد" },
    { key: "name", header: "نام" },
    { key: "status", header: "وضعیت" },
    {
      key: "is_default",
      header: "پیش‌فرض",
      render: (r) => (r.is_default ? "بله" : "خیر"),
    },
  ],
  list: (tid) => crmApi.pipelines.list(tid, { page: 1, page_size: 500 }),
  create: (tid, d) => crmApi.pipelines.create(tid, d),
  update: (tid, id, d) => crmApi.pipelines.update(tid, id, d),
  exportColumns: ["code", "name", "status"],
});
