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

const schema = z.object({
  subject: z.string().min(1),
  direction: z.enum(["inbound", "outbound"]).default("outbound"),
  called_at: z.string().min(1),
  duration_seconds: z.coerce.number().optional(),
  outcome: z.string().optional(),
});

export default createCrmListPage<CallLog>({
  title: "تماس‌ها",
  description: "لاگ تماس‌های فروش.",
  breadcrumb: "تماس‌ها",
  resourceKey: "calls",
  createSchema: schema,
  editSchema: schema.partial(),
  createDefaults: {
    subject: "",
    direction: "outbound",
    called_at: new Date().toISOString(),
    duration_seconds: 0,
    outcome: "",
  },
  fields: [
    { name: "subject", label: "موضوع" },
    {
      name: "direction",
      label: "جهت",
      type: "select",
      options: [
        { value: "inbound", label: "ورودی" },
        { value: "outbound", label: "خروجی" },
      ],
    },
    { name: "called_at", label: "زمان تماس (ISO)" },
    { name: "duration_seconds", label: "مدت (ثانیه)", type: "number" },
    { name: "outcome", label: "نتیجه" },
  ],
  columns: [
    { key: "subject", header: "موضوع" },
    { key: "direction", header: "جهت" },
    { key: "called_at", header: "زمان" },
    { key: "outcome", header: "نتیجه" },
  ],
  list: (tid) => crmApi.calls.list(tid, { page: 1, page_size: 500 }),
  create: (tid, d) => crmApi.calls.create(tid, d),
  exportColumns: ["subject", "direction", "called_at", "outcome"],
});
