import { Injectable } from '@angular/core';
import { ResponseArgs } from '@isa/common/data-access';
import { LoyaltyBonResponse } from '@generated/swagger/crm-api';

/**
 * Mock implementation of CustomerBonRedemptionFacade for UI testing without backend.
 *
 * Test Bon numbers:
 * - "123456789" - Valid Bon with data
 * - "987654321" - Valid Bon with different data
 * - "111111111" - Valid Bon with high total
 * - Any other number - Returns "Keine verpunktung möglich"
 *
 * @example
 * // In component for testing, provide the mock:
 * providers: [
 *   { provide: CustomerBonRedemptionFacade, useClass: CustomerBonRedemptionFacadeMock }
 * ]
 */
@Injectable()
export class CustomerBonRedemptionFacadeMock {
  /**
   * Mock Bon data for testing
   */
  private readonly mockBonData: Record<string, LoyaltyBonResponse> = {
    '123456789': {
      date: '23.05.2025',
      total: 76.12,
    },
    '987654321': {
      date: '15.04.2025',
      total: 42.5,
    },
    '111111111': {
      date: '01.01.2025',
      total: 150.0,
    },
  };

  /**
   * Simulate API delay (300ms)
   */
  private async simulateDelay(): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, 300));
  }

  /**
   * Mock check Bon implementation
   */
  async checkBon(params: {
    cardCode: string;
    bonNr: string;
    storeId?: string;
  }): Promise<ResponseArgs<LoyaltyBonResponse>> {
    await this.simulateDelay();

    const bonData = this.mockBonData[params.bonNr];

    if (bonData) {
      return {
        result: bonData,
        error: false,
        message: 'Bon gefunden',
      };
    }

    // Return empty result for unknown Bon numbers (triggers "Keine verpunktung möglich")
    return {
      result: undefined,
      error: false,
      message: 'Keine verpunktung möglich',
    };
  }

  /**
   * Mock add Bon implementation
   */
  async addBon(_params: {
    cardCode: string;
    bonNr: string;
    storeId?: string;
  }): Promise<boolean> {
    await this.simulateDelay();

    // Always succeed for mock
    return true;
  }
}
