import { Role } from '../../generated/prisma/enums.js';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { EmptyStringToUndefined } from '../../common/transforms/empty-string-to-undefined.transform.js';
import {
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  Matches,
  Min,
  MinLength,
} from 'class-validator';

/**
 * 院内账号密码登录 DTO。
 */
export class LoginDto {
  @ApiProperty({ description: '手机号', example: '13800000002' })
  @IsString({ message: 'phone 必须是字符串' })
  @Matches(/^1\d{10}$/, { message: 'phone 必须是合法手机号' })
  phone!: string;

  @ApiProperty({ description: '密码', example: 'Abcd1234' })
  @IsString({ message: 'password 必须是字符串' })
  @MinLength(8, { message: 'password 长度至少 8 位' })
  password!: string;

  @ApiPropertyOptional({
    description: '登录角色；不传时按手机号+密码匹配全部院内账号',
    enum: Role,
    example: Role.DOCTOR,
  })
  @IsOptional()
  @IsEnum(Role, { message: 'role 枚举值不合法' })
  role?: Role;

  @ApiPropertyOptional({
    description: '医院 ID；传入后仅在该医院范围内匹配候选账号',
    example: 1,
  })
  @IsOptional()
  @EmptyStringToUndefined()
  @Type(() => Number)
  @IsInt({ message: 'hospitalId 必须是整数' })
  @Min(1, { message: 'hospitalId 必须大于 0' })
  hospitalId?: number;
}
