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';

/**
 * B 端创建用户 DTO。
 */
export class CreateUserDto {
  @ApiProperty({ description: '用户姓名', example: '李医生' })
  @IsString({ message: 'name 必须是字符串' })
  name!: string;

  @ApiProperty({ description: '手机号', example: '13800000002' })
  @IsString({ message: 'phone 必须是字符串' })
  @Matches(/^1\d{10}$/, { message: 'phone 必须是合法手机号' })
  phone!: string;

  @ApiPropertyOptional({ description: '密码（可选）', example: 'Abcd1234' })
  @IsOptional()
  @IsString({ message: 'password 必须是字符串' })
  @MinLength(8, { message: 'password 长度至少 8 位' })
  password?: string;

  @ApiPropertyOptional({
    description: '微信 openId（院内账号间可复用）',
    example: 'wx-open-id-demo',
  })
  @IsOptional()
  @IsString({ message: 'openId 必须是字符串' })
  openId?: string;

  @ApiProperty({ description: '角色', enum: Role, example: Role.DOCTOR })
  @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;

  @ApiPropertyOptional({ description: '科室 ID', example: 1 })
  @IsOptional()
  @EmptyStringToUndefined()
  @Type(() => Number)
  @IsInt({ message: 'departmentId 必须是整数' })
  @Min(1, { message: 'departmentId 必须大于 0' })
  departmentId?: number;

  @ApiPropertyOptional({ description: '小组 ID', example: 1 })
  @IsOptional()
  @EmptyStringToUndefined()
  @Type(() => Number)
  @IsInt({ message: 'groupId 必须是整数' })
  @Min(1, { message: 'groupId 必须大于 0' })
  groupId?: number;
}
