@Library('release') _

final String repository = 'qe-fe-automation'
final String stagingPrimeBaseUrl = 'https://staging-prime.navan.com'
final String defaultBranch = 'master'

buildFunction = {

    Closure buildPRFunction = {
        String version = qaAutomation.calculateVersion(env.BRANCH_NAME)
        try {
            taRunCypressTests(suite: 'sanity_test', tags: '', baseUrl: stagingPrimeBaseUrl, branch: env.BRANCH_NAME, performBuild: true)
        } catch (Exception e) {
            awsCliHelper.deleteECRImage(repository: repository, imageTag: version)
            error("Build process failed with the following exception: ${e}")
        }
    }

    Closure buildMasterBranchFunction = {
        String version = qaAutomation.calculateVersion(env.BRANCH_NAME)
        String runImage = qaAutomation.buildTestImage(branch: defaultBranch)
        try {
            dockerCmdHelper.pushImage(env.ECR_DOCKER_HOST, repository, version)
            taRunCypressTests(suite: 'sanity_test', tags: '', baseUrl: stagingPrimeBaseUrl, branch: defaultBranch, runImage: runImage)
            dockerCmdHelper.tagImage(registry: env.ECR_DOCKER_HOST, imageName: repository, version: version, destinationRegistry: env.ECR_DOCKER_HOST, destinationImageName: repository, destinationVersion: 'latest')
            dockerCmdHelper.pushImage(env.ECR_DOCKER_HOST, repository, 'latest')
        } catch (Exception e) {
            awsCliHelper.deleteECRImage(repository: repository, imageTag: version)
            error("Build process failed with the following exception: ${e}")
        }
    }

    taPipelineCommon(
        repository,
        false,
        [],
        { },
        buildMasterBranchFunction,
        buildPRFunction,
        [:],
        'JenkinsFollower',
        false
    )

    if (env.IS_CUSTOM_SNAPSHOT_JOB == 'true') {
        // TODO execute tests here
        echo 'Not doing anything for now'
    }
}

taBuildLibrary(
    repository: repository,
    buildFunction: buildFunction
)
