import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { FxQuote } from '../models/fx-quote.model';
import { FxQuoteGateway } from './fx-quote-gateway.interface';

const QUOTE_TTL_MS = 5 * 60 * 1000;

/**
 * Mock rates, relative to RUB. Dev/demo data only - never presented as a
 * live market rate. This is the one place in the codebase such a table
 * belongs: explicitly the mock data source, only wired in when
 * environment.useMockData is true (see fx-quote-gateway.token.ts).
 *
 * Previously these numbers lived in CurrencyRatesService itself, silently
 * standing in for a real rate with no expiry and no indication they were
 * fake. Moved here so the real service (currency-rates.service.ts) has no
 * fallback data of its own to depend on - it must NOT call back into this
 * gateway for a rate (that would be circular: this gateway would depend on
 * the currency service, which is the whole point of the swap point in
 * fx-quote-gateway.token.ts).
 */
const MOCK_RATES: Record<string, number> = {
  RUB: 1,
  USD: 0.011,
  EUR: 0.01,
  AMD: 4.3,
};

@Injectable({ providedIn: 'root' })
export class FxQuoteLocalGateway implements FxQuoteGateway {
  getQuote(base: string, quote: string): Observable<FxQuote> {
    const baseRate = MOCK_RATES[base] ?? 1;
    const quoteRate = MOCK_RATES[quote] ?? 1;
    const rate = quoteRate / baseRate;
    const now = new Date();

    return of({
      quoteId: `fxq_local_${base}_${quote}_${now.getTime()}`,
      base,
      quote,
      rate,
      source: 'local-mock',
      observedAt: now.toISOString(),
      expiresAt: new Date(now.getTime() + QUOTE_TTL_MS).toISOString(),
    });
  }
}
