package io.delphiplatform.api.integration;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import io.delphiplatform.api.v3.model.ArtistSimple;
import io.delphiplatform.api.v3.model.Regions;
import io.delphiplatform.api.v3.model.SortOrder;
import io.delphiplatform.api.v3.rdb.entity.ArtistEntity;
import io.delphiplatform.api.v3.view.ArtistsApi;
import io.delphiplatform.api.v3.view.RegionsApi;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Integration tests sample using testcontainers. Tests execution attached to maven's validate phase.
 * <p>
 * The following conditionals and notes are important for integration tests execution:
 * - Docker should be installed and active
 * - LIQUIBASE_SCRIPTS env variable should point to liquibase scripts folder: delphi-api/liquibase  (will copy to resources/db/migration)
 * - IT tests should followed maven-failsafe-plugin default name convention: *IT.java, *IT*.java, *ITCase.java
 * TODO:
 * - Postgresql container use users/role from the file: db/migration/init/local.sql (critical for the successful Liquibase execution) -
 * - 'integration.tests.enable=true' environment variable being used as enable/disable switch for the tests execution. Integration tests are
 *   disabled by default.
 * <p>
 * Notes:
 * - Currently minor modification at db\migration\db.changelog-master.xml file is required.
 *   <includeAll path="migrations" /> to <includeAll path="migrations" relativeToChangelogFile="true"/>
 *   Will be modified by separate PR on 'delphi-api' project
 * - Postgresql and Bigtable containers are shared amongst all IT test executions.
 * - Every IT test should use @DelphiTestcontainer annotation and include both containers Postgresql and Bigtable
 *   or extend existing BaseIT
 * <p>
 * Examples:
 *  As maven variables:
 *  mvn clean verify -D integration.tests.enable=true -D env.LIQUIBASE_SCRIPTS=PATH_TO_LIQUIBASE_SCRIPTS
 *
 *  As inline environment variable:
 *  (env) LIQUIBASE_SCRIPTS=PATH_TO_LIQUIBASE_SCRIPTS mvn clean verify -D integration.tests.enable=true
 *
 *  As global variable:
 *  export LIQUIBASE_SCRIPTS=PATH_TO_LIQUIBASE_SCRIPTS
 *  mvn clean verify -D integration.tests.enable=true
 *
 *  To copy liquibase scripts only:
 *  mvn antrun:run -D env.LIQUIBASE_SCRIPTS=PATH_TO_LIQUIBASE_SCRIPTS
 *
 *  To clear liquibase from project
 *  mvn clear -D env.LIQUIBASE_SCRIPTS=PATH_TO_LIQUIBASE_SCRIPTS
 */

public class DelphiApplicationIT extends BaseIT {

    @Autowired
    ArtistsApi artistsApi;

    @Autowired
    RegionsApi regionsApi;

    @Autowired
    public EntityManager entityManager;

    void init() {
        ArtistEntity artist = new ArtistEntity();
        artist.setArtistId("artistId1");
        artist.setFirstName("FirstName");
        entityManager.persist(artist);
    }

    @Test
    @Transactional
    void findArtistSample() {
        init();
        ResponseEntity<ArtistSimple> response = artistsApi.findOne("artistId1");
        assertTrue(HttpStatus.OK.equals(response.getStatusCode()));
        ArtistSimple artist = response.getBody();
        assertTrue("FirstName".equals(artist.getFirstName().get()));
    }

    @Test
    @Transactional
    void findRegionSample() {
        ResponseEntity<Regions> response = regionsApi.regionsSearch(10, 0, "country_name", SortOrder.DESC);
        assertTrue(HttpStatus.OK.equals(response.getStatusCode()));
        Regions regions = response.getBody();
        assertTrue(regions.getItems().size() == 10);
    }

}
