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

export enum ProfileType {
  importer = "importer",
  exporter = "exporter",
  freightForwarder = "freight_forwarder",
  djFreightForwarder = "dj_freight_forwarder",
  transporter = "transporter",
}

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

/** A business-license document stored directly on the company profile. */
export interface BusinessLicenseFile {
  name: string;
  url: string;
  size: number;
  mimeType?: string;
}

@Entity({ schema: "freight", name: "company_profiles" })
@Index(["reference"], { unique: true })
@Index(["type"])
@Index(["companyId"])
export class CompanyProfile extends BaseEntity {
  @Column({ name: "company_id", type: "uuid" })
  companyId!: string;

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

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

  @Column({
    name: "reference",
    type: "varchar",
    length: 20,
    nullable: false,
    unique: true,
  })
  reference!: string;

  @Column({
    name: "status",
    type: "varchar",
    length: 32,
    default: ProfileStatus.Active,
  })
  status!: ProfileStatus;

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

  /**
   * Business-license documents for this profile, stored directly on the profile
   * (multi-file). The bytes live in object storage; only the metadata/URLs are
   * persisted here — this is intentionally NOT modelled via the FileRecord table.
   */
  @Column({ name: "business_license_files", type: "jsonb", nullable: true })
  businessLicenseFiles?: BusinessLicenseFile[] | null;

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