// Home AI Assistant — деплой на Linux (Docker).
//
// Нода Jenkins: Labels = linux  (Manage Jenkins → Nodes → мастер → Configure)
//
// На сервере: /home/grigo/to_services/Home_assistant (.env, data/)

pipeline {
    agent {
        label 'linux'
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '25'))
        timeout(time: 45, unit: 'MINUTES')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        string(
            name: 'GIT_BRANCH',
            defaultValue: 'main',
            description: 'Ветка: git reset --hard origin/<branch>'
        )
        string(
            name: 'DEPLOY_DIR',
            defaultValue: '/home/grigo/to_services/Home_assistant',
            description: 'Каталог с .env, data/ и docker-compose.yml'
        )
        string(
            name: 'BACKEND_HEALTH_URL',
            defaultValue: 'http://127.0.0.1:8202/api/v1/health',
            description: 'Healthcheck после деплоя'
        )
        booleanParam(
            name: 'RUN_TESTS',
            defaultValue: true,
            description: 'npm run build + compileall в workspace перед деплоем'
        )
        booleanParam(
            name: 'DOCKER_PULL',
            defaultValue: true,
            description: 'docker compose build --pull'
        )
    }

    environment {
        GIT_BRANCH = "${params.GIT_BRANCH}"
        DEPLOY_DIR = "${params.DEPLOY_DIR}"
        BACKEND_HEALTH_URL = "${params.BACKEND_HEALTH_URL}"
        DOCKER_PULL = "${params.DOCKER_PULL}"
        RUN_TESTS = "${params.RUN_TESTS}"
    }

    stages {
        stage('Test') {
            when {
                expression { return params.RUN_TESTS }
            }
            steps {
                sh '''
                    set -euxo pipefail
                    cd frontend
                    if [ -f package-lock.json ]; then npm ci; else npm install; fi
                    npm run build
                    cd ..
                    python3 -m compileall -q backend/app
                '''
            }
        }

        stage('Preflight') {
            steps {
                sh '''
                    set -euxo pipefail
                    command -v docker
                    docker compose version
                    test -d "${DEPLOY_DIR}"
                    test -f "${DEPLOY_DIR}/.env"
                    test -f "${DEPLOY_DIR}/docker-compose.yml"
                '''
            }
        }

        stage('Deploy') {
            steps {
                dir("${DEPLOY_DIR}") {
                    sh '''
                        set -euxo pipefail
                        git fetch --prune origin
                        git reset --hard "origin/${GIT_BRANCH}"
                        git clean -fd -e .env -e data -e 'data/**'

                        if [ "${DOCKER_PULL}" = "true" ]; then
                            docker compose build --pull
                        else
                            docker compose build
                        fi
                        docker compose up -d
                        docker compose ps
                    '''
                }
            }
        }

        stage('Healthcheck') {
            steps {
                sh '''
                    set -euxo pipefail
                    for i in $(seq 1 30); do
                        if curl -fsS "${BACKEND_HEALTH_URL}" >/dev/null; then
                            echo "OK: ${BACKEND_HEALTH_URL}"
                            exit 0
                        fi
                        sleep 2
                    done
                    echo "Healthcheck failed: ${BACKEND_HEALTH_URL}"
                    exit 1
                '''
            }
        }
    }

    post {
        success {
            echo "Deployed ${DEPLOY_DIR} @ origin/${GIT_BRANCH}"
        }
        failure {
            dir("${DEPLOY_DIR}") {
                sh '''
                    docker compose ps || true
                    docker compose logs --tail=100 backend || true
                    docker compose logs --tail=50 frontend || true
                '''
            }
        }
    }
}
