from __future__ import annotations

"""SDK templates resource tests — mocked HTTP."""

import os
import sys

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from nexure import Nexure, TemplateDetail, TemplateInfo, TemplateList, ValidationError
from pytest_httpx import HTTPXMock

MOCK_TEMPLATE = {
    "id": "credit_underwriting",
    "version": "1.0.0",
    "display_name": "Credit Underwriting Review",
    "description": "Decision brief for credit decisions",
    "industry": "financial_services",
    "author": "nexure",
}


class TestTemplatesList:
    def test_list_returns_template_list(self, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            method="GET",
            url="http://test:8000/v1/templates",
            json={"templates": [MOCK_TEMPLATE]},
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.list()
        nx.close()

        assert isinstance(result, TemplateList)
        assert len(result.templates) == 1
        assert result.templates[0].id == "credit_underwriting"

    def test_list_with_industry_filter(self, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            method="GET",
            url="http://test:8000/v1/templates?industry=financial_services",
            json={"templates": [MOCK_TEMPLATE]},
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.list(industry="financial_services")
        nx.close()

        assert len(result.templates) == 1


class TestTemplatesGet:
    def test_get_returns_detail(self, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            method="GET",
            url="http://test:8000/v1/templates/credit_underwriting",
            json={**MOCK_TEMPLATE, "config": {"template": {"id": "credit_underwriting"}}},
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.get("credit_underwriting")
        nx.close()

        assert isinstance(result, TemplateDetail)
        assert result.id == "credit_underwriting"
        assert "template" in result.config


class TestTemplatesRegister:
    def test_register_returns_template_info(self, httpx_mock: HTTPXMock) -> None:
        httpx_mock.add_response(
            method="POST",
            url="http://test:8000/v1/templates",
            json=MOCK_TEMPLATE,
            status_code=201,
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.register(template_yaml="template:\n  id: test")
        nx.close()

        assert isinstance(result, TemplateInfo)
        assert result.id == "credit_underwriting"


class TestTemplatesUpdate:
    def test_update_returns_template_info(self, httpx_mock: HTTPXMock) -> None:
        updated = {**MOCK_TEMPLATE, "version": "1.1.0", "display_name": "Updated Template"}
        httpx_mock.add_response(
            method="PUT",
            url="http://test:8000/v1/templates/credit_underwriting",
            json=updated,
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.update(
            "credit_underwriting", template_yaml="template:\n  id: credit_underwriting"
        )
        nx.close()

        assert isinstance(result, TemplateInfo)
        assert result.version == "1.1.0"
        assert result.display_name == "Updated Template"


class TestTemplatesRegisterFilepath:
    def test_register_from_filepath(self, httpx_mock: HTTPXMock, tmp_path) -> None:
        yaml_content = "template:\n  id: my_template\n  version: '1.0'"
        yaml_file = tmp_path / "my_template.yaml"
        yaml_file.write_text(yaml_content)

        httpx_mock.add_response(
            method="POST",
            url="http://test:8000/v1/templates",
            json=MOCK_TEMPLATE,
            status_code=201,
        )

        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        result = nx.templates.register(filepath=str(yaml_file))
        nx.close()

        assert isinstance(result, TemplateInfo)

    def test_register_both_raises(self) -> None:
        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        with pytest.raises(ValidationError):
            nx.templates.register(template_yaml="template:\n  id: x", filepath="/tmp/x.yaml")
        nx.close()

    def test_register_neither_raises(self) -> None:
        nx = Nexure(api_key="test_key", base_url="http://test:8000/v1")
        with pytest.raises(ValidationError):
            nx.templates.register()
        nx.close()
