/**
 * Customer-management types for the freight backoffice.
 *
 * These mirror the backend `Company` / `CompanyProfile` entities
 * (apps/edr-freight-api/src/modules/companies/entities) plus a few lightweight
 * shapes for the related data shown on the detail page (bookings / documents /
 * payments). The UI is currently driven by mock data, but the shapes match the
 * API so the data layer can be swapped to live endpoints with no UI changes.
 */

/** Mirrors backend `CompanyType`. */
export type CompanyType =
  | "customer"
  | "freight_forwarder"
  | "dj_freight_forwarder"
  | "transporter";

/** Mirrors backend `CompanyStatus`. */
export type CompanyStatus = "active" | "pending" | "suspended" | "blacklisted";

/** Mirrors backend `ProfileType` (the role a company plays). */
export type ProfileType =
  | "importer"
  | "exporter"
  | "freight_forwarder"
  | "dj_freight_forwarder"
  | "transporter";

/** Mirrors backend `ProfileStatus`. */
export type ProfileStatus = "active" | "pending" | "suspended" | "blacklisted";

/** A single role a company is registered for, with its reference code. */
export interface CompanyProfile {
  id: string;
  companyId: string;
  type: ProfileType;
  reference: string;
  status: ProfileStatus;
  businessLicense?: string | null;
  attributes?: Record<string, unknown> | null;
  createdAt: string;
  updatedAt: string;
}

/** Mirrors backend `Company` (+ its `companyProfiles`). */
export interface Company {
  id: string;
  name: string;
  type: CompanyType;
  status: CompanyStatus;
  tin: string;
  vatNumber?: string | null;
  fanNumber?: string | null;
  country: string;
  address?: string | null;
  phone?: string | null;
  email?: string | null;
  contactPersonName?: string | null;
  contactPersonPhone?: string | null;
  generalManagerName?: string | null;
  generalManagerEmail?: string | null;
  generalManagerPhone?: string | null;
  website?: string | null;
  attributes?: Record<string, unknown> | null;
  companyProfiles: CompanyProfile[];
  createdAt: string;
  updatedAt: string;
}

/** Query parameters for the company list. */
export interface CompanyListFilter {
  page: number;
  pageSize: number;
  search?: string;
  type?: CompanyType;
  status?: CompanyStatus;
}

/** Standard paginated list envelope (matches the bookings service shape). */
export interface PaginatedCompanies {
  items: Company[];
  total: number;
}

/** KPI counts returned by GET /companies/stats. */
export interface CompanyStats {
  total: number;
  active: number;
  pending: number;
  suspended: number;
  blacklisted: number;
}

/* ------------------------------------------------------------------ *
 * Related data shown on the customer detail page (mocked for now).   *
 * ------------------------------------------------------------------ */

export type CustomerBookingStatus =
  | "DRAFT"
  | "SUBMITTED"
  | "PENDING_APPROVAL"
  | "APPROVED"
  | "PAID"
  | "IN_TRANSIT"
  | "COMPLETED"
  | "REJECTED"
  | "CANCELLED";

export interface CustomerBooking {
  id: string;
  reference: string;
  status: CustomerBookingStatus;
  tradeDirection: "IMPORT" | "EXPORT";
  freightType: "CONTAINER" | "BULK";
  originLabel: string;
  destinationLabel: string;
  totalAmount: number;
  currency: "ETB" | "USD";
  scheduledDate?: string | null;
  createdAt: string;
}

export interface CustomerDocument {
  id: string;
  name: string;
  /** File-upload setting code, e.g. "business_license", "contract". */
  code: string;
  mimeType: string;
  /** Size in bytes. */
  size: number;
  uploadedAt: string;
  url?: string | null;
}

export type CustomerPaymentStatus =
  | "action-required"
  | "processing"
  | "success"
  | "failed"
  | "canceled"
  | "refunded";

export type CustomerPaymentMethod =
  | "telebirr"
  | "cbe-birr"
  | "ebirr"
  | "waafi"
  | "card"
  | "dmoney"
  | "cac-bank";

export interface CustomerPayment {
  id: string;
  reference: string;
  /** Booking reference the payment settles. */
  bookingReference: string;
  amount: number;
  currency: "ETB" | "USD";
  method: CustomerPaymentMethod;
  status: CustomerPaymentStatus;
  paidAt?: string | null;
  createdAt: string;
}
