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 RegisterUserDto {
  @ApiProperty({ description: '用户姓名', example: '张三' })
  @IsString({ message: 'name 必须是字符串' })
  name!: string;

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

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

  @ApiProperty({ description: '系统角色', enum: Role, example: Role.DOCTOR })
  @IsEnum(Role, { message: 'role 枚举值不合法' })
  role!: Role;

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

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

  @ApiPropertyOptional({
    description: '系统管理员注册引导密钥（仅注册 SYSTEM_ADMIN 需要）',
    example: 'admin-bootstrap-key',
  })
  @IsOptional()
  @IsString({ message: 'systemAdminBootstrapKey 必须是字符串' })
  systemAdminBootstrapKey?: string;
}
