#!/usr/bin/env bash

role_preflight() {
    if [[ "${ZHCT_STATIC_VALIDATE:-0}" != "1" ]]; then
        require_command sha256sum
        require_command tar
    fi
}

ensure_mysql_account() {
    if ! getent group mysql >/dev/null 2>&1; then run groupadd --system mysql; fi
    if ! id mysql >/dev/null 2>&1; then run useradd --system --gid mysql --home-dir /nonexistent --shell /sbin/nologin mysql; fi
}

install_mysql_binary() {
    local version=$1 archive=$2 extracted=$3
    local prefix="/opt/mysql/$version"
    [[ -x "$prefix/bin/mysqld" ]] && return
    backup_file "$prefix"
    run rm -rf "$BUILD_ROOT/$version"
    extract_archive "$(asset_path "$archive")" "$BUILD_ROOT/$version"
    ensure_dir /opt/mysql 0755 root root
    run mv "$BUILD_ROOT/$version/$extracted" "$prefix"
    run chown -R root:root "$prefix"
}

stage_mysql_templates() {
    ensure_dir "$TEMPLATE_ROOT" 0750 root root
    run rsync -a --delete "$ROLE_ROOT/templates/" "$TEMPLATE_ROOT/"
    for unit in mysql56 mysql80 mysql-health56 mysql-health80; do
        install_file "$ROLE_ROOT/templates/systemd/$unit.service" "/etc/systemd/system/$unit.service" 0644 root root
    done
    install_file "$ROLE_ROOT/templates/runtime/mysql-primary-health.sh" /usr/local/sbin/mysql-primary-health.sh 0750 root root
    run systemctl daemon-reload
    stop_disable_units "$UNITS_MANIFEST"
}

role_install() {
    if [[ "${ZHCT_STATIC_VALIDATE:-0}" == "1" ]]; then
        log 'STATIC: mysql install plan loaded'
        return
    fi
    install_offline_rpms
    ensure_dir "$BUILD_ROOT" 0750 root root
    ensure_mysql_account
    install_mysql_binary 5.6.51 mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz mysql-5.6.51-linux-glibc2.12-x86_64
    install_mysql_binary 8.0.46 mysql-8.0.46-linux-glibc2.28-x86_64.tar.xz mysql-8.0.46-linux-glibc2.28-x86_64
    ensure_dir /data/mysql56 0750 mysql mysql
    ensure_dir /data/mysql56/binlog 0750 mysql mysql
    ensure_dir /data/mysql56/relay 0750 mysql mysql
    ensure_dir /data/mysql80 0750 mysql mysql
    ensure_dir /data/mysql80/binlog 0750 mysql mysql
    ensure_dir /data/mysql80/relay 0750 mysql mysql
    ensure_dir /var/log/mysql56 0750 mysql mysql
    ensure_dir /var/log/mysql80 0750 mysql mysql
    stage_mysql_templates
}

role_verify() {
    if [[ "${ZHCT_STATIC_VALIDATE:-0}" == "1" ]]; then
        log 'STATIC: mysql role verification contract loaded'
        return
    fi
    /opt/mysql/5.6.51/bin/mysqld --version | grep -Fq '5.6.51'
    /opt/mysql/8.0.46/bin/mysqld --version | grep -Fq '8.0.46'
    for path in /data/mysql56 /data/mysql80; do
        if find "$path" -mindepth 1 ! -type d -print -quit | grep -q .; then
            die "template contains persistent MySQL state: $path"
        fi
    done
    verify_template_units_inactive "$UNITS_MANIFEST"
}

role_seal() {
    for path in /data/mysql56 /data/mysql80; do
        if find "$path" -mindepth 1 ! -type d -print -quit | grep -q .; then
            die "refuse to seal a template containing MySQL state: $path"
        fi
    done
    run rm -f /etc/zhct/node.env
    run rm -rf /etc/zhct/secrets
    run find /var/log/mysql56 /var/log/mysql80 -type f -delete
}

role_post_clone() {
    local node_template="$TEMPLATE_ROOT/nodes/$NODE_KEY"
    require_dir "$node_template"
    render_template "$node_template/my56.cnf" /etc/my56.cnf 0640 root mysql
    render_template "$node_template/my80.cnf" /etc/my80.cnf 0640 root mysql
    render_template "$TEMPLATE_ROOT/systemd/mysql-health56.service" /etc/systemd/system/mysql-health56.service 0644 root root
    render_template "$TEMPLATE_ROOT/systemd/mysql-health80.service" /etc/systemd/system/mysql-health80.service 0644 root root
    run systemctl daemon-reload
    stop_disable_units "$UNITS_MANIFEST"
}

role_activate() {
    if [[ "$DRY_RUN" == "1" ]]; then
        log 'DRY-RUN: would require both initialized data directories and validate MySQL configs before activation'
        return
    fi
    [[ -e /data/mysql56/mysql/user.frm ]] || die 'MySQL5.6 data directory is not initialized; complete Q-121/Q-122 first'
    [[ -e /data/mysql80/auto.cnf ]] || die 'MySQL8.0 data directory is not initialized; complete Q-121/Q-122 first'
    /opt/mysql/5.6.51/bin/mysqld --defaults-file=/etc/my56.cnf --verbose --help >/dev/null
    /opt/mysql/8.0.46/bin/mysqld --defaults-file=/etc/my80.cnf --validate-config
}

role_rollback() {
    stop_disable_units "$UNITS_MANIFEST"
}
