"""Shared fixtures for unit tests."""

from __future__ import annotations

import textwrap
from pathlib import Path

import pytest
import yaml


@pytest.fixture()
def tmp_config_dir(tmp_path: Path) -> Path:
    """Create a temporary bundled config directory with sample files."""
    config_dir = tmp_path / "config"
    config_dir.mkdir()

    # defaults.conf
    (config_dir / "defaults.conf").write_text(textwrap.dedent("""\
        DEFAULT_CLOUDINIT_USER="admin"
        DEFAULT_CLOUDINIT_PASSWORD="secret123"
        DEFAULT_VM_CORES=2
        DEFAULT_VM_MEMORY=2048
        DEFAULT_VM_DISK_SIZE="10G"
    """))

    # distributions.yml
    distributions = {
        "distributions": {
            "ubuntu": {
                "family": "debian",
                "display_name": "Ubuntu",
                "versions": {
                    "22.04": {
                        "name": "Ubuntu 22.04 LTS",
                        "iso_url": "https://example.com/ubuntu-22.04.iso",
                    },
                    "24.04": {
                        "name": "Ubuntu 24.04 LTS",
                        "iso_url": "https://example.com/ubuntu-24.04.iso",
                    },
                },
            },
            "rocky": {
                "family": "rhel",
                "display_name": "Rocky Linux",
                "versions": {
                    "9": {
                        "name": "Rocky Linux 9",
                        "iso_url": "https://example.com/rocky-9.iso",
                    },
                },
            },
        }
    }
    with open(config_dir / "distributions.yml", "w") as f:
        yaml.dump(distributions, f)

    # packages/
    pkg_dir = config_dir / "packages"
    pkg_dir.mkdir()

    base_packages = {
        "apt_packages": ["curl", "git", "wget"],
        "dnf_packages": ["curl", "git", "wget"],
    }
    with open(pkg_dir / "base-packages.yml", "w") as f:
        yaml.dump(base_packages, f)

    extended_packages = {
        "apt_packages": ["curl", "git", "wget", "docker.io", "terraform"],
        "dnf_packages": ["curl", "git", "wget", "docker-ce", "terraform"],
    }
    with open(pkg_dir / "extended-packages.yml", "w") as f:
        yaml.dump(extended_packages, f)

    return config_dir


@pytest.fixture()
def tmp_user_config_dir(tmp_path: Path) -> Path:
    """Create a temporary user override config directory."""
    user_dir = tmp_path / "user_config"
    user_dir.mkdir()
    return user_dir


@pytest.fixture()
def tmp_packer_dir(tmp_path: Path) -> Path:
    """Create a temporary packer directory with stub HCL files."""
    packer_dir = tmp_path / "packer"
    packer_dir.mkdir()

    for subdir, files in {
        "ubuntu": [
            "ubuntu-22.04.pkr.hcl",
            "ubuntu-24.04.pkr.hcl",
            "ubuntu-24.04-desktop.pkr.hcl",
        ],
        "debian": [
            "debian-12.pkr.hcl",
        ],
        "rhel-family": [
            "rocky-9.pkr.hcl",
            "alma-9.pkr.hcl",
            "fedora-42.pkr.hcl",
            "fedora-42-desktop.pkr.hcl",
        ],
        "suse": [
            "opensuse-leap-15.6.pkr.hcl",
            "opensuse-leap-15.6-desktop.pkr.hcl",
        ],
    }.items():
        d = packer_dir / subdir
        d.mkdir()
        for fname in files:
            (d / fname).write_text("# stub")

    return packer_dir
