from pathlib import Path
import tempfile
import unittest

from nightshift.artifacts import ArtifactStore
from nightshift.reports import ReportGenerator
from nightshift.stages import StageResult
from nightshift.tasks import parse_tasks


TASK_MD = """# Tasks

- [ ] TASK-001: Report results

Description:
Write summaries.

Acceptance Criteria:
- Final notes explain status
- Run summary includes artifacts
"""


class ReportGeneratorTests(unittest.TestCase):
    def test_writes_final_notes_stage_results_and_run_summary(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            artifacts = ArtifactStore(root, ".nightshift", run_id="test-run")
            reporter = ReportGenerator(root, artifacts)
            task = parse_tasks(TASK_MD)[0]
            context_out = artifacts.write_stage_output(task.id, "context-out.md", "# Context Out\n")

            report = reporter.write_reports(
                task,
                "complete",
                "done",
                1,
                [
                    StageResult(
                        stage_id="test",
                        status="pass",
                        reason="ok",
                        output_path=".nightshift/runs/test-run/tasks/TASK-001/test-output.txt",
                    )
                ],
                context_out_path=context_out,
            )

            self.assertTrue(report.final_notes_path.exists())
            self.assertTrue(report.stage_results_path.exists())
            self.assertTrue(report.run_summary_path.exists())
            self.assertIn("Retry count: 1", report.final_notes_path.read_text(encoding="utf-8"))
            self.assertIn("test", report.stage_results_path.read_text(encoding="utf-8"))
            self.assertIn("Final notes", report.run_summary_path.read_text(encoding="utf-8"))


if __name__ == "__main__":
    unittest.main()
