import { z } from 'zod';

export const backupStatusSchema = z.enum([
  'PENDING',
  'RUNNING',
  'AVAILABLE',
  'FAILED',
  'DELETED',
]);

export const backupTypeSchema = z.enum([
  'MANUAL',
  'SCHEDULED',
  'PRE_RESTORE',
  'PRE_WORLD_REPLACE',
]);

export const backupRestoreStatusSchema = z.enum([
  'PENDING',
  'RUNNING',
  'COMPLETED',
  'FAILED',
]);

export const backupResponseSchema = z.object({
  id: z.string().uuid(),
  serverId: z.string().uuid(),
  type: backupTypeSchema,
  status: backupStatusSchema,
  s3Key: z.string().nullable(),
  sizeBytes: z.string().nullable(),
  sha256: z.string().nullable(),
  label: z.string().nullable(),
  failureReason: z.string().nullable(),
  createdAt: z.string().datetime(),
  completedAt: z.string().datetime().nullable(),
  expiresAt: z.string().datetime().nullable(),
});

export const backupListResponseSchema = z.object({
  backups: z.array(backupResponseSchema),
});

export const createBackupRequestSchema = z.object({
  label: z.string().max(128).optional(),
});

export const backupActionResponseSchema = z.object({
  backup: backupResponseSchema,
  jobId: z.string().optional(),
});

export const backupDownloadUrlResponseSchema = z.object({
  downloadUrl: z.string().url(),
  expiresAt: z.string().datetime(),
});

export const restoreBackupRequestSchema = z.object({
  confirm: z.literal(true, {
    errorMap: () => ({ message: 'Restore must be explicitly confirmed' }),
  }),
});

export const backupRestoreResponseSchema = z.object({
  restore: z.object({
    id: z.string().uuid(),
    serverId: z.string().uuid(),
    backupId: z.string().uuid(),
    safetyBackupId: z.string().uuid().nullable(),
    status: backupRestoreStatusSchema,
    failureReason: z.string().nullable(),
    createdAt: z.string().datetime(),
    completedAt: z.string().datetime().nullable(),
  }),
  jobId: z.string().optional(),
});

export const backupScheduleSchema = z.object({
  enabled: z.boolean(),
  intervalHours: z.number().int().min(1).max(168),
  retentionCount: z.number().int().min(1).max(50),
  nextRunAt: z.string().datetime().nullable(),
  lastRunAt: z.string().datetime().nullable(),
});

export const updateBackupScheduleSchema = z.object({
  enabled: z.boolean().optional(),
  intervalHours: z.number().int().min(1).max(168).optional(),
  retentionCount: z.number().int().min(1).max(50).optional(),
});

export type BackupStatus = z.infer<typeof backupStatusSchema>;
export type BackupType = z.infer<typeof backupTypeSchema>;
export type BackupResponse = z.infer<typeof backupResponseSchema>;
export type BackupListResponse = z.infer<typeof backupListResponseSchema>;
export type CreateBackupRequest = z.infer<typeof createBackupRequestSchema>;
export type BackupActionResponse = z.infer<typeof backupActionResponseSchema>;
export type BackupDownloadUrlResponse = z.infer<typeof backupDownloadUrlResponseSchema>;
export type RestoreBackupRequest = z.infer<typeof restoreBackupRequestSchema>;
export type BackupRestoreResponse = z.infer<typeof backupRestoreResponseSchema>;
export type BackupSchedule = z.infer<typeof backupScheduleSchema>;
export type UpdateBackupSchedule = z.infer<typeof updateBackupScheduleSchema>;
