import {
  Entity, Column, PrimaryGeneratedColumn, CreateDateColumn,
  UpdateDateColumn, VersionColumn, Index,
} from 'typeorm';

export enum UserRole { USER = 'user', ISSUER = 'issuer', MARKET_MAKER = 'market_maker', ADMIN = 'admin' }
export enum UserStatus { ACTIVE = 'active', FROZEN = 'frozen', DELETED = 'deleted' }

@Entity('users')
export class User {
  @PrimaryGeneratedColumn('uuid') id: string;
  @Column({ type: 'varchar', length: 20, unique: true, nullable: true }) phone: string | null;
  @Column({ type: 'varchar', length: 100, unique: true, nullable: true }) email: string | null;
  @Column({ name: 'password_hash', type: 'varchar', length: 255, select: false }) passwordHash: string;
  @Column({ type: 'varchar', length: 50, nullable: true }) nickname: string | null;
  @Column({ name: 'avatar_url', type: 'varchar', length: 500, nullable: true }) avatarUrl: string | null;
  @Column({ name: 'kyc_level', type: 'smallint', default: 0 }) kycLevel: number;
  @Column({ name: 'wallet_mode', type: 'varchar', length: 10, default: 'standard' }) walletMode: string;
  @Index('idx_users_role') @Column({ type: 'varchar', length: 20, default: 'user' }) role: UserRole;
  @Index('idx_users_status') @Column({ type: 'varchar', length: 20, default: 'active' }) status: UserStatus;
  @Column({ name: 'residence_state', type: 'varchar', length: 5, nullable: true }) residenceState: string | null;
  @Column({ type: 'varchar', length: 5, nullable: true }) nationality: string | null;
  @Column({ name: 'preferred_language', type: 'varchar', length: 10, nullable: true, default: null }) preferredLanguage: string | null;
  @Column({ name: 'preferred_currency', type: 'varchar', length: 10, nullable: true, default: null }) preferredCurrency: string | null;
  @Column({ name: 'notification_prefs', type: 'jsonb', nullable: true, default: null }) notificationPrefs: { trade: boolean; expiry: boolean; marketing: boolean } | null;
  @Column({ name: 'last_login_at', type: 'timestamptz', nullable: true }) lastLoginAt: Date | null;
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' }) createdAt: Date;
  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' }) updatedAt: Date;
  @VersionColumn({ default: 1 }) version: number;
}
