from pathlib import Path
import re

import pytest


def test_install_script_runs_migrate():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "python manage.py migrate" in content


def test_install_script_includes_terminal_flag():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "--terminal" in content


def test_install_script_includes_watchtower_flag():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "--watchtower" in content


def test_install_script_excludes_virtual_flag():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "--virtual" not in content


def test_install_script_excludes_particle_flag():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "--particle" not in content





def test_install_script_runs_env_refresh():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "env-refresh.sh" in content


@pytest.mark.feature("nginx-server")
def test_install_script_requires_nginx_for_roles():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    expected_requirements = {
        "satellite": "satellite",
        "control": "control",
        "watchtower": "watchtower",
    }
    for flag, requirement in expected_requirements.items():
        assert f"--{flag}" in content
        assert f'require_nginx "{requirement}"' in content


def test_install_script_role_defaults():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()

    def block(flag: str) -> str:
        pattern = rf"--{flag}\)(.*?)\n\s*;;"
        match = re.search(pattern, content, re.S)
        assert match, f"block for {flag} not found"
        return match.group(1)

    satellite = block("satellite")
    assert "AUTO_UPGRADE" not in satellite

    watchtower = block("watchtower")
    assert "AUTO_UPGRADE" not in watchtower

    control = block("control")
    assert "AUTO_UPGRADE" not in control

    assert "--virtual)" not in content
    assert "--particle)" not in content


def test_install_script_uses_auto_upgrade_lockfile():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "auto_upgrade.lck" in content


def test_install_script_records_systemd_units():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()

    assert "systemd_services.lck" in content
    assert "arthexis_record_systemd_unit" in content


def test_install_script_checks_rfid_for_control_nodes():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "python -m ocpp.rfid.detect" in content
    assert "rfid-scanner" in content


def test_install_script_configures_nginx_during_install():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()
    assert "nginx-setup.sh" in content
    assert "configure_nginx_site" in content


def test_nginx_setup_script_renders_nginx_config_with_helper():
    script_path = Path(__file__).resolve().parent.parent / "nginx-setup.sh"
    content = script_path.read_text()
    assert "render_nginx_default.py" in content
    assert "backend_port.lck" in content
    assert "nginx_mode.lck" in content
    assert "--remove" in content
    assert "--ip6" in content


def test_install_script_stops_services_during_repair():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()

    assert "stop_existing_units_for_repair" in content
    assert 'if [ "$REPAIR" = true ] && [ -n "$SERVICE" ]; then' in content
    assert 'stop_existing_units_for_repair "$SERVICE"' in content


def test_install_script_cleans_services_and_artifacts_on_clean_install():
    script_path = Path(__file__).resolve().parent.parent / "install.sh"
    content = script_path.read_text()

    assert "clean_previous_installation_state" in content
    assert 'if [ "$CLEAN" = true ]; then' in content
    assert "remove_systemd_unit_if_present" in content
    assert "find \"$LOG_DIR\" -type f ! -samefile \"$LOG_FILE\" -delete" in content
