"""
Pytest configuration and fixtures for The Iron Vault tests.

Provides common fixtures for sandbox setup, API testing, and security tests.
"""

from __future__ import annotations

from collections.abc import Generator
from pathlib import Path

import pytest
from fastapi.testclient import TestClient


@pytest.fixture
def temp_sandbox(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
    """
    Create a real temp sandbox and configure environment.

    Sets FSAL_ROOT and APERION_ENV for testing.
    """
    sandbox = tmp_path / "sandbox"
    sandbox.mkdir()
    monkeypatch.setenv("FSAL_ROOT", str(sandbox))
    monkeypatch.setenv("APERION_ENV", "test")
    return sandbox


@pytest.fixture
def client(
    temp_sandbox: Path, monkeypatch: pytest.MonkeyPatch
) -> Generator[TestClient, None, None]:
    """
    Create a FastAPI TestClient with sandbox configured.

    Authentication is disabled for easier testing.
    """
    monkeypatch.delenv("FSAL_TOKEN", raising=False)

    # Reset auth warning flag
    from aperion_fsal.service.auth import reset_auth_warning

    reset_auth_warning()

    # Import app after env is configured
    from aperion_fsal.service.app import app

    with TestClient(app) as test_client:
        yield test_client


@pytest.fixture
def auth_client(
    temp_sandbox: Path, monkeypatch: pytest.MonkeyPatch
) -> Generator[tuple[TestClient, str], None, None]:
    """
    Create a FastAPI TestClient with authentication enabled.

    Returns tuple of (client, token).
    """
    token = "test-secret-token-12345"
    monkeypatch.setenv("FSAL_TOKEN", token)

    # Reset auth warning flag
    from aperion_fsal.service.auth import reset_auth_warning

    reset_auth_warning()

    # Import app after env is configured
    from aperion_fsal.service.app import app

    with TestClient(app) as test_client:
        yield test_client, token


@pytest.fixture
def populated_sandbox(temp_sandbox: Path) -> Path:
    """
    Create a sandbox with some pre-populated files and directories.

    Structure:
        sandbox/
        ├── file1.txt
        ├── file2.txt
        ├── data.json
        ├── subdir/
        │   ├── nested.txt
        │   └── deep/
        │       └── deepfile.txt
        └── empty_dir/
    """
    # Files in root
    (temp_sandbox / "file1.txt").write_text("content of file1")
    (temp_sandbox / "file2.txt").write_text("content of file2")
    (temp_sandbox / "data.json").write_text('{"key": "value"}')

    # Nested directory
    subdir = temp_sandbox / "subdir"
    subdir.mkdir()
    (subdir / "nested.txt").write_text("nested content")

    deep = subdir / "deep"
    deep.mkdir()
    (deep / "deepfile.txt").write_text("deep content")

    # Empty directory
    (temp_sandbox / "empty_dir").mkdir()

    return temp_sandbox


@pytest.fixture
def binary_file(temp_sandbox: Path) -> Path:
    """Create a file with binary content."""
    binary_path = temp_sandbox / "binary.bin"
    binary_path.write_bytes(b"\x00\x01\x02\x03\xff\xfe\xfd")
    return binary_path
