"""
Pytest fixtures for the Schaeffler Automat backend test suite.

The tests in this suite are divided into:
  - Unit tests (no DB / network required):  excel_parser, models, schemas
  - Integration tests (require running Postgres + Redis):  API endpoints, tasks

Unit tests run offline; integration tests are gated by the 'integration'
pytest mark so they can be skipped in CI without infrastructure.
"""
from __future__ import annotations

import os
import sys
from pathlib import Path

import pytest

# ---------------------------------------------------------------------------
# Make sure the backend package is importable when tests are run from the
# repo root or from the backend/ directory.
# ---------------------------------------------------------------------------
BACKEND_DIR = Path(__file__).resolve().parent.parent   # …/backend
if str(BACKEND_DIR) not in sys.path:
    sys.path.insert(0, str(BACKEND_DIR))

# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
EXCEL_DIR = Path(__file__).resolve().parent.parent.parent / "Excel-Order-Lists"

EXCEL_FILES: dict[str, Path] = {
    "TRB":             EXCEL_DIR / "TRB_Testscope_20260128.xlsx",
    "Kugellager":      EXCEL_DIR / "Kugellager_Testscope_20260128.xlsx",
    "CRB":             EXCEL_DIR / "CRB_Testscope_20260128.xlsx",
    "Gleitlager":      EXCEL_DIR / "Gleitlager_Testscope_20260128.xlsx",
    "SRB_TORB":        EXCEL_DIR / "SRB_TORB_Testscope_20260128.xlsx",
    "Linear_schiene":  EXCEL_DIR / "Linear_schiene_Testscope_20260128.xlsx",
    "Anschlagplatten": EXCEL_DIR / "Anschlagplatten_Testscope_20260128.xlsx",
}


# ---------------------------------------------------------------------------
# Fixtures – Excel file paths
# ---------------------------------------------------------------------------

@pytest.fixture(scope="session")
def excel_dir() -> Path:
    """Return the directory that contains all sample Excel order lists."""
    assert EXCEL_DIR.is_dir(), f"Excel sample directory not found: {EXCEL_DIR}"
    return EXCEL_DIR


@pytest.fixture(scope="session")
def excel_paths() -> dict[str, Path]:
    """Return a mapping of category key → absolute path for each sample file."""
    missing = [k for k, p in EXCEL_FILES.items() if not p.exists()]
    if missing:
        pytest.skip(f"Sample Excel files missing: {missing}")
    return EXCEL_FILES


# ---------------------------------------------------------------------------
# Fixtures – parsed Excel results (cached per test session)
# ---------------------------------------------------------------------------

@pytest.fixture(scope="session")
def parsed_excel_all(excel_paths: dict[str, Path]) -> dict:
    """Parse all 7 sample Excel files and return {category_key: ParsedExcel}."""
    from app.services.excel_parser import parse_excel

    return {cat: parse_excel(path) for cat, path in excel_paths.items()}


# ---------------------------------------------------------------------------
# Helpers exposed as fixtures
# ---------------------------------------------------------------------------

@pytest.fixture(scope="session")
def parsed_trb(parsed_excel_all):
    return parsed_excel_all["TRB"]


@pytest.fixture(scope="session")
def parsed_kugellager(parsed_excel_all):
    return parsed_excel_all["Kugellager"]


@pytest.fixture(scope="session")
def parsed_crb(parsed_excel_all):
    return parsed_excel_all["CRB"]


@pytest.fixture(scope="session")
def parsed_gleitlager(parsed_excel_all):
    return parsed_excel_all["Gleitlager"]


@pytest.fixture(scope="session")
def parsed_srb_torb(parsed_excel_all):
    return parsed_excel_all["SRB_TORB"]


@pytest.fixture(scope="session")
def parsed_linear_schiene(parsed_excel_all):
    return parsed_excel_all["Linear_schiene"]


@pytest.fixture(scope="session")
def parsed_anschlagplatten(parsed_excel_all):
    return parsed_excel_all["Anschlagplatten"]
