import {
  IsString,
  IsOptional,
  IsBoolean,
  IsNumber,
  IsArray,
  IsObject,
  IsUUID,
  IsEnum,
  IsIP,
  MaxLength,
  MinLength,
} from 'class-validator';

// ============================================
// AUDIT LOG DTOs
// ============================================

export class CreateAuditLogDto {
  @IsOptional()
  @IsUUID()
  userId?: string;

  @IsString()
  @MaxLength(20)
  action: string;

  @IsOptional()
  @IsString()
  @MaxLength(30)
  category?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  entityType?: string;

  @IsOptional()
  @IsUUID()
  entityId?: string;

  @IsOptional()
  @IsString()
  description?: string;

  @IsOptional()
  @IsObject()
  oldValues?: Record<string, any>;

  @IsOptional()
  @IsObject()
  newValues?: Record<string, any>;

  @IsOptional()
  @IsObject()
  metadata?: Record<string, any>;

  @IsOptional()
  @IsString()
  @MaxLength(45)
  ipAddress?: string;

  @IsOptional()
  @IsString()
  @MaxLength(500)
  userAgent?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  requestId?: string;
}

// ============================================
// ENTITY CHANGE DTOs
// ============================================

export class CreateEntityChangeDto {
  @IsString()
  @MaxLength(100)
  entityType: string;

  @IsUUID()
  entityId: string;

  @IsString()
  @MaxLength(20)
  changeType: string;

  @IsOptional()
  @IsUUID()
  changedBy?: string;

  @IsNumber()
  version: number;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  changedFields?: string[];

  @IsOptional()
  @IsObject()
  previousData?: Record<string, any>;

  @IsOptional()
  @IsObject()
  newData?: Record<string, any>;

  @IsOptional()
  @IsString()
  changeReason?: string;
}

// ============================================
// LOGIN HISTORY DTOs
// ============================================

export class CreateLoginHistoryDto {
  @IsUUID()
  userId: string;

  @IsOptional()
  @IsUUID()
  tenantId?: string;

  @IsString()
  @MaxLength(20)
  status: string;

  @IsOptional()
  @IsString()
  @MaxLength(30)
  authMethod?: string;

  @IsOptional()
  @IsString()
  @MaxLength(30)
  mfaMethod?: string;

  @IsOptional()
  @IsBoolean()
  mfaUsed?: boolean;

  @IsOptional()
  @IsString()
  @MaxLength(45)
  ipAddress?: string;

  @IsOptional()
  @IsString()
  @MaxLength(500)
  userAgent?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  deviceFingerprint?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  location?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  sessionId?: string;

  @IsOptional()
  @IsString()
  failureReason?: string;
}

// ============================================
// SENSITIVE DATA ACCESS DTOs
// ============================================

export class CreateSensitiveDataAccessDto {
  @IsUUID()
  userId: string;

  @IsString()
  @MaxLength(50)
  dataType: string;

  @IsString()
  @MaxLength(20)
  accessType: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  entityType?: string;

  @IsOptional()
  @IsUUID()
  entityId?: string;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  fieldsAccessed?: string[];

  @IsOptional()
  @IsString()
  accessReason?: string;

  @IsOptional()
  @IsBoolean()
  wasExported?: boolean;

  @IsOptional()
  @IsString()
  @MaxLength(45)
  ipAddress?: string;
}

// ============================================
// DATA EXPORT DTOs
// ============================================

export class CreateDataExportDto {
  @IsString()
  @MaxLength(30)
  exportType: string;

  @IsString()
  @MaxLength(20)
  format: string;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  entities?: string[];

  @IsOptional()
  @IsObject()
  filters?: Record<string, any>;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  fields?: string[];

  @IsOptional()
  @IsString()
  exportReason?: string;
}

export class UpdateDataExportStatusDto {
  @IsString()
  @MaxLength(20)
  status: string;

  @IsOptional()
  @IsString()
  filePath?: string;

  @IsOptional()
  @IsNumber()
  fileSize?: number;

  @IsOptional()
  @IsNumber()
  recordCount?: number;

  @IsOptional()
  @IsString()
  errorMessage?: string;
}

// ============================================
// PERMISSION CHANGE DTOs
// ============================================

export class CreatePermissionChangeDto {
  @IsUUID()
  targetUserId: string;

  @IsUUID()
  changedBy: string;

  @IsString()
  @MaxLength(20)
  changeType: string;

  @IsString()
  @MaxLength(30)
  scope: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  resourceType?: string;

  @IsOptional()
  @IsUUID()
  resourceId?: string;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  previousPermissions?: string[];

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  newPermissions?: string[];

  @IsOptional()
  @IsString()
  changeReason?: string;
}

// ============================================
// CONFIG CHANGE DTOs
// ============================================

export class CreateConfigChangeDto {
  @IsString()
  @MaxLength(30)
  configType: string;

  @IsString()
  @MaxLength(200)
  configKey: string;

  @IsUUID()
  changedBy: string;

  @IsNumber()
  version: number;

  @IsOptional()
  @IsObject()
  previousValue?: Record<string, any>;

  @IsOptional()
  @IsObject()
  newValue?: Record<string, any>;

  @IsOptional()
  @IsString()
  changeReason?: string;
}
