import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import {
  IsBoolean,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString,
  IsUUID,
  Matches,
  Max,
  MaxLength,
  Min,
  ValidateIf,
} from 'class-validator';

const HORA = /^([01]\d|2[0-3]):[0-5]\d$/;
const DIA = /^\d{4}-\d{2}-\d{2}$/;

export class CreateHorarioGymDto {
  @ApiProperty()
  @IsUUID('all', { message: 'Elegí la sede' })
  sede_id: string;

  @ApiProperty()
  @IsUUID('all', { message: 'Elegí la actividad' })
  actividad_id: string;

  @ApiPropertyOptional({ nullable: true })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsUUID()
  sala_id?: string | null;

  @ApiPropertyOptional({ nullable: true })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @IsUUID()
  instructor_id?: string | null;

  @ApiProperty({ minimum: 1, maximum: 7, description: '1 = lunes … 7 = domingo' })
  @IsInt({ message: 'Elegí el día' })
  @Min(1)
  @Max(7)
  dia_semana: number;

  @ApiProperty({ example: '18:30' })
  @Matches(HORA, { message: 'La hora va como HH:MM (ej. 18:30)' })
  hora_inicio: string;

  @ApiPropertyOptional({ example: 60, description: 'Por defecto, la duración de la actividad' })
  @IsOptional()
  @IsInt()
  @Min(5, { message: 'La clase dura al menos 5 minutos' })
  @Max(600)
  duracion_min?: number;

  @ApiPropertyOptional({ example: 12, description: 'Por defecto, la capacidad de la sala' })
  @IsOptional()
  @IsInt()
  @Min(1, { message: 'El cupo es al menos 1' })
  @Max(500)
  cupo?: number;

  @ApiPropertyOptional({ example: '2026-11-05', description: 'Por defecto, hoy' })
  @IsOptional()
  @Matches(DIA, { message: 'Fecha inválida' })
  vigente_desde?: string;

  @ApiPropertyOptional({ nullable: true, example: '2026-12-20' })
  @IsOptional()
  @ValidateIf((_, v) => v !== null)
  @Matches(DIA, { message: 'Fecha inválida' })
  vigente_hasta?: string | null;
}

export class UpdateHorarioGymDto extends PartialType(CreateHorarioGymDto) {}

export class CancelarSesionDto {
  @ApiProperty({ example: 'La instructora está enferma' })
  @IsString()
  @IsNotEmpty({ message: 'El motivo es obligatorio' })
  @MaxLength(500)
  motivo: string;
}

export class ReservarDto {
  @ApiProperty()
  @IsUUID('all', { message: 'Elegí el socio' })
  socio_id: string;
}

export class AsistenciaDto {
  @ApiProperty()
  @IsBoolean()
  asistio: boolean;
}
