import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, OneToMany } from 'typeorm';
import { ExternalProfile } from './external-profile.entity';

export enum CompanyType {
  Customer = 'customer',
  Forwarder = 'forwarder',
  Transporter = 'transporter',
  Broker = 'broker',
}

export enum CompanyStatus {
  Active = 'active',
  Pending = 'pending',
  Suspended = 'suspended',
  Blacklisted = 'blacklisted',
}

@Entity({ schema: 'freight', name: 'companies' })
@Index(['tin'])
@Index(['type'])
export class Company extends BaseEntity {
  @Column({ name: 'name', type: 'varchar', length: 200 })
  name!: string;

  @Column({ name: 'type', type: 'varchar', length: 32, enum: CompanyType })
  type!: CompanyType;

  @Column({ name: 'status', type: 'varchar', length: 32, default: CompanyStatus.Pending })
  status!: CompanyStatus;

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

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

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

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

  @Column({ name: 'country', type: 'varchar', length: 32, default: 'Ethiopia' })
  country!: string;

  @Column({ name: 'address', type: 'text', nullable: true })
  address?: string | null;

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

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

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

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

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

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

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

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

  @Column({ name: 'attributes', type: 'jsonb', nullable: true })
  attributes?: Record<string, any> | null;

  @OneToMany(() => ExternalProfile, (profile) => profile.company)
  profiles?: ExternalProfile[];
}
