import { z } from 'zod';

/**
 * EN: Governance & Compliance DTOs
 * VI: DTOs cho Quản Trị & Tuân Thủ
 */

// =============================================================================
// Compliance Report DTOs
// =============================================================================

export const GenerateComplianceReportDto = z.object({
  type: z.enum(['GDPR', 'SOC2', 'ISO27001', 'HIPAA', 'CUSTOM']),
  name: z.string().min(1, 'Report name is required / Tên báo cáo là bắt buộc').max(255),
  periodStart: z.string().datetime('Invalid start date format / Định dạng ngày bắt đầu không hợp lệ'),
  periodEnd: z.string().datetime('Invalid end date format / Định dạng ngày kết thúc không hợp lệ'),
});

export type GenerateComplianceReportDto = z.infer<typeof GenerateComplianceReportDto>;

export const ReportFiltersDto = z.object({
  type: z.enum(['GDPR', 'SOC2', 'ISO27001', 'HIPAA', 'CUSTOM']).optional(),
  status: z.enum(['DRAFT', 'GENERATED', 'PUBLISHED', 'ARCHIVED']).optional(),
  startDate: z.string().datetime().optional(),
  endDate: z.string().datetime().optional(),
  skip: z.number().int().min(0).default(0),
  take: z.number().int().min(1).max(100).default(20),
});

export type ReportFiltersDto = z.infer<typeof ReportFiltersDto>;

// =============================================================================
// Policy Template DTOs
// =============================================================================

export const CreatePolicyTemplateDto = z.object({
  name: z.string().min(1, 'Template name is required / Tên template là bắt buộc').max(255),
  category: z.enum(['SECURITY', 'ACCESS', 'COMPLIANCE', 'DATA', 'PRIVACY']),
  description: z.string().max(2000).optional(),
  content: z.record(z.string(), z.any()).refine((val) => Object.keys(val).length > 0, {
    message: 'Policy content is required / Nội dung policy là bắt buộc',
  }),
  version: z.string().default('1.0.0'),
});

export type CreatePolicyTemplateDto = z.infer<typeof CreatePolicyTemplateDto>;

export const UpdatePolicyTemplateDto = z.object({
  name: z.string().min(1).max(255).optional(),
  description: z.string().max(2000).optional(),
  content: z.record(z.string(), z.any()).optional(),
  version: z.string().optional(),
  isActive: z.boolean().optional(),
});

export type UpdatePolicyTemplateDto = z.infer<typeof UpdatePolicyTemplateDto>;

export const CreatePolicyFromTemplateDto = z.object({
  templateId: z.string().min(1, 'Template ID is required / Template ID là bắt buộc'),
  name: z.string().min(1, 'Policy name is required / Tên policy là bắt buộc').max(255),
  description: z.string().max(2000).optional(),
  resource: z.string().min(1, 'Resource is required / Resource là bắt buộc'),
  condition: z.record(z.string(), z.any()).refine((val) => Object.keys(val).length > 0, {
    message: 'Condition is required / Điều kiện là bắt buộc',
  }),
  effect: z.enum(['ALLOW', 'DENY']),
  priority: z.number().int().default(0),
  organizationId: z.string().optional(),
});

export type CreatePolicyFromTemplateDto = z.infer<typeof CreatePolicyFromTemplateDto>;

export const TestPolicyDto = z.object({
  testData: z.record(z.string(), z.any()).refine((val) => Object.keys(val).length > 0, {
    message: 'Test data is required / Dữ liệu test là bắt buộc',
  }),
});

export type TestPolicyDto = z.infer<typeof TestPolicyDto>;

// =============================================================================
// Risk Management DTOs
// =============================================================================

export const CalculateRiskScoreDto = z.object({
  userId: z.string().min(1, 'User ID is required / User ID là bắt buộc'),
  factors: z.record(z.string(), z.any()).optional(),
});

export type CalculateRiskScoreDto = z.infer<typeof CalculateRiskScoreDto>;

export const RiskFiltersDto = z.object({
  minScore: z.number().int().min(0).max(100).optional(),
  maxScore: z.number().int().min(0).max(100).optional(),
  level: z.enum(['LOW', 'MEDIUM', 'HIGH', 'CRITICAL']).optional(),
  userId: z.string().optional(),
  skip: z.number().int().min(0).default(0),
  take: z.number().int().min(1).max(100).default(20),
});

export type RiskFiltersDto = z.infer<typeof RiskFiltersDto>;

// =============================================================================
// Reporting DTOs
// =============================================================================

export const AccessSummaryFiltersDto = z.object({
  periodStart: z.string().datetime().optional(),
  periodEnd: z.string().datetime().optional(),
  organizationId: z.string().optional(),
});

export type AccessSummaryFiltersDto = z.infer<typeof AccessSummaryFiltersDto>;

export const UserActivityFiltersDto = z.object({
  userId: z.string().min(1, 'User ID is required / User ID là bắt buộc'),
  periodStart: z.string().datetime().optional(),
  periodEnd: z.string().datetime().optional(),
  eventType: z.string().optional(),
});

export type UserActivityFiltersDto = z.infer<typeof UserActivityFiltersDto>;

export const SecurityEventsFiltersDto = z.object({
  periodStart: z.string().datetime().optional(),
  periodEnd: z.string().datetime().optional(),
  eventType: z.string().optional(),
  success: z.boolean().optional(),
  skip: z.number().int().min(0).default(0),
  take: z.number().int().min(1).max(100).default(20),
});

export type SecurityEventsFiltersDto = z.infer<typeof SecurityEventsFiltersDto>;
