"""Tests for starter kit generator."""

import json
import tempfile
from pathlib import Path

from apiposture_pro.enterprise.starter_kits.generator import emit_starter_kit


def test_soc2_generates_all_five_files():
    with tempfile.TemporaryDirectory() as tmp:
        base = Path(tmp)
        emit_starter_kit("soc2", base)
        assert (base / ".apiposture-policy.json").exists()
        assert (base / "sample-report.html").exists()
        assert (base / "github-actions" / "apiposture-pr-gate.yml").exists()
        assert (base / "github-actions" / "apiposture-weekly-report.yml").exists()
        assert (base / "README.md").exists()


def test_iso27001_generates_all_five_files():
    with tempfile.TemporaryDirectory() as tmp:
        base = Path(tmp)
        emit_starter_kit("iso27001", base)
        assert (base / ".apiposture-policy.json").exists()
        assert (base / "sample-report.html").exists()
        assert (base / "github-actions" / "apiposture-pr-gate.yml").exists()
        assert (base / "github-actions" / "apiposture-weekly-report.yml").exists()
        assert (base / "README.md").exists()


def test_returns_list_of_five_paths():
    with tempfile.TemporaryDirectory() as tmp:
        result = emit_starter_kit("soc2", Path(tmp))
        assert isinstance(result, list)
        assert len(result) == 5
        for p in result:
            assert isinstance(p, Path)
            assert p.exists()


def test_soc2_policy_has_ap101_zero_tolerance():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        policy = json.loads(Path(tmp, ".apiposture-policy.json").read_text())
        assert "AP101" in policy["zero_tolerance"]
        assert "AP201" in policy["zero_tolerance"]


def test_iso27001_policy_has_ap102_zero_tolerance():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("iso27001", Path(tmp))
        policy = json.loads(Path(tmp, ".apiposture-policy.json").read_text())
        assert "AP102" in policy["zero_tolerance"]


def test_soc2_policy_fail_on_high():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        policy = json.loads(Path(tmp, ".apiposture-policy.json").read_text())
        assert policy["fail_on"] == "high"


def test_sample_report_html_has_scope_disclaimer():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        html = Path(tmp, "sample-report.html").read_text()
        assert "subset" in html.lower() or "scope" in html.lower()


def test_soc2_readme_mentions_soc2():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        readme = Path(tmp, "README.md").read_text()
        assert "SOC 2" in readme or "soc2" in readme.lower()


def test_iso27001_readme_mentions_iso():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("iso27001", Path(tmp))
        readme = Path(tmp, "README.md").read_text()
        assert "ISO 27001" in readme or "iso27001" in readme.lower()


def test_pr_gate_yaml_has_pull_request_trigger():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        yml = (Path(tmp) / "github-actions" / "apiposture-pr-gate.yml").read_text()
        assert "pull_request" in yml


def test_weekly_report_yaml_has_cron_schedule():
    with tempfile.TemporaryDirectory() as tmp:
        emit_starter_kit("soc2", Path(tmp))
        yml = (Path(tmp) / "github-actions" / "apiposture-weekly-report.yml").read_text()
        assert "cron" in yml or "schedule" in yml


def test_creates_output_dir_if_missing():
    with tempfile.TemporaryDirectory() as tmp:
        new_dir = Path(tmp) / "nested" / "output"
        assert not new_dir.exists()
        emit_starter_kit("soc2", new_dir)
        assert new_dir.exists()
        assert (new_dir / ".apiposture-policy.json").exists()
