"""Pytest configuration and fixtures."""

import tempfile
from pathlib import Path
from typing import Generator

import pytest

from src.core.constants import PATHS


@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
    """
    Create a temporary directory for tests.

    Yields:
        Path to temporary directory
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)


@pytest.fixture
def temp_config_dir(temp_dir: Path) -> Generator[Path, None, None]:
    """
    Create a temporary config directory with minimal config files.

    Yields:
        Path to temporary config directory
    """
    config_dir = temp_dir / "config"
    config_dir.mkdir()

    # Create minimal config.yaml
    config_file = config_dir / "config.yaml"
    config_file.write_text(
        """
app:
  name: "Test App"
  version: "0.1.0"
  environment: "test"
  debug: true

trading:
  session:
    start_time: "03:00"
    end_time: "04:00"
    timezone: "America/New_York"
  instrument:
    symbol: "TEST"
    exchange: "TEST"
    contract_size: 25

data:
  raw_data_path: "data/raw"
  processed_data_path: "data/processed"
  labels_path: "data/labels"
  screenshots_path: "data/screenshots"
  timeframes:
    - "1min"
    - "5min"
    - "15min"

models:
  base_path: "models"
  pattern_graders_path: "models/pattern_graders"
  strategy_models_path: "models/strategy_models"
  min_labels_per_pattern: 200
  train_test_split: 0.8
"""
    )

    # Create minimal logging.yaml
    logging_file = config_dir / "logging.yaml"
    logging_file.write_text(
        """
version: 1
disable_existing_loggers: false

formatters:
  detailed:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    datefmt: '%Y-%m-%d %H:%M:%S'

handlers:
  console:
    class: logging.StreamHandler
    level: INFO
    formatter: detailed
    stream: ext://sys.stdout

loggers:
  src:
    level: DEBUG
    handlers:
      - console
    propagate: false

root:
  level: INFO
  handlers:
    - console
"""
    )

    yield config_dir


@pytest.fixture
def sample_ohlcv_data():
    """Sample OHLCV data for testing."""
    import pandas as pd
    from datetime import datetime, timedelta

    dates = [datetime(2024, 1, 1, 3, 0) + timedelta(minutes=i) for i in range(60)]
    data = pd.DataFrame(
        {
            "timestamp": dates,
            "open": [100.0 + i * 0.1 for i in range(60)],
            "high": [100.5 + i * 0.1 for i in range(60)],
            "low": [99.5 + i * 0.1 for i in range(60)],
            "close": [100.2 + i * 0.1 for i in range(60)],
            "volume": [1000] * 60,
        }
    )
    return data


@pytest.fixture(autouse=True)
def reset_config():
    """Reset global config cache before each test."""
    import src.config.config_loader as config_module
    config_module._config = None

