package ru.ibs.tests;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;

public class ApplineBusinessTripTest {

    private WebDriver driver;
    private LoginPage loginPage;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.get("http://training.appline.ru/user/login");

        loginPage = new LoginPage(driver);
    }

    @Test
    public void applineLoginScenario() {
        DashboardPage dashboardPage = loginPage.login("Irina Filippova", "testing");
        Assert.assertTrue("Dashboard not loaded", dashboardPage.isDashboardLoaded());

        BusinessTripPage businessTripPage = dashboardPage.goToBusinessTrip();
        Assert.assertTrue("Trips page not loaded", businessTripPage.isTripsPageLoaded());

        businessTripPage.openCreateTripForm();

        businessTripPage.selectDepartment("Отдел внутренней разработки");
        businessTripPage.selectOrganization("Edge");
        businessTripPage.setTicketsCheckbox(true);
        businessTripPage.fillCity("Дмитров");

        businessTripPage.fillDate(
                By.xpath("//input[@placeholder='Укажите дату' and contains(@id, 'departureDatePlan')]"),
                "01.01.2025"
        );

        businessTripPage.fillDate(
                By.xpath("//input[@placeholder='Укажите дату' and contains(@id, 'returnDatePlan')]"),
                "10.01.2025"
        );

        businessTripPage.saveAndClose();

        Assert.assertTrue("Expected validation error not shown",
                businessTripPage.isErrorDisplayed("Список командируемых сотрудников не может быть пустым"));
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
