package cz.moneta.test.sanity.transactional.cards.wso2;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.sanity.transactional.SmokeData;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "SERVICE ACTIVATION/DEACTIVATION")
public class CardInsurance {
    private String serviceCode;
    private String card;
    private SmokeData smokeData;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        serviceCode = "BC";
        card = smokeData.getCreditData().getCard();
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2092")
    public void MWFA84_CRT_ADD_SERVICES(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA84"))
                .addField("CisloKarty", card)
                .addField("KodDS", serviceCode)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2444")
    public void MWFA82_DO_ADD_SERVICES_DEACTIVATE(Harness harness) {
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now().minusDays(30));
        deactivateServices(harness, date);
    }

    private void deactivateServices(Harness harness, String date) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA82"))
                .addField("CisloKarty", card)
                .addField("DSDatumZruseniDoruceno", date)
                .addField("DSKodDistribucnihoKanalu", "7")
                .addField("KodDS", serviceCode)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(3)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2437")
    public void MWFS86_ADD_SERVICES_REACTIVATE_CREDIT(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFS86"))
                .addField("CARD", card)
                .addField("DS", serviceCode)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(4)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2096")
    public void MWFAS7_GET_LIST_ADD_SERVICES(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFAS7"))
                .addField("CisloKarty", card)
                .addField("DruhKarty", "1")
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].PopisDS", "BCC/POJIŠTĚNÍ VYČERPANÉ ČÁSTKY");
    }

    @AfterAll
    public void deactivateService(Harness harness) {
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now().minusDays(30));
        deactivateServices(harness, date);
    }
}
