import { z } from "zod";
import {
  SUPPORT_CASE_STATUS,
  SUPPORT_CASE_PRIORITY,
  SUPPORT_CASE_CATEGORY,
} from "./contract";

const supportCaseStatusValues = [
  SUPPORT_CASE_STATUS.OPEN,
  SUPPORT_CASE_STATUS.IN_PROGRESS,
  SUPPORT_CASE_STATUS.WAITING_ON_CUSTOMER,
  SUPPORT_CASE_STATUS.RESOLVED,
  SUPPORT_CASE_STATUS.CLOSED,
] as const;

const supportCasePriorityValues = [
  SUPPORT_CASE_PRIORITY.LOW,
  SUPPORT_CASE_PRIORITY.MEDIUM,
  SUPPORT_CASE_PRIORITY.HIGH,
  SUPPORT_CASE_PRIORITY.CRITICAL,
] as const;

const supportCaseCategoryValues = [
  SUPPORT_CASE_CATEGORY.TECHNICAL,
  SUPPORT_CASE_CATEGORY.BILLING,
  SUPPORT_CASE_CATEGORY.GENERAL,
  SUPPORT_CASE_CATEGORY.FEATURE_REQUEST,
] as const;

export const supportCaseStatusSchema = z.enum(supportCaseStatusValues);
export const supportCasePrioritySchema = z.enum(supportCasePriorityValues);
export const supportCaseCategorySchema = z.enum(supportCaseCategoryValues);

export const supportCaseSchema = z.object({
  id: z.number().int().positive(),
  subject: z.string().min(1),
  status: supportCaseStatusSchema,
  priority: supportCasePrioritySchema,
  category: supportCaseCategorySchema,
  createdAt: z.string(),
  updatedAt: z.string(),
  lastReply: z.string().optional(),
  description: z.string(),
  assignedTo: z.string().optional(),
});

export const supportCaseSummarySchema = z.object({
  total: z.number().int().nonnegative(),
  open: z.number().int().nonnegative(),
  highPriority: z.number().int().nonnegative(),
  resolved: z.number().int().nonnegative(),
});

export const supportCaseListSchema = z.object({
  cases: z.array(supportCaseSchema),
  summary: supportCaseSummarySchema,
});

export const supportCaseFilterSchema = z
  .object({
    status: supportCaseStatusSchema.optional(),
    priority: supportCasePrioritySchema.optional(),
    category: supportCaseCategorySchema.optional(),
    search: z.string().trim().min(1).optional(),
  })
  .default({});

export type SupportCaseStatus = z.infer<typeof supportCaseStatusSchema>;
export type SupportCasePriority = z.infer<typeof supportCasePrioritySchema>;
export type SupportCaseCategory = z.infer<typeof supportCaseCategorySchema>;
export type SupportCase = z.infer<typeof supportCaseSchema>;
export type SupportCaseSummary = z.infer<typeof supportCaseSummarySchema>;
export type SupportCaseList = z.infer<typeof supportCaseListSchema>;
export type SupportCaseFilter = z.infer<typeof supportCaseFilterSchema>;
