"""
Pytest fixtures for error pattern and comparison tests.
"""

import pytest
import tempfile
import pandas as pd
from pathlib import Path


@pytest.fixture
def sample_results_df():
    """Create a sample DataFrame mimicking *_results.xlsx format."""
    return pd.DataFrame({
        "index": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "question": [
            "What is shown?",
            "Is this red?",
            "How many?",
            "What text?",
            "What number?",
            "What brand?",
            "Is it open?",
            "What color?",
            "How much?",
            "What animal?",
        ],
        "answer": [
            "cat",
            "yes",
            "5",
            "STOP",
            "42%",
            "Coca-Cola",
            "no",
            "blue",
            "100 dollars",
            "dog",
        ],
        "prediction": [
            "",  # empty_or_null
            "no",  # yes_no_flip
            "6",  # numeric_mismatch
            "The sign says STOP in red",  # noise_coverage
            "42 percent",  # unit_mismatch
            "Coca Cola",  # case_punct_only or char_level_typo
            "yes",  # yes_no_flip
            "red",  # completely_wrong
            "100",  # numeric format or partial
            "cat cat cat cat cat",  # repetition_loop
        ],
        "eval_score": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        "image_path": [f"/images/{i}.jpg" for i in range(10)],
    })


@pytest.fixture
def temp_experiment_dir(tmp_path):
    """Create a temporary experiment directory structure."""
    exp_name = "test_experiment"
    vlmeval_dir = tmp_path / "VLMEvalKit_local" / "output_models" / exp_name
    eval_dir = vlmeval_dir / "eval_vlmeval" / "checkpoint-1000"
    benchmark_dir = eval_dir / "TextVQA_VAL"
    benchmark_dir.mkdir(parents=True)
    
    return {
        "root": tmp_path,
        "exp_name": exp_name,
        "vlmeval_dir": vlmeval_dir,
        "eval_dir": eval_dir,
        "benchmark_dir": benchmark_dir,
    }


@pytest.fixture
def temp_xlsx_file(temp_experiment_dir, sample_results_df):
    """Create a temporary XLSX results file."""
    benchmark_dir = temp_experiment_dir["benchmark_dir"]
    xlsx_path = benchmark_dir / "test_model_TextVQA_VAL_results.xlsx"
    sample_results_df.to_excel(xlsx_path, index=False)
    return xlsx_path

