import { ApiProperty, ApiPropertyOptional, PartialType, OmitType } from '@nestjs/swagger';
import { inm_aporte_tipo } from '@prisma/client';
import {
  IsDateString,
  IsEmail,
  IsEnum,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  IsUUID,
  Max,
  MaxLength,
  Min,
} from 'class-validator';

export class CreateInversorDto {
  @ApiProperty() @IsUUID() proyecto_id: string;
  @ApiProperty({ example: 'Inversiones del Este S.A.' })
  @IsString()
  @IsNotEmpty({ message: 'Indicá el nombre del inversor' })
  @MaxLength(150)
  nombre: string;
  @ApiPropertyOptional({ example: '80012345-6' }) @IsOptional() @IsString() @MaxLength(30) documento?: string | null;
  @ApiPropertyOptional() @IsOptional() @IsEmail({}, { message: 'Email inválido' }) @MaxLength(120) email?:
    | string
    | null;
  @ApiPropertyOptional() @IsOptional() @IsString() @MaxLength(40) telefono?: string | null;
  @ApiPropertyOptional({ description: 'Proveedor del ERP al que se le paga (opcional)' })
  @IsOptional()
  @IsUUID()
  proveedor_id?: string | null;
  @ApiProperty({ example: 25, description: 'Participación en el resultado del proyecto, en %' })
  @IsNumber({ maxDecimalPlaces: 4 })
  @Min(0)
  @Max(100, { message: 'La participación no puede pasar del 100 %' })
  participacion_pct: number;
  @ApiPropertyOptional() @IsOptional() @IsString() @MaxLength(1000) notas?: string | null;
}

export class UpdateInversorDto extends PartialType(OmitType(CreateInversorDto, ['proyecto_id'] as const)) {}

export class CreateAporteDto {
  @ApiProperty() @IsUUID() inversor_id: string;
  @ApiProperty({ example: '2026-03-15' }) @IsDateString() fecha: string;
  @ApiProperty({ enum: inm_aporte_tipo }) @IsEnum(inm_aporte_tipo) tipo: inm_aporte_tipo;
  @ApiProperty({ example: 150000, description: 'En la moneda del proyecto (terreno y servicios, valuados)' })
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01, { message: 'El monto tiene que ser mayor a cero' })
  monto: number;
  @ApiPropertyOptional({ example: 'Terreno Finca 1234, Luque' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  descripcion?: string;
  @ApiPropertyOptional({ example: 'Transferencia 00123' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  referencia?: string;
}

export class CreateCanjeDto {
  @ApiProperty() @IsUUID() inversor_id: string;
  @ApiProperty() @IsUUID() unidad_id: string;
  @ApiProperty({ example: '2026-10-11' }) @IsDateString() fecha: string;
  @ApiPropertyOptional({ description: 'Vacío = precio de lista de la unidad' })
  @IsOptional()
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  valor?: number;
}

export class CreateDistribucionDto {
  @ApiProperty() @IsUUID() proyecto_id: string;
  @ApiProperty({ example: '2026-12-31' }) @IsDateString() fecha: string;
  @ApiProperty({ example: 'Utilidad de la etapa 1' })
  @IsString()
  @IsNotEmpty({ message: 'Indicá el concepto' })
  @MaxLength(255)
  concepto: string;
  @ApiProperty({ example: 400000, description: 'Resultado a repartir (cada inversor recibe su %)' })
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01, { message: 'El monto tiene que ser mayor a cero' })
  monto_total: number;
}

export class PagarLineaDto {
  @ApiProperty({ example: '2027-01-05' }) @IsDateString() fecha: string;
  @ApiProperty({ example: 'Transferencia 4455 Banco X' })
  @IsString()
  @IsNotEmpty({ message: 'Indicá la referencia del pago' })
  @MaxLength(100)
  referencia: string;
}

export class MotivoInversoresDto {
  @ApiProperty() @IsString() @IsNotEmpty({ message: 'Indicá el motivo' }) @MaxLength(255) motivo: string;
}
