#!/usr/bin/env bash

export ENABLE_OTEL=true
export OCUROOT_DEBUG=true
export OCUROOT_DEBUG_TRACES=true
export OCUROOT_CHECK_STAGED_FILES=true

export OCU_REPO_COMMIT_OVERRIDE=${OCU_REPO_COMMIT_OVERRIDE:-commitid}

export OCUROOT_HOME=$(pwd)/$(dirname "$0")/.ocuroot

source $(dirname "$0")/../test_helpers.sh

test_basic() {
    echo "Test: basic"
    echo ""
    setup_test

    echo "== release v1 =="
    ocuroot release new basic.ocu.star
    assert_equal "0" "$?" "Failed to release v1"

    assert_deployed "basic.ocu.star" "staging"
    assert_deployed "basic.ocu.star" "production"
    assert_deployed "basic.ocu.star" "production2"

    echo "Test succeeded"
    echo ""
}

test_basic() {
    echo "Test: missing done"
    echo ""
    setup_test

    echo "== release v1 =="
    ocuroot release new missing_done.star
    assert_equal "0" "$?" "Failed to release v1"

    echo "Test succeeded"
    echo ""
}

test_two_releases() {
    echo "Test: two releases"
    echo ""
    setup_test

    echo "== release v1 (using omnibus work command) =="
    ocuroot release new approvals.ocu.star
    assert_equal "0" "$?" "Failed to release v1"

    echo "== create approval intent =="
    ocuroot state set "minimal/repo/-/approvals.ocu.star/@v1/custom/approval" 1
    assert_equal "0" "$?" "Failed to create approval intent"
    echo "== apply approval intent and continue work =="
    ocuroot work any --dryrun
    ocuroot work any
    assert_equal "0" "$?" "Failed to apply approval intent and continue work"

    # Check that the work was applied
    check_ref_exists "minimal/repo/-/approvals.ocu.star/@r1/custom/approval"

    echo "== create approval2 intent =="
    ocuroot state set "minimal/repo/-/approvals.ocu.star/@r1/custom/approval2" 1
    assert_equal "0" "$?" "Failed to create approval2 intent"
    echo "== apply approval2 intent and continue work =="
    ocuroot work any
    assert_equal "0" "$?" "Failed to apply approval intent and continue work"

    assert_deployed "approvals.ocu.star" "staging"
    assert_deployed "approvals.ocu.star" "production"
    assert_deployed "approvals.ocu.star" "production2"

    echo "== continue work - should be none =="
    ocuroot work continue
    assert_equal "0" "$?" "Should be no work to continue"

    echo ""
    echo "v2"
    echo ""

    echo "== release v2 =="
    ocuroot release new approvals.ocu.star --force
    assert_equal "0" "$?" "Failed to release v2"
    echo "== create approval intent =="
    ocuroot state set "minimal/repo/-/approvals.ocu.star/@v2/custom/approval" 1
    assert_equal "0" "$?" "Failed to create approval intent"
    echo "== apply approval intent =="
    ocuroot state apply "minimal/repo/-/approvals.ocu.star/@v2/custom/approval"
    assert_equal "0" "$?" "Failed to apply approval intent"
    echo "== continue release up to second approval =="
    ocuroot release continue minimal/repo/-/approvals.ocu.star/@v2
    assert_equal "0" "$?" "Failed to continue release up to second approval"
    echo "== create approval2 intent =="
    ocuroot state set "minimal/repo/-/approvals.ocu.star/@v2/custom/approval2" 1
    assert_equal "0" "$?" "Failed to create approval2 intent"
    echo "== apply approval2 intent =="
    ocuroot state apply "minimal/repo/-/approvals.ocu.star/@v2/custom/approval2"
    assert_equal "0" "$?" "Failed to apply approval2 intent"
    echo "== continue release =="
    ocuroot release continue minimal/repo/-/approvals.ocu.star/@v2
    assert_equal "0" "$?" "Failed to continue release"

    echo "Test succeeded"
    echo ""
}

test_deploy_intent() {
    echo "Test: deploy intent"
    echo ""
    setup_test

    echo "== release v1 =="
    ocuroot release new basic.ocu.star
    assert_equal "0" "$?" "Failed to release v1"

    assert_deployed "basic.ocu.star" "staging"
    assert_deployed "basic.ocu.star" "production"
    assert_deployed "basic.ocu.star" "production2"

    echo "== delete deployment intent =="
    ocuroot state delete minimal/repo/-/basic.ocu.star/@/deploy/production
    assert_equal "0" "$?" "Failed to delete deployment intent"

    echo "== apply all outstanding intents =="
    ocuroot state diff | xargs -n1 ocuroot state apply
    assert_equal "0" "$?" "Failed to diff state"

    echo "== continue =="
    ocuroot work continue
    assert_equal "0" "$?" "Failed to continue work"

    assert_deployed "basic.ocu.star" "staging"
    assert_not_deployed "basic.ocu.star" "production"
    assert_deployed "basic.ocu.star" "production2"

    echo "Test succeeded"
    echo ""
}

test_force_deploy() {
    echo "Test: force deploy"
    echo ""
    setup_test

    echo "== initial release =="
    ocuroot release new basic.ocu.star
    assert_equal "0" "$?" "Failed to release v1"

    check_ref_exists "basic.ocu.star/@r1"

    echo "== block release to same commit =="
    ocuroot release new basic.ocu.star
    assert_not_equal "0" "$?" "Should not allow release to same commit"

    check_ref_does_not_exist "basic.ocu.star/@r2"

    echo "== force release to same commit =="
    ocuroot release new basic.ocu.star --force
    assert_equal "0" "$?" "Failed to force release to same commit"

    check_ref_exists "basic.ocu.star/@r2"

    echo "== deploy to different commit =="
    OCU_REPO_COMMIT_OVERRIDE=commit2 ocuroot release new basic.ocu.star
    assert_equal "0" "$?" "Failed to deploy to different commit"

    check_ref_exists "basic.ocu.star/@r3"

    echo "Test succeeded"
    echo ""
}

test_task_with_no_phases() {
    echo "Test: task with no phases"
    echo ""
    setup_test

    echo "== release v1 =="
    ocuroot release new build_only.ocu.star
    assert_equal "0" "$?" "Failed to release v1"

    check_ref_exists "build_only.ocu.star/@r1/task/build/1/status/complete"

    echo "Test succeeded"
    echo ""
}

setup_test() {
    # Clean up any previous runs
    rm -rf .store
    rm -rf .build
    rm -rf $OCUROOT_HOME

    # Set up environments
    echo "ocuroot release new environments/package.ocu.star"
    ocuroot release new environments/package.ocu.star
    assert_equal "0" "$?" "Failed to set up environments"
}

build_ocuroot

pushd "$(dirname "$0")" > /dev/null

test_basic
test_two_releases
test_deploy_intent
test_force_deploy
test_task_with_no_phases
popd > /dev/null
