import type { BaseEntity } from "../common";

export * from "./file_upload_settings";
export * from "./dropdown_settings";

export enum TradeDirection {
  IMPORT = 'IMPORT',
  EXPORT = 'EXPORT',
  BOTH   = 'BOTH',
}

export enum PriorityType {
  USD_PAYER            = 'USD_PAYER',
  RAIL_AND_FORWARDING  = 'RAIL_AND_FORWARDING',
  GOVERNMENT_ACCOUNT   = 'GOVERNMENT_ACCOUNT',
  HIGH_VOLUME_SHIPMENT = 'HIGH_VOLUME_SHIPMENT',
}

export enum ExceededAction {
  WARNING_ONLY = 'WARNING_ONLY',
  HARD_BLOCK   = 'HARD_BLOCK',
}

export enum CalculationMethod {
  PER_TON    = 'PER_TON',
  FLAT_FEE   = 'FLAT_FEE',
  PERCENTAGE = 'PERCENTAGE',
}

export enum FreightType {
  Container = 'CONTAINER',
  Bulk = 'BULK',
}

export enum BookingStatus {
  Draft = "DRAFT",
  Submitted = "SUBMITTED",
  ChangesRequested = "CHANGES_REQUESTED",
  PendingApproval = "PENDING_APPROVAL",
  ApprovedPendingSignature = "APPROVED_PENDING_SIGNATURE",
  Approved = "APPROVED",
  ContractReady = "CONTRACT_READY",
  SignedCustomer = "SIGNED_CUSTOMER",
  FullyExecuted = "FULLY_EXECUTED",
  PnrGenerated = "PNR_GENERATED",
  PaymentVerificationInProgress = "PAYMENT_VERIFICATION_IN_PROGRESS",
  Paid = "PAID",
  InTransit = "IN_TRANSIT",
  Completed = "COMPLETED",
  Delivered = "DELIVERED",
  Rejected = "REJECTED",
  Cancelled = "CANCELLED",
  PendingConsolidation = "PENDING_CONSOLIDATION",
  Consolidated = "CONSOLIDATED",
}

export enum ConsignmentStatus {
  Pending = "PENDING",
  Loaded = "LOADED",
  InTransit = "IN_TRANSIT",
  AtDestination = "AT_DESTINATION",
  Delivered = "DELIVERED",
  Returned = "RETURNED",
}

export enum TrainStatus {
  Available = "AVAILABLE",
  Scheduled = "SCHEDULED",
  InService = "IN_SERVICE",
  UnderMaintenance = "UNDER_MAINTENANCE",
  OutOfService = "OUT_OF_SERVICE",
}

export enum CargoType {
  Container = "CONTAINER",
  BulkLiquid = "BULK_LIQUID",
  BulkDry = "BULK_DRY",
  General = "GENERAL",
  Refrigerated = "REFRIGERATED",
  Hazardous = "HAZARDOUS",
}

export enum PaymentStatus {
  Pending = "PENDING",
  Paid = "PAID",
  Overdue = "OVERDUE",
  Cancelled = "CANCELLED",
  Refunded = "REFUNDED",
}

export type CustomerStatus = "Active" | "Pending" | "Inactive";
export type CustomerType = "Importer" | "Exporter" | "Supplier";

export interface ICustomer extends BaseEntity {
  userId: string;
  firstName: string;
  lastName: string;
  email: string;
  phone: string;
  companyName: string;
  companyEmail: string;
  companyPhone: string;
  companyLocation: string;
  companyAddress: string;
  contactPersonName: string;
  contactPersonPhone: string;
  tinNumber: string;
  vatNumber?: string | null;
  fanNumber: string;
  generalManagerName: string;
  generalManagerEmail: string;
  generalManagerPhone: string;
  poaName?: string | null;
  poaPhone?: string | null;
  poaAddress?: string | null;
  poaEmail?: string | null;
  poaLocation?: string | null;
  notes?: string | null;
}

export interface CreateCustomerDto {
  userId: string;
  firstName: string;
  lastName: string;
  email: string;
  phone: string;
  companyName: string;
  companyEmail: string;
  companyPhone: string;
  companyLocation: string;
  companyAddress: string;
  contactPersonName: string;
  contactPersonPhone: string;
  tinNumber: string;
  vatNumber: string;
  fanNumber: string;
  generalManagerName: string;
  generalManagerEmail: string;
  generalManagerPhone: string;
  poaName?: string;
  poaPhone?: string;
  poaAddress?: string;
  poaEmail?: string;
  poaLocation?: string;
  notes?: string;
}

export type UpdateCustomerDto = Partial<CreateCustomerDto>;

export interface ITrain extends BaseEntity {
  code: string;
  capacityTons: number;
  status: TrainStatus;
  notes?: string;
}

export interface ITrackingEvent extends BaseEntity {
  consignmentId: string;
  location: string;
  status: ConsignmentStatus;
  occurredAt: string;
  description?: string;
}

export interface IConsignment extends BaseEntity {
  bookingId: string;
  trackingNumber: string;
  cargoType: CargoType;
  weightKg: number;
  status: ConsignmentStatus;
  originStation: string;
  destinationStation: string;
}

export interface IYard extends BaseEntity {
  code: string;
  label: string;
  country: string;
  isActive: boolean;
  displayOrder: number;
}

export interface IBooking extends BaseEntity {
  reference: string;
  customerId: string;
  trainId?: string | null;
  status: BookingStatus;
  scheduledDate: string;
  totalAmount: number;
  paymentStatus: PaymentStatus;

  contractType: "NEW" | "RENEWAL";
  previousContractId?: string | null;
  serviceType: "RAIL_ONLY" | "RAIL_AND_FORWARDING";

  firstMileEnabled: boolean;
  firstMilePickupAddress?: string | null;
  lastMileEnabled: boolean;
  lastMileDeliveryAddress?: string | null;

  equipmentReturn: "WITH_RETURN" | "WITHOUT_RETURN";
  originYard?: IYard | null;
  destinationYard?: IYard | null;
  cargoTotalWeightVgm: number;

  freightType: FreightType;
  freightSubtype?: string | null;

  isHazardous: boolean;
  isRefrigerated: boolean;

  tradeDirection: "IMPORT" | "EXPORT";
  paymentCurrency: string;
  allowConsolidation: boolean;
  consolidationPartnerId?: string | null;

  startDate?: string | null;
  endDate?: string | null;
  financialTerms?: string | null;

  containers?: Array<{ type: string; qty: number; vgm: number }> | null;

  versionNumber: number;
  priorityScore: number;

  approvedByStaffId?: string | null;
  approvedByStaffAt?: string | null;
  signedByDirectorId?: string | null;
  signedByDirectorAt?: string | null;
  signedByCeoId?: string | null;
  signedByCeoAt?: string | null;

  files?: Array<{
    id: string;
    code: string;
    name: string;
    url: string;
    mimeType: string;
    size: number;
    resourceId: string;
    resource: string;
    signedUrl?: string | null;
  }>;

  latestChangeRequestNote?: string | null;
  contractSummary?: string | null;
  pricingBreakdown?: PricingBreakdown | null;
}

export interface PricingBreakdownLineItem {
  code: string;
  amount: number;
  currency: string;
  description: string;
}

export interface PricingBreakdown {
  currency: string;
  lineItems: PricingBreakdownLineItem[];
  generatedAt: string;
  totalAmount: number;
}

export interface IInvoice extends BaseEntity {
  bookingId: string;
  invoiceNumber: string;
  amount: number;
  currency: string;
  status: PaymentStatus;
  issuedAt: string;
  dueAt: string;
}

// ── Reference Data (booking form catalog) ──────────────────────────────────────

export interface BookingReferenceYard {
  id: string;
  name: string;
  code: string;
  country: string;
}

export interface BookingReferenceContainerType {
  id: string;
  name: string;
  code: string;
  is_reefer: boolean;
  wagons_per_unit: number;
}

export interface BookingReferenceContainerSizeGroup {
  size: string;
  types: BookingReferenceContainerType[];
}

export interface BookingReferenceService {
  id: string;
  name: string;
  code: string;
}

export interface BookingReferenceShippingLine {
  id: string;
  name: string;
  code: string;
}

export interface BookingReferenceCargoTypeChild {
  id: string;
  name: string;
  code: string;
  show_free_text_box: boolean;
}

export interface BookingReferenceCargoTypeGroup {
  id: string;
  name: string;
  code: string;
  children?: BookingReferenceCargoTypeChild[];
}

export interface BookingReferenceData {
  yard: BookingReferenceYard[];
  containers: BookingReferenceContainerSizeGroup[];
  service: BookingReferenceService[];
  shipping_line: BookingReferenceShippingLine[];
  cargo_type: BookingReferenceCargoTypeGroup[];
}

// ── DTOs ───────────────────────────────────────────────────────────────────────

export interface CreateBookingContainerDto {
  containerTypeId: string;
  quantity: number;
  vgmPerUnitTons: number;
}

export interface CreateBookingDto {
  reference?: string;
  customerId?: string;
  companyId?: string;
  trainId?: string;
  scheduledDate: string;
  contractType: "NEW" | "RENEWAL";
  previousContractId?: string;
  serviceTypeId: string;
  firstMilePickupAddress?: string;
  lastMileDeliveryAddress?: string;
  equipmentReturn: "WITH_RETURN" | "WITHOUT_RETURN" | "NA";
  originYardId: string;
  destinationYardId: string;
  tradeDirection: "IMPORT" | "EXPORT" | "DOMESTIC";
  freightType: FreightType;
  cargoTypeId?: string;
  cargoFreeText?: string;
  shippingLineId?: string;
  cargoTotalWeightVgm: number;
  isHazardous?: boolean;
  paymentCurrency: "ETB" | "USD";
  pnrCode?: string;
  startDate?: string;
  endDate?: string;
  financialTerms?: string;
  containers?: CreateBookingContainerDto[];
  allowConsolidation?: boolean;
}
