/**
 * Document Version DTOs - Data Transfer Objects para Versiones de Documentos
 *
 * Gestiona la creacion y filtrado de versiones de documentos.
 *
 * @module Documents (MAE-016)
 */

import {
  IsString,
  IsUUID,
  IsOptional,
  IsEnum,
  IsNumber,
  IsUrl,
  IsDateString,
  MinLength,
  MaxLength,
  Min,
} from 'class-validator';

/**
 * Estado de la version
 */
export enum VersionStatusEnum {
  CURRENT = 'current',
  SUPERSEDED = 'superseded',
  ARCHIVED = 'archived',
}

/**
 * DTO para crear una nueva version de documento
 */
export class CreateVersionDto {
  @IsUUID()
  documentId!: string;

  @IsString()
  @MinLength(1)
  @MaxLength(20)
  versionNumber!: string;

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

  @IsString()
  @MinLength(1)
  @MaxLength(500)
  fileName!: string;

  @IsString()
  @MinLength(1)
  @MaxLength(20)
  fileExtension!: string;

  @IsNumber()
  @Min(1)
  fileSizeBytes!: number;

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

  @IsOptional()
  @IsString()
  @MaxLength(32)
  checksumMd5?: string;

  @IsOptional()
  @IsString()
  @MaxLength(64)
  checksumSha256?: string;

  @IsOptional()
  @IsString()
  @MaxLength(50)
  storageProvider?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  storageBucket?: string;

  @IsString()
  @MinLength(1)
  @MaxLength(1000)
  storageKey!: string;

  @IsOptional()
  @IsUrl()
  @MaxLength(2000)
  storageUrl?: string;

  @IsOptional()
  @IsUrl()
  @MaxLength(2000)
  thumbnailUrl?: string;

  @IsOptional()
  @IsUrl()
  @MaxLength(2000)
  previewUrl?: string;

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

  @IsOptional()
  @IsString()
  @MaxLength(50)
  changeType?: string;

  @IsOptional()
  @IsNumber()
  @Min(1)
  pageCount?: number;

  @IsOptional()
  @IsString()
  @MaxLength(50)
  uploadSource?: string;
}

/**
 * DTO para filtrar versiones de documentos
 */
export class VersionFiltersDto {
  @IsOptional()
  @IsUUID()
  documentId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(20)
  versionNumber?: string;

  @IsOptional()
  @IsEnum(VersionStatusEnum)
  status?: VersionStatusEnum;

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

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

  @IsOptional()
  @IsDateString()
  createdFrom?: string;

  @IsOptional()
  @IsDateString()
  createdTo?: string;

  @IsOptional()
  @IsNumber()
  @Min(1)
  page?: number;

  @IsOptional()
  @IsNumber()
  @Min(1)
  limit?: number;

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

  @IsOptional()
  @IsEnum(['ASC', 'DESC'])
  sortOrder?: 'ASC' | 'DESC';
}

/**
 * DTO de respuesta para una version de documento
 */
export class VersionResponseDto {
  id!: string;
  tenantId!: string;
  documentId!: string;
  versionNumber!: string;
  versionLabel?: string;
  status!: VersionStatusEnum;
  fileName!: string;
  fileExtension!: string;
  fileSizeBytes!: number;
  mimeType?: string;
  checksumMd5?: string;
  checksumSha256?: string;
  storageProvider!: string;
  storageBucket?: string;
  storageKey!: string;
  storageUrl?: string;
  thumbnailUrl?: string;
  previewUrl?: string;
  isProcessed!: boolean;
  ocrText?: string;
  pageCount?: number;
  changeSummary?: string;
  changeType?: string;
  uploadedById?: string;
  uploadedByName?: string;
  uploadSource?: string;
  createdAt!: Date;
  supersededAt?: Date;
  supersededByVersionId?: string;
}

/**
 * DTO para marcar una version como actual
 */
export class SetCurrentVersionDto {
  @IsOptional()
  @IsString()
  @MaxLength(500)
  reason?: string;
}

/**
 * DTO para archivar una version
 */
export class ArchiveVersionDto {
  @IsOptional()
  @IsString()
  @MaxLength(500)
  reason?: string;
}
