#!/bin/bash

set -e

cd /var/app

export DBT_PROFILES_DIR="/var/app/profiles/snowflake_auth"

# run linting of python and yaml files
echo 'Running linting...'
uv run sqlfluff lint .

# run seed
echo 'Running using test target ...'
uv run dbt seed --full-refresh --target test

# run models
run_command="uv run dbt run --target test"
if [ -n "${DBT_FULL_REFRESH}" ]; then
  run_command+=" --full-refresh"
fi
run_command+=" --exclude ${POST_SNAPSHOT_MODELS}"
if [ -n "${DISABLE_DEV_SEGMENTATION}" ]; then
  run_command+=" ${EXCLUDED_MODELS}"
fi
echo "Running dbt run command: ${run_command}"
$run_command

# run snapshot
echo "Running dbt snapshot command..."
uv run dbt snapshot --target test

# run post snapshot
echo "Running dbt post snapshot command..."
uv run dbt run --target test --select ${POST_SNAPSHOT_MODELS}

# run test
test_command="uv run dbt test --target test"
if [ -n "${DISABLE_DEV_SEGMENTATION}" ] && [ -n "${EXCLUDED_MODELS}" ]; then
  test_command+=" --exclude ${EXCLUDED_MODELS}"
fi
echo "Running dbt test command: ${test_command}"
$test_command

echo "Running Elementary report creation"
uv run edr report

echo 'Done!'
