import { Optional } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import {
  IsArray,
  IsDateString,
  IsEmail,
  IsInt,
  IsOptional,
  IsString,
  ValidateIf,
} from 'class-validator';

export class CreateTrainingDto {
  // === 1. DATOS BÁSICOS ===
  @ApiProperty()
  @IsString()
  coorFullName: string;

  @ApiProperty()
  @IsDateString()
  visitDate: string; // Llega como string ISO "2024-11-11T10:00"

  @ApiProperty()
  @IsString()
  @IsOptional()
  coorPhone?: string;

  // === 2. DATOS OSP ===
  @ApiProperty()
  @IsOptional()
  ospName: string;

  @ApiProperty()
  @IsOptional()
  ospRif: string;

  @ApiProperty()
  @IsString()
  ospType: string; // 'UPF', etc.

  @ApiProperty()
  @IsString()
  productiveActivity: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  productiveActivityOther: string;

  @ApiProperty()
  @IsString()
  currentStatus: string;

  @ApiProperty()
  @IsInt()
  @Type(() => Number) // Convierte "2017" -> 2017
  companyConstitutionYear: number;

  @ApiProperty()
  @IsString()
  @IsOptional()
  ospAddress: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  ospGoogleMapsLink?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  infrastructureMt2?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  structureType?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  hasTransport?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  isOpenSpace?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  paralysisReason?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  generalObservations?: string;

  // === 3. SECTORES ===
  @ApiProperty()
  @IsString()
  ecoSector: string;

  @ApiProperty()
  @IsString()
  productiveSector: string;

  @ApiProperty()
  @IsString()
  centralProductiveActivity: string;

  @ApiProperty()
  @IsString()
  mainProductiveActivity: string;

  // === 4. DATOS RESPONSABLE ===
  @ApiProperty()
  @IsString()
  ospResponsibleFullname: string;

  @ApiProperty()
  @IsString()
  ospResponsibleCedula: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  ospResponsibleRif: string;

  @ApiProperty()
  @IsString()
  ospResponsiblePhone: string;

  @ApiProperty()
  @IsOptional()
  @ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
  @IsEmail()
  ospResponsibleEmail?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  civilState: string;

  @ApiProperty()
  @IsInt()
  @IsOptional()
  @Type(() => Number) // Convierte "3" -> 3
  familyBurden: number;

  @ApiProperty()
  @IsInt()
  @IsOptional()
  @Type(() => Number)
  numberOfChildren: number;

  // === 5. COMUNA Y CONSEJO COMUNAL ===
  @ApiProperty()
  @IsString()
  siturCodeCommune: string;

  @ApiProperty()
  @IsString()
  communeName: string;

  @ApiProperty()
  @IsString()
  communeRif: string;

  @ApiProperty()
  @IsString()
  communeSpokespersonName: string;

  @ApiProperty()
  @IsString()
  communeSpokespersonPhone: string;

  @ApiProperty()
  @IsOptional()
  @ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
  @IsEmail()
  communeEmail?: string;

  @ApiProperty()
  @IsString()
  communalCouncil: string;

  @ApiProperty()
  @IsString()
  siturCodeCommunalCouncil: string;

  @ApiProperty()
  @IsString()
  communalCouncilRif: string;

  @ApiProperty()
  @IsString()
  communalCouncilSpokespersonName: string;

  @ApiProperty()
  @IsString()
  communalCouncilSpokespersonPhone: string;

  @ApiProperty()
  @IsOptional()
  @ValidateIf((o, v) => v !== '' && v !== null && v !== undefined)
  @IsEmail()
  communalCouncilEmail?: string;

  // === 6. DISTRIBUCIÓN Y EXPORTACIÓN ===
  @ApiProperty()
  @IsString()
  @IsOptional()
  internalDistributionZone?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  isExporting?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  externalCountry?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  externalCity?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  externalDescription?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  externalQuantity?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  externalUnit?: string;

  // === 7. MANO DE OBRA ===
  @ApiProperty()
  @IsInt()
  @IsOptional()
  @Type(() => Number)
  womenCount?: number;

  @ApiProperty()
  @IsInt()
  @IsOptional()
  @Type(() => Number)
  menCount?: number;

  // === 8. LISTAS (Arrays JSON) ===
  // Reciben un string JSON '[{...}]' y lo convierten a Objeto JS real

  @ApiProperty()
  @IsOptional()
  @IsArray()
  @Transform(({ value }) => {
    if (typeof value === 'string') {
      try {
        return JSON.parse(value);
      } catch {
        return [];
      }
    }
    return value;
  })
  equipmentList?: any[];

  @ApiProperty()
  @IsOptional()
  @IsArray()
  @Transform(({ value }) => {
    if (typeof value === 'string') {
      try {
        return JSON.parse(value);
      } catch {
        return [];
      }
    }
    return value;
  })
  productionList?: any[];

  @ApiProperty()
  @IsOptional()
  @IsArray()
  @Transform(({ value }) => {
    if (typeof value === 'string') {
      try {
        return JSON.parse(value);
      } catch {
        return [];
      }
    }
    return value;
  })
  productList?: any[];

  //ubicacion
  @ApiProperty()
  @IsString()
  state: string;

  @ApiProperty()
  @IsString()
  municipality: string;

  @ApiProperty()
  @IsString()
  parish: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  photo1?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  photo2?: string;

  @ApiProperty()
  @IsString()
  @IsOptional()
  photo3?: string;
}
