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

const schema = z.object({
  title: z.string().min(1),
  total_amount: z.string().default("0"),
  currency_code: z.string().default("IRR"),
  status: z.enum(["draft", "sent", "accepted", "rejected", "expired"]).default("draft"),
  notes: z.string().optional(),
});

export default createCrmListPage<Quote>({
  title: "پیش‌فاکتورها",
  description: "Quote management — بدون حذف در API فعلی.",
  breadcrumb: "پیش‌فاکتورها",
  resourceKey: "quotes",
  createSchema: schema,
  editSchema: schema.partial(),
  createDefaults: {
    title: "",
    total_amount: "0",
    currency_code: "IRR",
    status: "draft",
    notes: "",
  },
  fields: [
    { name: "title", label: "عنوان" },
    { name: "total_amount", label: "مبلغ کل" },
    { name: "currency_code", label: "ارز" },
    {
      name: "status",
      label: "وضعیت",
      type: "select",
      options: [
        { value: "draft", label: "پیش‌نویس" },
        { value: "sent", label: "ارسال‌شده" },
        { value: "accepted", label: "پذیرفته" },
        { value: "rejected", label: "رد شده" },
        { value: "expired", label: "منقضی" },
      ],
    },
    { name: "notes", label: "یادداشت", type: "textarea" },
  ],
  columns: [
    { key: "quote_number", header: "شماره" },
    { key: "title", header: "عنوان" },
    { key: "total_amount", header: "مبلغ" },
    { key: "status", header: "وضعیت" },
  ],
  list: (tid) => crmApi.quotes.list(tid, { page: 1, page_size: 500 }),
  create: (tid, d) => crmApi.quotes.create(tid, d),
  update: (tid, id, d) => crmApi.quotes.update(tid, id, d),
  exportColumns: ["quote_number", "title", "total_amount", "status"],
});
