/**
 * BidDocument DTOs - Data Transfer Objects para Documentos de Licitacion
 *
 * Almacena documentos asociados a una licitacion.
 *
 * @module Bidding (MAI-018)
 */

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

/**
 * Tipo/categoria de documento
 */
export enum BidDocumentType {
  BASES = 'bases',
  TECHNICAL_ANNEX = 'technical_annex',
  ECONOMIC_ANNEX = 'economic_annex',
  CLARIFICATION = 'clarification',
  PROPOSAL_TECH = 'proposal_tech',
  PROPOSAL_ECON = 'proposal_econ',
  CONTRACT = 'contract',
  OTHER = 'other',
}

/**
 * DTO para crear un nuevo documento
 */
export class CreateBidDocumentDto {
  @IsUUID()
  tenderId: string;

  @IsEnum(BidDocumentType)
  documentType: BidDocumentType;

  @IsString()
  @MinLength(3)
  @MaxLength(255)
  name: string;

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

  @IsString()
  @MaxLength(500)
  fileUrl: string;

  @IsNumber()
  @Min(1)
  fileSize: number;

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

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

  @IsOptional()
  @IsDateString()
  uploadedAt?: string;
}

/**
 * DTO para actualizar un documento existente
 */
export class UpdateBidDocumentDto {
  @IsOptional()
  @IsEnum(BidDocumentType)
  documentType?: BidDocumentType;

  @IsOptional()
  @IsString()
  @MinLength(3)
  @MaxLength(255)
  name?: string;

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

  @IsOptional()
  @IsNumber()
  @Min(1)
  version?: number;
}

/**
 * DTO para subir nueva version de documento
 */
export class UploadNewVersionDto {
  @IsString()
  @MaxLength(500)
  fileUrl: string;

  @IsNumber()
  @Min(1)
  fileSize: number;

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

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

/**
 * DTO para filtrar documentos
 */
export class BidDocumentFiltersDto {
  @IsOptional()
  @IsUUID()
  tenderId?: string;

  @IsOptional()
  @IsEnum(BidDocumentType)
  documentType?: BidDocumentType;

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

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

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

  @IsOptional()
  @IsString()
  search?: 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 un documento
 */
export class BidDocumentResponseDto {
  id: string;
  tenantId: string;
  tenderId: string;
  tender?: {
    id: string;
    number: string;
    title: string;
  };
  documentType: BidDocumentType;
  name: string;
  description?: string;
  fileUrl: string;
  fileSize: string;
  mimeType: string;
  version: number;
  uploadedById: string;
  uploadedBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  uploadedAt: Date;
  createdAt: Date;
  createdById?: string;
  updatedAt: Date;
  updatedById?: string;
}

/**
 * DTO para resumen de documentos por tipo
 */
export class DocumentSummaryByTypeDto {
  documentType: BidDocumentType;
  count: number;
  totalSize: number;
  latestVersion: number;
}
