import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsOptional, IsString, Matches, MaxLength } from 'class-validator';

export class CreateDeviceDto {
  @ApiProperty()
  @IsString()
  @IsNotEmpty()
  @MaxLength(120)
  name: string;

  // Broker username (or future TLS cert CN) if this device connects to MQTT
  // itself; locators relayed by a gateway need only a serial number.
  @ApiPropertyOptional({
    description: 'Broker username, only if this device connects to MQTT itself',
    pattern: '^[a-zA-Z0-9._-]{3,64}$',
  })
  @IsOptional()
  @IsString()
  @Matches(/^[a-zA-Z0-9._-]{3,64}$/, {
    message: 'mqttUsername must be 3-64 chars of letters, digits, dot, dash, underscore',
  })
  mqttUsername?: string;

  @ApiPropertyOptional({ description: 'Locator serial number, globally unique' })
  @IsOptional()
  @IsString()
  @MaxLength(64)
  serialNumber?: string;
}

export class UpdateDeviceDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(120)
  name?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(120)
  serialNumber?: string;

  @ApiPropertyOptional({ description: 'Set false to remotely disable the device' })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  // Only meaningful when disabling (isActive: false); cleared automatically
  // on re-enable regardless of what's passed here.
  @ApiPropertyOptional({ description: 'Only used when isActive: false; cleared automatically on re-enable' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  disabledReason?: string;
}
