import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, ManyToOne, JoinColumn } from 'typeorm';
import { Company } from './company.entity';
import { ProfileType } from './company-profile.entity';

@Entity({ schema: 'freight', name: 'external_profiles' })
@Index(['userId'])
@Index(['companyId'])
export class ExternalProfile extends BaseEntity {
  @Column({ name: 'user_id', type: 'uuid' })
  userId!: string;

  @Column({ name: 'company_id', type: 'uuid' })
  companyId!: string;

  @ManyToOne(() => Company, (company) => company.profiles)
  @JoinColumn({ name: 'company_id' })
  company!: Company;

  @Column({ name: 'first_name', type: 'varchar', length: 100 })
  firstName!: string;

  @Column({ name: 'last_name', type: 'varchar', length: 100 })
  lastName!: string;

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

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

  @Column({ name: 'national_id', type: 'varchar', length: 50, nullable: true })
  nationalId?: string | null;

  @Column({ name: 'job_title', type: 'varchar', length: 100, nullable: true })
  jobTitle?: string | null;

  @Column({ name: 'is_primary_contact', type: 'boolean', default: false })
  isPrimaryContact!: boolean;

  /**
   * The operational profile the user is currently "in" (importer vs exporter,
   * or the single forwarder profile). Drives header switching and scopes the
   * customer's bookings / dashboard to that company_profile. Nullable for
   * users who haven't picked a role yet.
   */
  @Column({
    name: 'active_profile_type',
    type: 'varchar',
    length: 32,
    nullable: true,
    enum: ProfileType,
  })
  activeProfileType?: ProfileType | null;

  /** Coarse resume point for the onboarding wizard (e.g. 'role', 'company', 'documents', 'done'). */
  @Column({
    name: 'onboarding_step',
    type: 'varchar',
    length: 40,
    nullable: true,
  })
  onboardingStep?: string | null;

  @Column({ name: 'onboarding_completed', type: 'boolean', default: false })
  onboardingCompleted!: boolean;
}
