import os
from pathlib import Path

import pytest

from fixtures import types
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD


def _env_file(pytestconfig: pytest.Config) -> Path:
    override = os.environ.get("SIGNOZ_E2E_ENV_FILE")
    if override:
        return Path(override)
    return pytestconfig.rootpath / "e2e" / ".env.local"


def test_setup(
    signoz: types.SigNoz,
    create_user_admin: types.Operation,  # pylint: disable=unused-argument
    apply_license: types.Operation,  # pylint: disable=unused-argument
    seeder: types.TestContainerDocker,
    pytestconfig: pytest.Config,
) -> None:
    """Bring the backend up and write e2e coordinates to .env.local."""
    host_cfg = signoz.self.host_configs["8080"]
    seeder_cfg = seeder.host_configs["8080"]
    out = _env_file(pytestconfig)
    out.parent.mkdir(parents=True, exist_ok=True)
    with out.open("w") as f:
        f.write("# Generated by tests/e2e/bootstrap/setup.py — do not edit.\n")
        f.write(f"SIGNOZ_E2E_BASE_URL={host_cfg.base()}\n")
        f.write(f"SIGNOZ_E2E_USERNAME={USER_ADMIN_EMAIL}\n")
        f.write(f"SIGNOZ_E2E_PASSWORD={USER_ADMIN_PASSWORD}\n")
        f.write(f"SIGNOZ_E2E_SEEDER_URL={seeder_cfg.base()}\n")


def test_teardown(
    signoz: types.SigNoz,  # pylint: disable=unused-argument
    create_user_admin: types.Operation,  # pylint: disable=unused-argument
    apply_license: types.Operation,  # pylint: disable=unused-argument
    seeder: types.TestContainerDocker,  # pylint: disable=unused-argument
) -> None:
    """Fixture dependencies trigger container teardown via --teardown."""
