import { BookingApprovalStep } from './entities/booking-approval-step.entity';
import { Booking } from './entities/booking.entity';

export interface BookingNextStep {
  action: string;
  description: string;
  requiredRole?: string;
}

export function computeNextStep(
  booking: Pick<Booking, 'status' | 'paymentCurrency'>,
  nextPendingStep?: Pick<BookingApprovalStep, 'requiredRole' | 'stepOrder'> | null,
): BookingNextStep | null {
  const { status } = booking;

  switch (status) {
    case 'PRICE_CHANGED_PENDING_CONFIRM':
      return {
        action: 'CONFIRM_SUBMIT',
        description: 'Price has changed since preview; confirm to submit booking',
      };
    case 'SUBMITTED':
      return {
        action: 'ACCEPT_INTAKE',
        description: 'Line Staff must accept the submission to begin approval',
      };
    case 'PENDING_APPROVAL':
    case 'APPROVED_PENDING_SIGNATURE':
      if (nextPendingStep) {
        return {
          action: 'APPROVE_STEP',
          requiredRole: nextPendingStep.requiredRole,
          description: `${nextPendingStep.requiredRole} must approve step ${nextPendingStep.stepOrder}`,
        };
      }
      return {
        action: 'APPROVE_STEP',
        description: 'Complete the pending approval step in sequence',
      };
    case 'APPROVED':
      return {
        action: 'CUSTOMER_SIGN',
        description: 'Contract generated; customer must sign',
      };
    case 'CONTRACT_READY':
      return {
        action: 'CUSTOMER_SIGN',
        description: 'Customer must sign the contract',
      };
    case 'SIGNED_CUSTOMER':
      return {
        action: 'STAFF_SIGN',
        description: 'Internal staff must counter-sign the contract',
      };
    case 'FULLY_EXECUTED':
      return {
        action: 'AWAIT_PAYMENT',
        description: 'Awaiting customer payment',
      };
    case 'AWAITING_DOCUMENTS':
      return {
        action: 'UPLOAD_DOCUMENTS',
        description: 'Upload the clearance documents for your shipment',
      };
    case 'DOCUMENTS_UNDER_REVIEW':
      return {
        action: 'AWAIT_DOCUMENT_REVIEW',
        description: 'Global Logistics is reviewing your documents',
      };
    case 'CLEARANCE_READY':
      return {
        action: 'PROCEED_TO_OPERATION',
        description:
          'Clearance is ready — pick a schedule day and request operation',
      };
    case 'OPERATION_REQUEST_PENDING':
      return {
        action: 'AWAIT_OPERATION_REVIEW',
        description:
          'Operations is reviewing your request (capacity, documents, route)',
      };
    case 'OPERATION_CHANGES_REQUESTED':
      return {
        action: 'RESUBMIT_OPERATION',
        description:
          'Operations requested changes — update and resubmit your operation request',
      };
    case 'OPERATION_PRICE_PENDING_CONFIRM':
      return {
        action: 'CONFIRM_OPERATION_PRICE',
        description:
          'Operations adjusted the price — confirm the new total to proceed',
      };
    case 'OPERATION_REQUESTED':
      return {
        action: 'AWAIT_OPERATION',
        description: 'Operation requested; an operator will take it forward',
      };
    case 'PAID':
      return {
        action: 'START_TRANSIT',
        description: 'Mark shipment as in transit',
      };
    case 'IN_TRANSIT':
      return {
        action: 'COMPLETE',
        description: 'Mark shipment complete',
      };
    default:
      return null;
  }
}
