import { ApiProperty, ApiPropertyOptional, OmitType, PartialType } from '@nestjs/swagger';
import { gym_sala_tipo } from '@prisma/client';
import { IsEnum, IsInt, IsNotEmpty, IsObject, IsOptional, IsString, IsUUID, MaxLength, Min, ValidateIf } from 'class-validator';

export class CreateSedeGymDto {
  @ApiProperty({ example: 'Sede Centro' })
  @IsString()
  @IsNotEmpty({ message: 'El nombre de la sede es obligatorio' })
  @MaxLength(120)
  nombre: string;

  @ApiProperty({ description: 'Sucursal del ERP (timbrado y punto de expedición de la sede).' })
  @IsUUID('all', { message: 'Elegí la sucursal de la sede' })
  sucursal_id: string;

  @ApiPropertyOptional({ nullable: true, description: 'Depósito de donde salen los productos que vende la sede.' })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsUUID()
  deposito_id?: string | null;

  @ApiPropertyOptional({ nullable: true, description: 'Caja de recepción.' })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsUUID()
  caja_id?: string | null;

  @ApiPropertyOptional({ nullable: true })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsString()
  @MaxLength(255)
  direccion?: string | null;

  @ApiPropertyOptional({ nullable: true })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsString()
  @MaxLength(30)
  telefono?: string | null;

  @ApiPropertyOptional({
    nullable: true,
    example: { lun: { abre: '06:00', cierra: '22:00' }, dom: null },
    description: 'Horario por día (lun…dom); null = cerrado.',
  })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsObject()
  horario_apertura?: Record<string, { abre: string; cierra: string } | null> | null;

  @ApiPropertyOptional({ nullable: true, example: 120 })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsInt()
  @Min(1, { message: 'La capacidad tiene que ser mayor que 0' })
  capacidad_maxima?: number | null;
}

export class UpdateSedeGymDto extends PartialType(CreateSedeGymDto) {}

export class CreateSalaGymDto {
  @ApiProperty({ example: 'Cabina 1' })
  @IsString()
  @IsNotEmpty({ message: 'El nombre es obligatorio' })
  @MaxLength(80)
  nombre: string;

  @ApiPropertyOptional({ enum: gym_sala_tipo, default: gym_sala_tipo.SALA })
  @IsOptional()
  @IsEnum(gym_sala_tipo, { message: 'Tipo de sala inválido' })
  tipo?: gym_sala_tipo;

  @ApiPropertyOptional({ nullable: true, example: 12 })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsInt()
  @Min(1, { message: 'La capacidad tiene que ser mayor que 0' })
  capacidad?: number | null;
}

export class UpdateSalaGymDto extends PartialType(OmitType(CreateSalaGymDto, [] as const)) {}
