import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, CreateDateColumn, UpdateDateColumn, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
import { Role, RoleSchema } from './role.entity';
import { z } from '@hono/zod-openapi';
import { DeleteStatus, DisabledStatus } from '@/share/types';
import { UserType } from './user.enum';
import { File } from '../files/file.entity';


@Entity({ name: 'users' })
export class UserEntity {
  @PrimaryGeneratedColumn({ unsigned: true, comment: '用户ID' })
  id!: number;

  @Column({ name: 'username', type: 'varchar', length: 255, unique: true, comment: '用户名' })
  username!: string;

  @Column({ name: 'password', type: 'varchar', length: 255, comment: '密码' })
  password!: string;

  @Column({ name: 'phone', type: 'varchar', length: 255, nullable: true, comment: '手机号' })
  phone!: string | null;

  @Column({ name: 'email', type: 'varchar', length: 255, nullable: true, comment: '邮箱' })
  email!: string | null;

  @Column({ name: 'nickname', type: 'varchar', length: 255, nullable: true, comment: '昵称' })
  nickname!: string | null;

  @Column({ name: 'name', type: 'varchar', length: 255, nullable: true, comment: '真实姓名' })
  name!: string | null;


  @Column({ name: 'avatar_file_id', type: 'int', unsigned: true, nullable: true, comment: '头像文件ID' })
  avatarFileId!: number | null;

  @ManyToOne(() => File, { nullable: true })
  @JoinColumn({ name: 'avatar_file_id', referencedColumnName: 'id' })
  avatarFile!: File | null;


  @Column({ name: 'is_disabled', type: 'int', default: DisabledStatus.ENABLED, comment: '是否禁用(0:启用,1:禁用)' })
  isDisabled!: DisabledStatus;

  @Column({ name: 'is_deleted', type: 'int', default: DeleteStatus.NOT_DELETED, comment: '是否删除(0:未删除,1:已删除)' })
  isDeleted!: DeleteStatus;

  @Column({
    name: 'user_type',
    type: 'enum',
    enum: UserType,
    default: UserType.STUDENT,
    comment: '用户类型(teacher:老师,student:学生,trainee:学员)'
  })
  userType!: UserType;

  @ManyToMany(() => Role)
  @JoinTable()
  roles!: Role[];

  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt!: Date;

  @Column({ name: 'wechat_openid', type: 'varchar', length: 255, nullable: true, comment: '微信开放平台openid' })
  wechatOpenid!: string | null;

  @Column({ name: 'wechat_unionid', type: 'varchar', length: 255, nullable: true, comment: '微信开放平台unionid' })
  wechatUnionid!: string | null;

  @Column({ name: 'wechat_nickname', type: 'varchar', length: 255, nullable: true, comment: '微信昵称' })
  wechatNickname!: string | null;

  @Column({ name: 'wechat_avatar', type: 'varchar', length: 500, nullable: true, comment: '微信头像URL' })
  wechatAvatar!: string | null;

  @Column({ name: 'wechat_sex', type: 'tinyint', nullable: true, comment: '微信性别(1:男,2:女,0:未知)' })
  wechatSex!: number | null;

  @Column({ name: 'wechat_province', type: 'varchar', length: 100, nullable: true, comment: '微信省份' })
  wechatProvince!: string | null;

  @Column({ name: 'wechat_city', type: 'varchar', length: 100, nullable: true, comment: '微信城市' })
  wechatCity!: string | null;

  @Column({ name: 'wechat_country', type: 'varchar', length: 100, nullable: true, comment: '微信国家' })
  wechatCountry!: string | null;

  @Column({ name: 'trainee_valid_from', type: 'timestamp', nullable: true, comment: '学员有效期开始时间' })
  traineeValidFrom!: Date | null;

  @Column({ name: 'trainee_valid_to', type: 'timestamp', nullable: true, comment: '学员有效期结束时间' })
  traineeValidTo!: Date | null;

  constructor(partial?: Partial<UserEntity>) {
    Object.assign(this, partial);
  }
}
