"""Tests for agent database query primitives."""

import pytest
import json

pytest.importorskip("duckdb")


class TestQueryMotions:
    def test_returns_motion_rows(self, tmp_duckdb_path):
        from agent_tools.database import query_motions

        result = query_motions(tmp_duckdb_path)
        assert isinstance(result, list)

    def test_respects_limit(self, tmp_duckdb_path):
        from agent_tools.database import query_motions

        result = query_motions(tmp_duckdb_path, limit=5)
        assert len(result) <= 5

    def test_empty_db_returns_empty_list(self, tmp_duckdb_path):
        from agent_tools.database import query_motions

        result = query_motions(tmp_duckdb_path)
        assert result == []


class TestQueryVotes:
    def test_returns_vote_counts(self, tmp_duckdb_path):
        from agent_tools.database import query_votes

        result = query_votes(tmp_duckdb_path, motion_id=1)
        assert isinstance(result, list)

    def test_filters_by_party(self, tmp_duckdb_path):
        from agent_tools.database import query_votes

        result = query_votes(tmp_duckdb_path, motion_id=1, party="VVD")
        assert isinstance(result, list)


class TestQuerySvdVectors:
    def test_returns_vectors(self, tmp_duckdb_path):
        from agent_tools.database import query_svd_vectors

        result = query_svd_vectors(tmp_duckdb_path, window_id="current_parliament")
        assert isinstance(result, list)

    def test_filters_by_entity_type(self, tmp_duckdb_path):
        from agent_tools.database import query_svd_vectors

        result = query_svd_vectors(
            tmp_duckdb_path, window_id="current_parliament", entity_type="mp"
        )
        assert isinstance(result, list)


class TestQueryPartyPositions:
    def test_returns_party_scores(self, tmp_duckdb_path):
        from agent_tools.database import query_party_positions

        result = query_party_positions(tmp_duckdb_path, window_id="current_parliament")
        assert isinstance(result, list)


class TestQueryPipelineStatus:
    def test_returns_status_dict(self, tmp_duckdb_path):
        from agent_tools.database import query_pipeline_status

        result = query_pipeline_status(tmp_duckdb_path)
        assert isinstance(result, dict)
        assert "motion_count" in result
        assert "latest_motion_date" in result
        assert "svd_window_count" in result
        assert "healthy" not in result


class TestCrudTools:
    def test_create_motion_returns_id(self, tmp_duckdb_path):
        from agent_tools.database import create_motion

        result = create_motion(
            tmp_duckdb_path,
            title="Test Motion",
            description="A test motion",
            date="2024-06-01",
            policy_area="Test",
        )
        assert isinstance(result, dict)
        assert "motion_id" in result or "error" in result

    def test_update_motion_changes_field(self, tmp_duckdb_path):
        from agent_tools.database import create_motion, update_motion

        created = create_motion(
            tmp_duckdb_path,
            title="Original",
            description="Original desc",
            date="2024-06-01",
        )
        if "error" in created:
            pytest.skip("create_motion not supported by schema")
        motion_id = created["motion_id"]

        result = update_motion(
            tmp_duckdb_path,
            motion_id=motion_id,
            title="Updated",
        )
        assert isinstance(result, dict)
        assert "updated" in result or "error" in result

    def test_delete_report_removes_file(self, tmp_path):
        from agent_tools.database import delete_report

        report_path = tmp_path / "test_report.md"
        report_path.write_text("# Test Report\n")

        result = delete_report(str(report_path))
        assert result.get("deleted") is True
        assert not report_path.exists()
