import { ApiProperty } from '@nestjs/swagger';
import { IsDateString, IsEmail, IsInt, IsNotEmpty, IsNumber, IsOptional, IsString, IsUUID, Length, Matches, Max, Min } from 'class-validator';

export class CreateEmpresaDto {
  @ApiProperty({ example: '12345678' })
  @IsNotEmpty({ message: 'RUC debe tener entre 3 y 8 dígitos' })
  @Length(3, 8, { message: 'RUC debe tener entre 3 y 8 dígitos' })
  ruc: string;

  @ApiProperty({ example: '1' })
  @IsNotEmpty({ message: 'DV debe tener exactamente 1 dígito' })
  @Length(1, 1, { message: 'DV debe tener exactamente 1 dígito' })
  dv: string;

  @ApiProperty({ example: 'Empresa S.A.' })
  @IsNotEmpty({ message: 'Razón social debe tener entre 4 y 255 caracteres' })
  @Length(4, 255, { message: 'Razón social debe tener entre 4 y 255 caracteres' })
  razon_social: string;

  @ApiProperty({ example: '' })
  @IsOptional()
  @Length(4, 255, { message: 'Nombre fantasia debe tener entre 4 y 255 caracteres' })
  nombre_fantasia?: string;

  @ApiProperty({ example: '1' })
  @IsNotEmpty({ message: 'La naturaleza es obligatoria' })
  @IsNumber()
  @IsOptional()
  naturaleza?: number;

  @ApiProperty({ example: '1' })
  @IsNotEmpty({ message: 'El tipo contribuyente es obligatorio' })
  @IsNumber()
  tipo_contribuyente: number;

  @ApiProperty({
    example: '595981743120',
    description: 'Formato: 595 + 9 + 8 dígitos (mínimo 12 dígitos en total). Puede incluir + opcional al inicio.',
  })
  @IsNotEmpty({ message: 'Celular es obligatorio' })
  @Matches(/^\+?5959\d{8,}$/, {
    message: 'Celular debe iniciar con 595, seguido de 9, y tener mínimo 12 dígitos en total',
  })
  @Length(12, 20, { message: 'Celular debe tener mínimo 12 y máximo 20 caracteres' })
  celular: string;

  @ApiProperty({ example: 'email@example.com' })
  @IsEmail({}, { message: 'Email debe tener un formato válido' })
  @IsNotEmpty({ message: 'Email debe tener un formato válido' })
  @Length(3, 80, { message: 'Email debe tener entre 3 y 80 caracteres' })
  email: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsOptional()
  @IsUUID('all', { message: 'Plan debe ser un UUID válido' })
  plan_id?: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsOptional()
  @IsUUID('all', { message: 'Estado debe ser un UUID válido' })
  estado_id?: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf', required: false })
  @IsOptional()
  @IsUUID('all', { message: 'Parent ID debe ser un UUID válido' })
  parent_id?: string;

  @ApiProperty({ example: 'standalone', enum: ['holding', 'subsidiary', 'reseller', 'standalone'], required: false })
  @IsOptional()
  type?: string;

  @ApiProperty({ example: 1500000, description: 'Precio acordado para esta empresa (vacío = precio base del plan)', required: false })
  @IsOptional()
  @IsNumber()
  precio_acordado?: number;

  @ApiProperty({ example: '2026-05-01', description: 'Fecha de inicio del plan (ISO date)', required: false })
  @IsOptional()
  @IsDateString()
  fecha_inicio_plan?: string;

  @ApiProperty({ example: 1, description: 'Día del mes para el cobro (1-28)', required: false })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(28)
  dia_cobro?: number;

  @ApiProperty({ example: '2 meses gratis por acuerdo comercial', description: 'Notas del acuerdo comercial', required: false })
  @IsOptional()
  @IsString()
  @Length(0, 255)
  notas_comerciales?: string;
}
