from __future__ import annotations

from io import BytesIO

import pandas as pd
import pytest

from services.importers import ImportResult, load_project_imports, load_scenario_imports
from schemas.imports import ProjectImportRow, ScenarioImportRow


def test_load_project_imports_from_csv() -> None:
    csv_content = (
        "name,location,operation_type,description\n"
        "Project A,Chile,open pit,First project\n"
        "Project B,,underground,Second project\n"
    )
    stream = BytesIO(csv_content.encode("utf-8"))

    result = load_project_imports(stream, "projects.csv")

    assert isinstance(result, ImportResult)
    assert len(result.rows) == 2
    assert not result.errors
    first = result.rows[0]
    assert first.row_number == 2
    assert isinstance(first.data, ProjectImportRow)
    assert first.data.name == "Project A"
    assert first.data.operation_type.value == "open_pit"
    second = result.rows[1]
    assert second.row_number == 3
    assert isinstance(second.data, ProjectImportRow)
    assert second.data.location is None


def test_load_scenario_imports_from_excel() -> None:
    df = pd.DataFrame(
        [
            {
                "project_name": "Project A",
                "name": "Scenario 1",
                "status": "Active",
                "start_date": "2025-01-01",
                "end_date": "2025-12-31",
                "discount_rate": "7.5%",
                "currency": "usd",
                "primary_resource": "Electricity",
            }
        ]
    )
    buffer = BytesIO()
    df.to_excel(buffer, index=False)
    buffer.seek(0)

    result = load_scenario_imports(buffer, "scenarios.xlsx")

    assert len(result.rows) == 1
    assert not result.errors
    row = result.rows[0]
    assert row.row_number == 2
    assert isinstance(row.data, ScenarioImportRow)
    assert row.data.status.value == "active"
    assert row.data.currency == "USD"
    assert row.data.discount_rate == pytest.approx(7.5)


def test_import_errors_include_row_numbers() -> None:
    csv_content = "name,operation_type\n,open pit\n"
    stream = BytesIO(csv_content.encode("utf-8"))

    result = load_project_imports(stream, "projects.csv")

    assert len(result.rows) == 0
    assert len(result.errors) == 1
    error = result.errors[0]
    assert error.row_number == 2
    assert error.field == "name"
    assert "required" in error.message
