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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.wso2.Wso2Utils;
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.harness.support.rest.RawRestRequest;
import cz.moneta.test.sanity.transactional.SmokeData;
import org.junit.jupiter.api.BeforeAll;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@TestScenario(name = "Card promos")
public class CardPromoCodes {
    private String account;
    private String promoCode;
    private String offerId;
    private SmokeData smokeData;
    private String dateTo;
    private String datefrom;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        account = smokeData.getCreditData().getAccount();
        promoCode = "CBK0010001";
        offerId = "CCP0010001";
        dateTo = "2027-01-01";
        datefrom = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now().plusDays(6));
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2729")
    public void MWFJX6_CARD_PROMO_SET(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJX6"))
                .addField("ACCOUNT", account)
                .addField("CATEGORY", "RET")
                .addField("CHANNEL_ID", "OM")
                .addField("DATE_TO", dateTo)
                .addField("PROMO_CODE", promoCode)
                .postExtended();

        //fixme promo code can be set only once during batch period. Else got 500.  Design ?
        Wso2Utils.validateResponse(response,
                500,
                smokeData.isGw(),
                "tifResponseStatus.errorDesc",
                "VPLBSAE20S: OFFER ENROLLMENT ALREADY ON FILE",
                "errorMessage",
                "OFFER ENROLLMENT ALREADY ON FILE");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3151")
    public void MWFXXX7_CARD_PROMO_ADD_LOYALTY(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFXXX7"))
                .addField("cardAccountNr", account)
                .addField("offerId", offerId)
                .addField("channelId", "POS")
                .addField("startDate", datefrom)
                .addField("expirationDate", dateTo)
                .addField("SSO", "543830666")
                .postExtended();
        //fixme loyalty can be set only once during batch period. Else got 500.  Design ?
        if (response.getStatus() == 500) {
            response.andAssertFieldValue("tifResponseStatus.errorDesc", "VPLBSAE20S: OFFER ENROLLMENT ALREADY ON FILE");
        } else {
            response.andAssertStatus(200);
        }
    }
}
