#!/bin/bash -e

# This script tests full refresh and incremental builds for all views
# in a given model group
#
# Example usage:
#   test_integration_models.sh summary_streams

MODELS=$1

# Clean up any stale data/ directories on exit (success or failure),
# so a failed run never leaves seed CSVs that conflict with model names.
cleanup_data() {
    rm -rf data/full_refresh data/incremental data/base
}
trap cleanup_data EXIT
cleanup_data

RUN_OPTIONS="--models base.* $MODELS.* --exclude MOVE_DATA_FROM_RECENT_TO_HISTORY tag:v_view mappings.MAPPING_ISRC_TO_GLOBAL_PARTICIPANT_DBT"
ALL_FEED_IDS="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, \
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43"
ALL_DISTRIBUTORS="'theorchard', 'sme', 'awal'"

# Helper to run tests for given models
test_views() {
    pipenv run dbt test --target test $1
}



# Full refresh
run_full_refresh() {
    export FEED_FILTER=""
    export DISTRIBUTOR_FILTER=""
    echo -e '\n\033[0;34m=== Running full refresh on module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
    if [ -d data/full_refresh ]; then
        rm -rf data/full_refresh
    fi
    mkdir -p data/full_refresh
    cp -r base/tests/base_tables/* data/full_refresh/
    if [ -d $MODELS/tests/integration/fixtures/full_refresh ]; then
        cp -r $MODELS/tests/integration/fixtures/full_refresh/* data/full_refresh/
    fi
    # Copy tier-specific fixtures (e.g., fixtures/hourly/full_refresh/)
    for tier_dir in $MODELS/tests/integration/fixtures/*/full_refresh; do
        if [ -d "$tier_dir" ]; then
            cp -r "$tier_dir"/* data/full_refresh/
        fi
    done
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' seeds in full-refresh mode ===\033[0m'
    # Ignoring warnings until config issues with 0.17.0 are properly fixed
    # scripts/run_seed_tests.sh --target test --full-refresh
    scripts/run_seed.sh --target test --full-refresh
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' views and tables in full-refresh mode ===\033[0m'
    # scripts/run_views_tests.sh --target test --full-refresh $RUN_OPTIONS
    scripts/run_views.sh --target test --full-refresh $RUN_OPTIONS
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' tests in full-refresh mode ===\033[0m'
    test_views "$RUN_OPTIONS"
    rm -rf data/full_refresh
    echo -e '\n\033[0;34m=== Finished running full refresh on module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
}

# Incremental
run_incremental() {
    export FEED_FILTER="$ALL_FEED_IDS"
    export DISTRIBUTOR_FILTER="$ALL_DISTRIBUTORS"
    echo -e '\n\033[0;34m=== Running incremental build on module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
    if [ -d data/incremental ]; then
        rm -rf data/incremental
    fi
    mkdir -p data/incremental
    cp -r base/tests/base_tables/* data/incremental/
    if [ -d $MODELS/tests/integration/fixtures/incremental ]; then
        cp -r $MODELS/tests/integration/fixtures/incremental/* data/incremental/
    fi
    # Copy tier-specific fixtures (e.g., fixtures/hourly/incremental/)
    for tier_dir in $MODELS/tests/integration/fixtures/*/incremental; do
        if [ -d "$tier_dir" ]; then
            cp -r "$tier_dir"/* data/incremental/
        fi
    done
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' seeds in incremental mode ===\033[0m'
    # Ignoring warnings until config issues with 0.17.0 are properly fixed
    # scripts/run_seed_tests.sh --target test
    scripts/run_seed.sh --target test
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' views and tables in incremental mode ===\033[0m'
    # scripts/run_views_tests.sh --target test $RUN_OPTIONS
    scripts/run_views.sh --target test $RUN_OPTIONS
    echo -e '\n\n\033[0;33m=== Running '"$MODELS"' tests in incremental mode ===\033[0m'
    test_views "$RUN_OPTIONS"
    rm -rf data/incremental
    echo -e '\n\033[0;34m=== Finished running incremental build on module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
}

run_v_view_tests() {
    echo -e '\n\033[0;33m=== Running V_ view models ===\033[0m'
    pipenv run dbt run --target test --models "$MODELS.*,tag:v_view"
    echo -e '\n\033[0;33m=== Testing V_ views ===\033[0m'
    pipenv run dbt test --target test --models "$MODELS.*,tag:v_view"
}

run_operations() {
    echo -e '\n\033[0;34m=== Running module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
    scripts/run_views.sh --target test --models "$MODELS" --exclude MOVE_DATA_FROM_RECENT_TO_HISTORY
    echo -e '\n\033[0;34m=== Finished running module \033[0;36m'"$MODELS"'\033[0;34m ===\n\033[0m'
}

if [ -d "$MODELS/tests/integration/fixtures/full_refresh" ]; then
  run_full_refresh
  if [ "$MODELS" = "playlists" ]; then
    run_v_view_tests
  fi
fi

if [ -d "$MODELS/tests/integration/fixtures/incremental" ]; then
  run_incremental
  if [ "$MODELS" = "playlists" ]; then
    run_v_view_tests
  fi
fi

if [ "$MODELS" = "operations" ]; then
  run_operations
fi
