import { LogisticianDTO as OmsLogisticianDTO } from '@generated/swagger/oms-api';

/**
 * Logistician interface compatible with catalogue-api
 */
export interface CatalogueLogistician {
  id: number;
  [key: string]: any;
}

/**
 * Adapter for converting between oms-api LogisticianDTO and catalogue-api Logistician interface.
 *
 * Handles:
 * - Required vs optional field differences
 * - Field name normalization
 * - Validation of required fields for catalogue operations
 */
export class LogisticianAdapter {
  private static readonly ADAPTER_NAME = 'LogisticianAdapter';

  /**
   * Converts oms-api LogisticianDTO to catalogue-api Logistician format.
   *
   * @param logistician - LogisticianDTO from oms-api
   * @returns Logistician compatible with catalogue-api
   * @throws Error if required fields are missing
   */
  static toCatalogueFormat(
    logistician: OmsLogisticianDTO,
  ): CatalogueLogistician {
    if (!logistician.id) {
      const error = new Error('Logistician missing required id');
      console.error(
        `[${this.ADAPTER_NAME}] Invalid logistician for conversion:`,
        logistician,
      );
      throw error;
    }

    // Catalogue-api expects a simple Logistician interface with id and flexible additional properties
    const catalogueLogistician: CatalogueLogistician = {
      id: logistician.id,
      // Spread all other properties
      ...logistician,
    };

    return catalogueLogistician;
  }

  /**
   * Type guard for oms LogisticianDTO
   */
  static isValidOmsLogistician(value: unknown): value is OmsLogisticianDTO {
    if (typeof value !== 'object' || value === null) return false;

    const logistician = value as OmsLogisticianDTO;
    return typeof logistician.id === 'number';
  }

  /**
   * Type guard for catalogue Logistician
   */
  static isValidCatalogueLogistician(
    value: unknown,
  ): value is CatalogueLogistician {
    if (typeof value !== 'object' || value === null) return false;

    const logistician = value as CatalogueLogistician;
    return typeof logistician.id === 'number';
  }
}
