import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsInt,
  IsEnum,
  IsBoolean,
  IsArray,
  Min,
  Max,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { SipTransport } from '@prisma/client';

export class CreateSipConfigDto {
  @ApiProperty({
    description: 'SIP server hostname or IP',
    example: 'sip.example.com',
  })
  @IsString()
  @IsNotEmpty()
  sipServer: string;

  @ApiPropertyOptional({
    description: 'SIP server port',
    default: 5060,
    minimum: 1,
    maximum: 65535,
  })
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(65535)
  @IsOptional()
  sipPort?: number = 5060;

  @ApiPropertyOptional({
    description: 'SIP transport protocol',
    enum: SipTransport,
    default: SipTransport.TLS,
  })
  @IsEnum(SipTransport)
  @IsOptional()
  transport?: SipTransport = SipTransport.TLS;

  @ApiPropertyOptional({
    description: 'SIP realm for authentication',
    example: 'sip.example.com',
  })
  @IsString()
  @IsOptional()
  realm?: string;

  @ApiPropertyOptional({
    description: 'STUN server address',
    example: 'stun:stun.example.com:3478',
  })
  @IsString()
  @IsOptional()
  stunServer?: string;

  @ApiPropertyOptional({
    description: 'TURN server address',
    example: 'turn:turn.example.com:3478',
  })
  @IsString()
  @IsOptional()
  turnServer?: string;

  @ApiPropertyOptional({
    description: 'TURN server username',
    example: 'turnuser',
  })
  @IsString()
  @IsOptional()
  turnUsername?: string;

  @ApiPropertyOptional({
    description: 'TURN server password',
    example: 'turnpass',
  })
  @IsString()
  @IsOptional()
  turnPassword?: string;

  @ApiProperty({
    description: 'Caller ID displayed to the callee',
    example: '+74951234567',
  })
  @IsString()
  @IsNotEmpty()
  callerId: string;

  @ApiPropertyOptional({
    description: 'Supported audio codecs in priority order',
    default: ['opus', 'g711_alaw', 'g711_ulaw'],
    example: ['opus', 'g711_alaw'],
  })
  @IsArray()
  @IsString({ each: true })
  @IsOptional()
  codecs?: string[] = ['opus', 'g711_alaw', 'g711_ulaw'];
}

export class UpdateSipConfigDto {
  @ApiPropertyOptional({
    description: 'SIP server hostname or IP',
    example: 'sip.example.com',
  })
  @IsString()
  @IsOptional()
  sipServer?: string;

  @ApiPropertyOptional({
    description: 'SIP server port',
    minimum: 1,
    maximum: 65535,
  })
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(65535)
  @IsOptional()
  sipPort?: number;

  @ApiPropertyOptional({
    description: 'SIP transport protocol',
    enum: SipTransport,
  })
  @IsEnum(SipTransport)
  @IsOptional()
  transport?: SipTransport;

  @ApiPropertyOptional({
    description: 'SIP realm for authentication',
    example: 'sip.example.com',
  })
  @IsString()
  @IsOptional()
  realm?: string;

  @ApiPropertyOptional({
    description: 'STUN server address',
    example: 'stun:stun.example.com:3478',
  })
  @IsString()
  @IsOptional()
  stunServer?: string;

  @ApiPropertyOptional({
    description: 'TURN server address',
    example: 'turn:turn.example.com:3478',
  })
  @IsString()
  @IsOptional()
  turnServer?: string;

  @ApiPropertyOptional({ description: 'TURN server username' })
  @IsString()
  @IsOptional()
  turnUsername?: string;

  @ApiPropertyOptional({ description: 'TURN server password' })
  @IsString()
  @IsOptional()
  turnPassword?: string;

  @ApiPropertyOptional({
    description: 'Caller ID displayed to the callee',
    example: '+74951234567',
  })
  @IsString()
  @IsOptional()
  callerId?: string;

  @ApiPropertyOptional({
    description: 'Supported audio codecs in priority order',
    example: ['opus', 'g711_alaw'],
  })
  @IsArray()
  @IsString({ each: true })
  @IsOptional()
  codecs?: string[];

  @ApiPropertyOptional({
    description: 'Whether SIP is active for the club',
  })
  @IsBoolean()
  @IsOptional()
  isActive?: boolean;
}
