import { Entity, PrimaryGeneratedColumn, Column, OneToMany, CreateDateColumn } from 'typeorm';
import { License } from './license.entity';
import { TeamMember } from './team-member.entity';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 255, unique: true })
  email: string;

  @Column({ type: 'varchar', length: 50, unique: true })
  username: string;

  @Column({ type: 'text' })
  password_hash: string;

  @Column({ type: 'text', nullable: true })
  totp_secret: string | null;

  @Column({ type: 'boolean', default: false })
  totp_enabled: boolean;

  @Column('text', { array: true, nullable: true })
  backup_codes: string[] | null;

  @Column({ type: 'boolean', default: false })
  email_verified: boolean;

  @Column({ type: 'boolean', default: false })
  is_super_admin: boolean;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  created_at: Date;

  @Column({ type: 'timestamptz', nullable: true })
  last_login_at: Date | null;

  @OneToMany(() => License, license => license.owner)
  licenses: License[];

  @OneToMany(() => TeamMember, teamMember => teamMember.user)
  team_memberships: TeamMember[];
}
