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

@Entity('users') // table name is 'users'
export class UserEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Index()
  @Column('varchar', { length: 50, nullable: false })
  username: string;

  // @Index({ unique: true }) — UNIQUE enforced at DB level via UQ_users_email index
  // Nullable: OTP users start without an email (entered during onboarding)
  @Column('varchar', { length: 100, nullable: true })
  email: string | null;

  // OLD: @Column('varchar', { length: 255, nullable: false, select: false })
  // NEW: Changed to nullable for OTP users
  @Column('varchar', { length: 255, nullable: true, select: false })
  password: string | null;

  @Column('enum', {
    enum: [
      'seeker',
      'recruiter',
      'admin',
      'franchise',
      'operation',
      'trainer',
      'associate',
    ],
    default: 'seeker',
    nullable: false,
  })
  role:
    | 'seeker'
    | 'recruiter'
    | 'admin'
    | 'franchise'
    | 'operation'
    | 'trainer'
    | 'associate';

  @Column('varchar', { length: 150, nullable: false, name: 'full_name' })
  fullName: string;

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

  // @Column('varchar', { length: 150, nullable: true })
  // designation: string | null;

  @Column('varchar', { length: 20, nullable: true })
  secondary_phone: string | null;

  @Column('varchar', { length: 20, nullable: true })
  whatsapp_number: string | null;

  @Column('varchar', { length: 255, nullable: true, name: 'profile_picture' })
  profilePicture: string | null;

  @Column('enum', {
    enum: ['entry_level', 'mid_level', 'professional'],
    nullable: true,
    name: 'seeker_type',
  })
  seekerType: 'entry_level' | 'mid_level' | 'professional' | null;

  @Column('enum', {
    enum: ['active', 'inactive', 'banned'],
    nullable: true,
    default: 'active',
  })
  status: 'active' | 'inactive' | 'banned' | null;

  @Column('timestamp', { name: 'deleted_at', nullable: true })
  deletedAt: Date | null;

  // ═══════════════════════════════════════════════════════════
  // NEW FIELDS FOR OTP AUTHENTICATION
  // ═══════════════════════════════════════════════════════════

  @Column('boolean', { default: false, name: 'phone_verified' })
  phoneVerified: boolean;

  @Column('enum', {
    enum: ['password', 'otp'],
    default: 'otp',
    name: 'auth_method',
  })
  authMethod: 'password' | 'otp';

  // ═══════════════════════════════════════════════════════════
  // ONBOARDING FIELDS
  // ═══════════════════════════════════════════════════════════

  @Column('enum', {
    enum: ['not_started', 'in_progress', 'completed'],
    nullable: true,
    name: 'onboarding_status',
  })
  onboardingStatus: 'not_started' | 'in_progress' | 'completed' | null;

  @Column('int', { nullable: true, name: 'onboarding_step' })
  onboardingStep: number | null;

  @Column('enum', {
    enum: ['white_collar', 'blue_collar'],
    nullable: true,
    name: 'selected_role_category',
  })
  selectedRoleCategory: 'white_collar' | 'blue_collar' | null;

  @Column('int', { nullable: true, name: 'mobile_onboarding_step' })
  mobileOnboardingStep: number | null;

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

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