from __future__ import annotations

import json
import tempfile
from pathlib import Path

import pytest

import sync_state_machine.domain  # noqa: F401
from sync_state_machine.pipeline.copy_data_pipeline import CopyDataPipeline


@pytest.fixture
def source_data_dir() -> Path:
    temp_dir = Path(tempfile.mkdtemp(prefix="sync_state_machine_source_"))

    project_data = [
        {
            "_id": f"proj-{i}",
            "type": "project",
            "name": f"Test Project {i}",
            "code": f"P{i:03d}",
            "status": "active",
            "progress_type": "C",
            "project_type": "onshore_wind",
        }
        for i in range(1, 4)
    ]
    (temp_dir / "project_1.jsonl").write_text(
        "\n".join(json.dumps(d, ensure_ascii=False) for d in project_data),
        encoding="utf-8",
    )

    contract_data = [
        {
            "_id": f"contract-{i}",
            "type": "contract",
            "name": f"Test Contract {i}",
            "code": f"C{i:03d}",
            "total_price": i * 10000,
        }
        for i in range(1, 6)
    ]
    (temp_dir / "contract_1.jsonl").write_text(
        "\n".join(json.dumps(d, ensure_ascii=False) for d in contract_data),
        encoding="utf-8",
    )

    return temp_dir


@pytest.fixture
def target_data_dir() -> Path:
    return Path(tempfile.mkdtemp(prefix="sync_state_machine_target_"))


@pytest.mark.asyncio
async def test_copy_pipeline_jsonl_end_to_end(source_data_dir: Path, target_data_dir: Path) -> None:
    pipeline = CopyDataPipeline(
        source_dir=source_data_dir,
        target_dir=target_data_dir,
        node_types=["project", "contract"],
    )

    stats = await pipeline.run()

    assert stats["loaded"] == 8
    assert stats["copied"] == 8
    assert stats["saved"] == 8
    assert stats["failed"] == 0

    output_files = {p.name for p in target_data_dir.glob("*.jsonl")}
    assert "project_1.jsonl" in output_files
    assert "contract_1.jsonl" in output_files
