import {
  Entity,
  Column,
  ManyToOne,
  JoinColumn,
  Unique,
} from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { RoleEntity } from './role.entity';

/**
 * User entity — tenant-scoped.
 *
 * Per scope doc (User Management):
 * - Fields: Name, Email, Phone, Role, Status, Last Login
 * - Password stored as bcrypt hash
 * - Single role per user (ManyToOne)
 * - Email unique PER TENANT (not globally) — multi-tenant SaaS requirement
 */
@Entity('users')
@Unique(['tenant_id', 'email'])
export class UserEntity extends BaseEntity {
  @Column({ type: 'varchar', length: 255 })
  name: string;

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

  @Column({ type: 'varchar', length: 50, nullable: true })
  phone: string | null;

  @Column({ type: 'varchar', length: 255 })
  password_hash: string;

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

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

  @ManyToOne(() => RoleEntity, { eager: false })
  @JoinColumn({ name: 'role_id' })
  role: RoleEntity;

  @Column({ type: 'uuid', nullable: true })
  role_id: string;
}
