import { z } from 'zod';

export const CaseAuditSourceSchema = z.enum([
  'slash_command',
  'button_confirmation',
  'escalation',
  'job',
  'automod'
]);

export type CaseAuditSource = z.infer<typeof CaseAuditSourceSchema>;

export const PurgeAuditFiltersSchema = z.object({
  targetUserId: z.string().nullable().optional(),
  botsOnly: z.boolean().optional(),
  linksOnly: z.boolean().optional(),
  attachmentsOnly: z.boolean().optional(),
  regex: z.string().nullable().optional()
});

export const CaseAuditMetadataSchema = z
  .object({
    source: CaseAuditSourceSchema,
    recordedAt: z.string().datetime(),
    channelId: z.string().optional(),
    confirmed: z.boolean().optional(),
    escalation: z.boolean().optional(),
    warningCount: z.number().int().positive().optional(),
    durationMs: z.number().int().positive().optional(),
    deletedCaseId: z.string().optional(),
    deletedCaseNumber: z.number().int().positive().optional(),
    purge: z
      .object({
        requestedAmount: z.number().int().positive(),
        deletedCount: z.number().int().nonnegative(),
        filters: PurgeAuditFiltersSchema.optional()
      })
      .optional()
  })
  .passthrough();

export type CaseAuditMetadata = z.infer<typeof CaseAuditMetadataSchema>;

type BuildCaseMetadataInput = {
  source: CaseAuditSource;
  channelId?: string;
  extras?: Record<string, unknown>;
};

export function buildCaseMetadata(input: BuildCaseMetadataInput): CaseAuditMetadata {
  return CaseAuditMetadataSchema.parse({
    source: input.source,
    recordedAt: new Date().toISOString(),
    channelId: input.channelId,
    ...input.extras
  });
}
