from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime, timezone


@dataclass(slots=True)
class MarkdownLogger:
    lines: list[str] = field(default_factory=list)

    def header(self, query: str) -> None:
        now = datetime.now(timezone.utc).isoformat()
        self.lines.append("# Execution Report")
        self.lines.append(f"- Query: `{query}`")
        self.lines.append(f"- Timestamp (UTC): `{now}`")
        self.lines.append("")

    def stage(self, name: str, action: str, tool: str) -> None:
        self.lines.append(f"## {name}")
        self.lines.append(f"- Action: {action}")
        self.lines.append(f"- Tool: `{tool}`")

    def info(self, message: str) -> None:
        self.lines.append(f"- Result: {message}")

    def validation(self, passed: bool, message: str) -> None:
        state = "Passed" if passed else "Failed"
        self.lines.append(f"- Validation: {state}")
        self.lines.append(f"- Validation Detail: {message}")
        self.lines.append("")

    def final_success(self, detail_lines: list[str]) -> None:
        self.lines.append("## Final Status")
        self.lines.append("**Execution Complete — Success**")
        for line in detail_lines:
            self.lines.append(f"- {line}")

    def final_error(self, stage: str, code: str, message: str) -> None:
        self.lines.append("## Final Status")
        self.lines.append("**Execution Terminated — Error**")
        self.lines.append(f"- Stage: {stage}")
        self.lines.append(f"- Error Code: `{code}`")
        self.lines.append(f"- Reason: {message}")

    def render(self) -> str:
        return "\n".join(self.lines).strip() + "\n"
