/**
 * User Entity - Authentication and authorization
 *
 * @module @erp-suite/core/entities
 */

import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from './base.entity';
import { Tenant } from './tenant.entity';

/**
 * User entity for authentication and multi-tenancy
 *
 * Users belong to a tenant and have roles for authorization.
 * Stored in auth.users schema for centralized authentication.
 *
 * @example
 * ```typescript
 * const user = new User();
 * user.email = 'user@example.com';
 * user.fullName = 'John Doe';
 * user.status = 'active';
 * ```
 */
@Entity('users', { schema: 'auth' })
export class User extends BaseEntity {
  @Column({ type: 'varchar', length: 255, unique: true })
  email: string;

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

  @Column({ name: 'full_name', type: 'varchar', length: 255 })
  fullName: string;

  @Column({ type: 'varchar', length: 50, default: 'active' })
  status: 'active' | 'inactive' | 'suspended';

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

  @Column({ type: 'jsonb', nullable: true })
  preferences?: Record<string, unknown>;

  @Column({ name: 'avatar_url', type: 'text', nullable: true })
  avatarUrl?: string;

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

  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant?: Tenant;
}
