"""Tests for infra_diagram.output.html_report."""

from __future__ import annotations

import tempfile
from pathlib import Path

from infra_diagram.models import InfraDiagram, InfraEdge, InfraGroup, InfraNode, NodeType, SourceType
from infra_diagram.output.html_report import export_html


def _make_diagram() -> InfraDiagram:
    d = InfraDiagram(title="Test Infra", source_type=SourceType.TERRAFORM)
    d.source_files.append("main.tf")
    d.add_node(InfraNode(id="vpc", name="main-vpc", node_type=NodeType.NETWORK, provider="aws"))
    d.add_node(InfraNode(id="ec2", name="web", node_type=NodeType.VM, provider="aws"))
    d.add_node(InfraNode(id="rds", name="database", node_type=NodeType.DATABASE, provider="aws"))
    d.add_edge(InfraEdge(source="vpc", target="ec2", label="contains"))
    d.add_edge(InfraEdge(source="ec2", target="rds", label="connects"))
    d.add_group(InfraGroup(id="aws", name="AWS", node_ids=["vpc", "ec2", "rds"]))
    return d


class TestExportHtml:
    def test_file_created(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            assert Path(out).exists()

    def test_html_structure(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "<!DOCTYPE html>" in content
            assert "<html" in content
            assert "</html>" in content

    def test_title_included(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "Test Infra" in content

    def test_mermaid_js_included(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "mermaid" in content
            assert "cdn.jsdelivr.net" in content

    def test_components_table(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "main-vpc" in content
            assert "web" in content
            assert "database" in content

    def test_connections_table(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "contains" in content
            assert "connects" in content

    def test_summary_cards(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            diagram = _make_diagram()
            export_html(diagram, out)
            content = Path(out).read_text(encoding="utf-8")
            assert "Components" in content
            assert "Connections" in content

    def test_mermaid_source_block(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "Mermaid Source" in content
            assert "mermaid.live" in content

    def test_dark_theme_css(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "--bg:" in content
            assert "#0f172a" in content

    def test_type_badges(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "type-badge" in content

    def test_empty_diagram(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(InfraDiagram(), out)
            assert Path(out).exists()
            content = Path(out).read_text(encoding="utf-8")
            assert "<!DOCTYPE html>" in content

    def test_subdirectory_created(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "sub" / "dir" / "report.html")
            export_html(_make_diagram(), out)
            assert Path(out).exists()

    def test_return_value(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            result = export_html(_make_diagram(), out)
            assert result == out

    def test_provider_shown(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "aws" in content

    def test_footer(self):
        with tempfile.TemporaryDirectory() as d:
            out = str(Path(d) / "report.html")
            export_html(_make_diagram(), out)
            content = Path(out).read_text(encoding="utf-8")
            assert "infra-diagram" in content
