import type { Page } from '@playwright/test';

/** City title helper — the real API returns multilingual titles */
function t(ru: string, en: string) {
  return { ru, en, de: en, es: en, fr: en, it: en, ja: en, ko: en, zh: en };
}

const MOCK_CITIES = [
  { code: 'MOW', title: t('Москва', 'Moscow'), country_code: 'RU', location: { lat: 55.75, lon: 37.62 } },
  { code: 'LED', title: t('Санкт-Петербург', 'Saint Petersburg'), country_code: 'RU', location: { lat: 59.93, lon: 30.32 } },
  { code: 'AER', title: t('Сочи', 'Sochi'), country_code: 'RU', location: { lat: 43.45, lon: 39.96 } },
  { code: 'KRR', title: t('Краснодар', 'Krasnodar'), country_code: 'RU', location: { lat: 45.03, lon: 38.97 } },
  { code: 'OVB', title: t('Новосибирск', 'Novosibirsk'), country_code: 'RU', location: { lat: 55.01, lon: 82.93 } },
  { code: 'SVX', title: t('Екатеринбург', 'Ekaterinburg'), country_code: 'RU', location: { lat: 56.85, lon: 60.61 } },
  { code: 'KUF', title: t('Самара', 'Samara'), country_code: 'RU', location: { lat: 53.20, lon: 50.15 } },
  { code: 'KZN', title: t('Казань', 'Kazan'), country_code: 'RU', location: { lat: 55.80, lon: 49.11 } },
  { code: 'UFA', title: t('Уфа', 'Ufa'), country_code: 'RU', location: { lat: 54.74, lon: 55.97 } },
  { code: 'ROV', title: t('Ростов-на-Дону', 'Rostov-on-Don'), country_code: 'RU', location: { lat: 47.24, lon: 39.71 } },
  { code: 'VVO', title: t('Владивосток', 'Vladivostok'), country_code: 'RU', location: { lat: 43.12, lon: 131.89 } },
  { code: 'VKO', title: t('Внуково', 'Vnukovo'), country_code: 'RU', location: { lat: 55.60, lon: 37.27 } },
];

const MOCK_AIRPORTS = [
  { code: 'SVO', city_code: 'MOW', title: t('Шереметьево', 'Sheremetyevo'), has_afl_flights: true, location: { lat: 55.97, lon: 37.41 } },
  { code: 'DME', city_code: 'MOW', title: t('Домодедово', 'Domodedovo'), has_afl_flights: true, location: { lat: 55.41, lon: 37.91 } },
  { code: 'VKO', city_code: 'MOW', title: t('Внуково', 'Vnukovo'), has_afl_flights: true, location: { lat: 55.60, lon: 37.27 } },
  { code: 'LED', city_code: 'LED', title: t('Пулково', 'Pulkovo'), has_afl_flights: true, location: { lat: 59.80, lon: 30.27 } },
  { code: 'AER', city_code: 'AER', title: t('Адлер', 'Adler'), has_afl_flights: true, location: { lat: 43.45, lon: 39.96 } },
];

export async function mockAngularAPIs(page: Page): Promise<void> {
  await page.route('**/api/appSettings', (route) => {
    route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({
        showDebugVersion: 'False',
        uiOptions: {
          filter: {
            onlineboard: { searchFrom: '2d', searchTo: '14d' },
            schedule: { searchFrom: '30d', searchTo: '30d' },
          },
          buttons: {
            flightStatus: { availableFrom: '24h' },
            buyTicket: { period: { min: '2h', max: '72h' } },
          },
        },
      }),
    });
  });

  await page.route('**/api/Requests/*/getpopular', (route) => {
    route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify([
        { requestType: 'FlightNumber', carrierCode: 'SU', flightNumber: '0654' },
        { requestType: 'Route', departureCity: 'LED', arrivalCity: 'KRR' },
        { requestType: 'Route', departureCity: 'VKO', arrivalCity: 'KUF' },
        { requestType: 'Arrival', arrivalCity: 'VKO' },
      ]),
    });
  });

  await page.route('**/api/dictionary/*/cities', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(MOCK_CITIES) });
  });
  await page.route('**/api/dictionary/*/airports', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(MOCK_AIRPORTS) });
  });
  await page.route('**/api/dictionary/*/countries', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([{ code: 'RU', title: t('Россия', 'Russia'), world_region_id: 500374 }]) });
  });
  await page.route('**/api/dictionary/*/world_regions', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([{ title: t('Россия и страны СНГ', 'Russia and the CIS'), world_region_id: 500374 }]) });
  });

  await page.route('**/api/version', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: '{"version":"1.0"}' });
  });

  await page.route('**/api/board/1/**', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ flights: [], partners: [] }) });
  });
  await page.route('**/api/schedule/1', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
  });
  await page.route('**/api/schedule/details**', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify([]) });
  });
  await page.route('**/api/days/**', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ days: '' }) });
  });
  await page.route('**/api/destinations/**', (route) => {
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ data: { routes: [] } }) });
  });

  await page.route('https://www.aeroflot.ru/**', (route) => route.abort());
  await page.route('https://forms.office.com/**', (route) => route.abort());
}
