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

const schema = z.object({
  subject: z.string().min(1),
  activity_type: z.enum(["call", "email", "meeting", "task", "note", "other"]).default("task"),
  priority: z.enum(["low", "medium", "high", "urgent"]).default("medium"),
  description: z.string().optional(),
});

export default createCrmListPage<SalesActivity>({
  title: "فعالیت‌های فروش",
  description: "ثبت و پیگیری فعالیت‌های مرتبط با CRM entities.",
  breadcrumb: "فعالیت‌ها",
  resourceKey: "activities",
  createSchema: schema,
  editSchema: schema.partial(),
  createDefaults: {
    subject: "",
    activity_type: "task",
    priority: "medium",
    description: "",
  },
  fields: [
    { name: "subject", label: "موضوع" },
    {
      name: "activity_type",
      label: "نوع",
      type: "select",
      options: [
        { value: "call", label: "تماس" },
        { value: "email", label: "ایمیل" },
        { value: "meeting", label: "جلسه" },
        { value: "task", label: "وظیفه" },
        { value: "note", label: "یادداشت" },
        { value: "other", label: "سایر" },
      ],
    },
    {
      name: "priority",
      label: "اولویت",
      type: "select",
      options: [
        { value: "low", label: "کم" },
        { value: "medium", label: "متوسط" },
        { value: "high", label: "بالا" },
        { value: "urgent", label: "فوری" },
      ],
    },
    { name: "description", label: "توضیحات", type: "textarea" },
  ],
  columns: [
    { key: "subject", header: "موضوع" },
    { key: "activity_type", header: "نوع" },
    {
      key: "status",
      header: "وضعیت",
      render: (r) => <CrmStatusChip status={r.status} domain="generic" />,
    },
    {
      key: "priority",
      header: "اولویت",
      render: (r) => <CrmStatusChip status={r.priority} domain="priority" />,
    },
  ],
  list: (tid) => crmApi.activities.list(tid, { page: 1, page_size: 500 }),
  create: (tid, d) => crmApi.activities.create(tid, d),
  update: (tid, id, d) => crmApi.activities.update(tid, id, d),
  exportColumns: ["subject", "activity_type", "status", "priority"],
});
