#!/bin/bash

# script/develop: Start Home Assistant in development mode
#
# Starts Home Assistant with debug logging enabled, using the config/ directory
# for configuration. Automatically cleans up accidental package installations and
# sets PYTHONPATH to load the integration from custom_components/.
#
# Usage:
#   ./script/develop
#
# Examples:
#   ./script/develop

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR/.."

# shellcheck source=script/.lib/output.sh
source "$SCRIPT_DIR/.lib/output.sh"

if [[ -z ${VIRTUAL_ENV:-} ]]; then
    log_header "Activating virtual environment"
    # shellcheck source=/dev/null
    if [[ -f "$HOME/.local/ha-venv/bin/activate" ]]; then
        source "$HOME/.local/ha-venv/bin/activate"
    elif [[ -f "$PWD/.local/ha-venv/bin/activate" ]]; then
        source "$PWD/.local/ha-venv/bin/activate"
    else
        log_error "Virtual environment not found in $HOME/.local/ha-venv or $PWD/.local/ha-venv"
        exit 1
    fi
fi

# Kill any existing Home Assistant and debugpy processes
log_header "Cleaning up existing processes"
if pkill -f "hass --config.*${PWD}/config"; then
    log_info "Stopped existing Home Assistant process"
fi
if pkill -f "debugpy.*5678"; then
    log_info "Stopped existing debugpy process (port 5678)"
fi
sleep 1  # Give processes time to terminate

# Clean up critical issues (accidental package installations)
# Uses minimal mode to avoid deleting useful caches
"$SCRIPT_DIR/clean" --minimal

# Create config dir if not present
if [[ ! -d ${PWD}/config ]]; then
        mkdir -p "${PWD}/config"
        hass --config "${PWD}/config" --script ensure_config
fi

"$SCRIPT_DIR/setup/sync-hacs"

# Set the path to custom_components
## This let's us have the structure we want <root>/custom_components/tapo
## while at the same time have Home Assistant configuration inside <root>/config
## without resulting to symlinks.
export PYTHONPATH="${PYTHONPATH:-}:${PWD}/custom_components"

log_header "Starting Home Assistant in development mode"
log_info "Config directory: ${PWD}/config"
log_info "Debug logging: enabled"
log_separator
echo ""

# Start Home Assistant
hass --config "${PWD}/config" --debug
