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

@Entity('admins')
@Index('idx_admins_tenant', ['tenantId'])
@Index('idx_admins_username_tenant', ['tenantId', 'username'])
export class AdminORM {
  @PrimaryColumn('uuid')
  id: string;

  /**
   * 租户 ID
   * null 表示超级管理员 (可管理所有租户)
   */
  @Column({ name: 'tenant_id', type: 'uuid', nullable: true })
  tenantId: string | null;

  /**
   * 是否为超级管理员
   */
  @Column({ name: 'is_super_admin', type: 'boolean', default: false })
  isSuperAdmin: boolean;

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

  @Column({ name: 'password_hash', length: 255 })
  passwordHash: string;

  @Column({ length: 100 })
  name: string;

  @Column({ length: 255, nullable: true })
  email?: string;

  @Column({ length: 20, nullable: true })
  phone?: string;

  @Column({ length: 20, default: 'OPERATOR' })
  role: string;

  @Column('jsonb', { default: '[]' })
  permissions: string[];

  @Column({ length: 500, nullable: true })
  avatar?: string;

  @Column({ name: 'last_login_at', nullable: true })
  lastLoginAt?: Date;

  @Column({ name: 'last_login_ip', length: 50, nullable: true })
  lastLoginIp?: string;

  @Column({ name: 'is_active', default: true })
  isActive: boolean;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
