describe('Расписание: Поиск по маршруту', () => {
    const route = {
        departureCity: {
            name: 'Москва',
            code: 'MOW',
            latitude: 55.7558,
            longitude: 37.62,
        },
        arrivalCity: {
            name: 'Сочи',
            code: 'AER',
            latitude: 43.58,
            longitude: 39.72,
        },
    };

    beforeEach(() => {
        cy.intercept('GET', '**/api/flights/1/ru/schedule**').as('getFlights');
        cy.mockGeolocation(route.departureCity);
        cy.visit('/ru-ru/schedule');
    });

    const assertSearchResults = () => {
        cy.getByTestId('loader').should('be.visible');
        cy.wait('@getFlights').then(() => {
            cy.getByTestId('schedule-search-results').should('be.visible');
            cy.getByTestId('schedule-search-result').should('have.length.at.least', 1);
        });
    };

    it(`Должен искать рейсы ${route.departureCity.name} - ${route.arrivalCity.name} , открывать корректный URL и детали рейса`, () => {
        // cy.getByTestId('schedule-departure-city-input').type(`${route.departureCity.name}`).type('{enter}'); 
        cy.getByTestId('schedule-arrival-city-input').type(`${route.arrivalCity.name}`).type('{enter}');

        cy.wait(200);

        cy.getByTestId('schedule-arrival-city-input').getByTestId('city-code').contains(`${route.arrivalCity.code}`).should('be.visible');

        cy.getByTestId('schedule-departure-city-input')
            .getByTestId('city-code')
            .contains(`${route.departureCity.code}`)
            .should('be.visible');

        cy.getByTestId('schedule-search-button').click();

        assertSearchResults();

        cy.getByTestId('schedule-search-result')
            .first()
            .getByTestId('flight-number')
            .should('be.visible')
            .getByTestId('flight-carrier')
            .should('be.visible');

        cy.getByTestId('flight-details-button').click({ force: true });
        cy.getByTestId('flight-details-number').should('be.visible');
        cy.getByTestId('flight-company-logo').should('be.visible');
    });
});
