import { UserRole } from '@prisma/client';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsEnum, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, MinLength } from 'class-validator';
import { ESTATE_ID_EXAMPLE } from '../../common/swagger-docs';

export class LoginDto {
  @ApiProperty({
    example: '+2348000000001',
    description: 'Registered user phone number. Seeded development users use Nigerian phone numbers.'
  })
  @IsPhoneNumber()
  phone!: string;
}

export class VerifyOtpDto {
  @ApiProperty({ example: '+2348000000001', description: 'Phone number used in the OTP login request.' })
  @IsPhoneNumber()
  phone!: string;

  @ApiProperty({ example: '123456', description: 'OTP delivered by the active provider. DEV mode defaults to OTP_DEV_CODE.' })
  @IsString()
  @IsNotEmpty()
  otp!: string;
}

export class RefreshTokenDto {
  @ApiProperty({
    example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
    description: 'Refresh JWT returned by verify-otp or refresh.'
  })
  @IsString()
  @IsNotEmpty()
  refreshToken!: string;
}

export class SignupDto {
  @ApiProperty({ example: 'Ade Okafor', description: 'Display name for the new user account.' })
  @IsString()
  @MinLength(2)
  name!: string;

  @ApiProperty({ example: '+2348000000100', description: 'Unique phone number for login.' })
  @IsPhoneNumber()
  phone!: string;

  @ApiProperty({
    enum: UserRole,
    example: UserRole.RESIDENT,
    description: 'Role to create. Creator permissions decide which roles are allowed.'
  })
  @IsEnum(UserRole)
  role!: UserRole;

  @ApiPropertyOptional({
    example: ESTATE_ID_EXAMPLE,
    description: 'Required for estate-scoped roles. Omit for SUPER_ADMIN.'
  })
  @IsOptional()
  @IsString()
  estateId?: string;

  @ApiPropertyOptional({ example: 'B14', description: 'Required when role is RESIDENT.' })
  @IsOptional()
  @IsString()
  unit?: string;

  @ApiPropertyOptional({ example: 'Ada', description: 'Optional first name for resident, guard, or admin profiles.' })
  @IsOptional()
  @IsString()
  firstName?: string;

  @ApiPropertyOptional({ example: 'Okafor', description: 'Optional last name for resident, guard, or admin profiles.' })
  @IsOptional()
  @IsString()
  lastName?: string;

  @ApiPropertyOptional({ example: 'ada@example.com', description: 'Optional email address.' })
  @IsOptional()
  @IsString()
  email?: string;

  @ApiPropertyOptional({ example: 'Block A', description: 'Optional resident block number.' })
  @IsOptional()
  @IsString()
  blockNumber?: string;

  @ApiPropertyOptional({ example: 'Flat 12', description: 'Optional resident flat or apartment number.' })
  @IsOptional()
  @IsString()
  flatNumber?: string;

  @ApiPropertyOptional({ example: 'Block A, Flat 12', description: 'Optional resident or guard address.' })
  @IsOptional()
  @IsString()
  address?: string;

  @ApiPropertyOptional({ example: 'gate-uuid', description: 'Optional assigned gate for GUARD users.' })
  @IsOptional()
  @IsString()
  assignedGateId?: string;

  @ApiPropertyOptional({ example: 'guard-device-002', description: 'Optional device assignment for GUARD users.' })
  @IsOptional()
  @IsString()
  deviceId?: string;
}
