#!/usr/bin/env bash
# state.sh - Deployment state management

set_stage() {
    local stage=$1
    echo "$stage" > "${STATE_DIR}/current_stage"
}

get_stage() {
    if [[ -f "${STATE_DIR}/current_stage" ]]; then
        cat "${STATE_DIR}/current_stage"
    else
        echo "none"
    fi
}

mark_stage_complete() {
    local stage=$1
    touch "${STATE_DIR}/stage_${stage}_complete"
}

is_stage_complete() {
    local stage=$1
    [[ -f "${STATE_DIR}/stage_${stage}_complete" ]]
}

clear_deployment_state() {
    rm -f "${STATE_DIR}"/stage_*_complete
    rm -f "${STATE_DIR}/current_stage"
    rm -f "${STATE_DIR}/last_service"
}

set_last_service() {
    echo "$1" > "${STATE_DIR}/last_service"
}

get_last_service() {
    if [[ -f "${STATE_DIR}/last_service" ]]; then
        cat "${STATE_DIR}/last_service"
    else
        echo ""
    fi
}

export -f set_stage
export -f get_stage
export -f mark_stage_complete
export -f is_stage_complete
export -f clear_deployment_state
export -f set_last_service
export -f get_last_service
