import { z } from 'zod';

export const SupportCaseStatusSchema = z.enum([
  'open',
  'triaged',
  'in_progress',
  'waiting_customer',
  'resolved',
  'closed',
  'escalated',
]);
export const SupportCasePrioritySchema = z.enum(['critical', 'high', 'medium', 'low']);
export const SupportCaseSourceSchema = z.enum(['manual', 'agent', 'telemetry', 'customer']);
export const SupportNoteVisibilitySchema = z.enum(['internal', 'customer']);
export const SupportAuthorTypeSchema = z.enum(['user', 'agent', 'system']);

export const SupportCaseSchema = z.object({
  id: z.string().min(1),
  productId: z.string().min(1),
  orgId: z.string().optional(),
  workspaceId: z.string().optional(),
  requesterUserId: z.string().optional(),
  assignedTo: z.string().optional(),
  title: z.string().min(1),
  description: z.string().optional(),
  status: SupportCaseStatusSchema,
  priority: SupportCasePrioritySchema,
  source: SupportCaseSourceSchema,
  runId: z.string().optional(),
  reviewId: z.string().optional(),
  knowledgeBaseId: z.string().optional(),
  tags: z.array(z.string()).default([]),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export type SupportCaseDoc = z.infer<typeof SupportCaseSchema> & {
  _ts?: number;
  _etag?: string;
};

export const SupportCaseNoteSchema = z.object({
  id: z.string().min(1),
  caseId: z.string().min(1),
  productId: z.string().min(1),
  authorType: SupportAuthorTypeSchema,
  authorId: z.string().min(1),
  visibility: SupportNoteVisibilitySchema,
  body: z.string().min(1),
  createdAt: z.string(),
});

export type SupportCaseNoteDoc = z.infer<typeof SupportCaseNoteSchema> & {
  _ts?: number;
  _etag?: string;
};

export const SupportEscalationEventSchema = z.object({
  id: z.string().min(1),
  caseId: z.string().min(1),
  productId: z.string().min(1),
  escalatedTo: z.string().min(1),
  reason: z.string().min(1),
  triggeredBy: z.string().min(1),
  createdAt: z.string(),
});

export type SupportEscalationEventDoc = z.infer<typeof SupportEscalationEventSchema> & {
  _ts?: number;
  _etag?: string;
};

export const CreateSupportCaseSchema = z.object({
  orgId: z.string().optional(),
  workspaceId: z.string().optional(),
  requesterUserId: z.string().optional(),
  assignedTo: z.string().optional(),
  title: z.string().min(1),
  description: z.string().optional(),
  priority: SupportCasePrioritySchema.default('medium'),
  source: SupportCaseSourceSchema.default('manual'),
  runId: z.string().optional(),
  reviewId: z.string().optional(),
  knowledgeBaseId: z.string().optional(),
  tags: z.array(z.string()).default([]),
});

export const UpdateSupportCaseSchema = z.object({
  assignedTo: z.string().optional(),
  title: z.string().min(1).optional(),
  description: z.string().optional(),
  status: SupportCaseStatusSchema.optional(),
  priority: SupportCasePrioritySchema.optional(),
  knowledgeBaseId: z.string().optional(),
  tags: z.array(z.string()).optional(),
});

export const CreateSupportNoteSchema = z.object({
  authorType: SupportAuthorTypeSchema.default('user'),
  authorId: z.string().min(1),
  visibility: SupportNoteVisibilitySchema.default('internal'),
  body: z.string().min(1),
});

export const CreateSupportEscalationSchema = z.object({
  escalatedTo: z.string().min(1),
  reason: z.string().min(1),
});

export const ListSupportCasesQuerySchema = z.object({
  status: SupportCaseStatusSchema.optional(),
  priority: SupportCasePrioritySchema.optional(),
  source: SupportCaseSourceSchema.optional(),
  assignedTo: z.string().optional(),
  limit: z.coerce.number().min(1).max(100).default(20),
});

export type ListSupportCasesQuery = z.infer<typeof ListSupportCasesQuerySchema>;
