import os
import sys
import tempfile
import shutil
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))

from pygeai.templates.engine import TemplateEngine
from pygeai.evaluation.dataset.clients import EvaluationDatasetClient


def test_evaluation_basic_template():
    """
    Test evaluation-basic template by:
    1. Generating the template files
    2. Verifying all files exist
    3. Testing dataset creation workflow
    """
    test_dir = tempfile.mkdtemp(prefix="pygeai_eval_test_")
    dataset_id = None

    print(f"Testing in: {test_dir}")

    try:
        print("[STEP 1] Generating evaluation-basic template...")
        engine = TemplateEngine()

        params = {
            "name": "test_evaluation",
            "description": "Test evaluation workflow",
            "assistant_name": "test_rag_assistant",
        }

        generated_files = engine.generate("evaluation-basic", Path(test_dir), params)

        assert len(generated_files) == 4, (
            f"Expected 4 files, got {len(generated_files)}"
        )
        print("[PASS] Template generated successfully")

        create_dataset = Path(test_dir) / "create_dataset.py"
        create_plan = Path(test_dir) / "create_evaluation_plan.py"
        run_eval = Path(test_dir) / "run_evaluation.py"
        readme = Path(test_dir) / "README.md"

        assert create_dataset.exists(), "create_dataset.py not found"
        assert create_plan.exists(), "create_evaluation_plan.py not found"
        assert run_eval.exists(), "run_evaluation.py not found"
        assert readme.exists(), "README.md not found"

        print("[PASS] All expected files exist")

        print("[STEP 2] Testing dataset creation...")
        client = EvaluationDatasetClient()

        dataset = client.create_dataset(
            dataset_name=f"test_eval_{os.urandom(4).hex()}",
            dataset_description="Test evaluation dataset",
            dataset_type="T",
            dataset_active=True,
            rows=[],
        )

        dataset_id = dataset.get("dataSetId")

        if not dataset_id:
            print("[FAIL] Could not create dataset")
            sys.exit(1)

        print(f"[PASS] Dataset created: {dataset_id}")

        print("[STEP 3] Adding test rows...")
        row1 = client.create_dataset_row(
            dataset_id=dataset_id,
            row={
                "dataSetRowInput": "Test question?",
                "dataSetRowExpectedAnswer": "Test answer",
                "dataSetRowContextDocument": "Test context",
            },
        )

        row1_id = row1.get("dataSetRowId")
        if not row1_id:
            print("[FAIL] Could not create dataset row")
            sys.exit(1)

        print(f"[PASS] Dataset row created: {row1_id}")

        print("[STEP 4] Verifying dataset contents...")
        dataset_data = client.get_dataset(dataset_id=dataset_id)
        rows = dataset_data.get("rows", [])

        if len(rows) != 1:
            print(f"[FAIL] Expected 1 row, got {len(rows)}")
            sys.exit(1)

        print("[PASS] Dataset contains expected rows")

        print(
            "\n[PASS] ALL TESTS PASSED - evaluation-basic template is working correctly!"
        )

    except Exception as e:
        print(f"[FAIL] Test failed with error: {e}")
        import traceback

        traceback.print_exc()
        sys.exit(1)

    finally:
        print("\nCleaning up...")
        if dataset_id:
            try:
                client = EvaluationDatasetClient()
                client.delete_dataset(dataset_id=dataset_id)
                print(f"Deleted test dataset: {dataset_id}")
            except Exception as e:
                print(f"Warning: Could not delete dataset: {e}")

        if os.path.exists(test_dir):
            shutil.rmtree(test_dir)
            print(f"Deleted test directory: {test_dir}")


if __name__ == "__main__":
    test_evaluation_basic_template()
