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

export enum CompanyType {
  Customer = "customer",
  FreightForwarder = "freight_forwarder",
  DJFreightForwarder = "dj_freight_forwarder",
  Transporter = "transporter",
}

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

export enum CompanyNationality {
  Ethiopian = "ethiopian",
  Foreign = "foreign",
}

@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: "fan_number", type: "varchar", length: 16, nullable: true })
  fanNumber?: string | null;

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

  /** Whether the company is Ethiopian or Foreign — drives the required onboarding documents. */
  @Column({
    name: "nationality",
    type: "varchar",
    length: 32,
    nullable: true,
    enum: CompanyNationality,
  })
  nationality?: CompanyNationality | null;

  @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;

  @Column({
    name: "licence_number",
    type: "varchar",
    length: 100,
    nullable: true,
  })
  licenceNumber?: string | null;

  @Column({ name: "status_description", type: "text", nullable: true })
  statusDescription?: string | null;

  @Column({
    name: "date_registered",
    type: "varchar",
    length: 50,
    nullable: true,
  })
  dateRegistered?: string | null;

  @Column({
    name: "renewed_from",
    type: "varchar",
    length: 50,
    nullable: true,
  })
  renewedFrom?: string | null;

  @Column({
    name: "renewal_date",
    type: "varchar",
    length: 50,
    nullable: true,
  })
  renewalDate?: string | null;

  @Column({
    name: "renewed_to",
    type: "varchar",
    length: 50,
    nullable: true,
  })
  renewedTo?: string | null;

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

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

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

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

  @Column({ name: "house_no", type: "varchar", length: 100, nullable: true })
  houseNo?: string | null;

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

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

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