"""Tests for Docker Compose linter."""

from yaml_doctor.analyzers.compose import lint_compose


class TestComposeLinter:
    def _compose(self, **svc_overrides):
        doc = {
            "services": {
                "web": {
                    "image": "myapp:1.0",
                    "restart": "unless-stopped",
                    "healthcheck": {"test": ["CMD", "curl", "-f", "http://localhost"]},
                    "logging": {"driver": "json-file"},
                }
            }
        }
        for k, v in svc_overrides.items():
            if v is None:
                doc["services"]["web"].pop(k, None)
            else:
                doc["services"]["web"][k] = v
        return doc

    def test_clean(self):
        doc = self._compose()
        findings = lint_compose([doc])
        # May have DC006/DC011 (low/info)
        high_crit = [f for f in findings if f.rule.severity.value in ("critical", "high")]
        assert len(high_crit) == 0

    def test_dc001_latest_tag(self):
        doc = self._compose(image="nginx:latest")
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC001" for f in findings)

    def test_dc002_privileged(self):
        doc = self._compose(privileged=True)
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC002" for f in findings)

    def test_dc003_no_restart(self):
        doc = self._compose(restart=None)
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC003" for f in findings)

    def test_dc004_no_healthcheck(self):
        doc = self._compose(healthcheck=None)
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC004" for f in findings)

    def test_dc005_secrets_in_env(self):
        doc = self._compose(environment=["AWS_SECRET_KEY=AKIAIOSFODNN7EXAMPLE"])
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC005" for f in findings)

    def test_dc005_secrets_dict(self):
        doc = self._compose(environment={"PASSWORD": "secret123"})
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC005" for f in findings)

    def test_dc007_exposed_all(self):
        doc = self._compose(ports=["0.0.0.0:8080:80"])
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC007" for f in findings)

    def test_dc008_version_key(self):
        doc = self._compose()
        doc["version"] = "3"
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC008" for f in findings)

    def test_dc009_host_volume(self):
        doc = self._compose(volumes=["/var/run/docker.sock:/var/run/docker.sock"])
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC009" for f in findings)

    def test_dc012_host_network(self):
        doc = self._compose(network_mode="host")
        findings = lint_compose([doc])
        assert any(f.rule.rule_id == "DC012" for f in findings)

    def test_empty_docs(self):
        assert lint_compose([]) == []
        assert lint_compose([None]) == []

    def test_no_services(self):
        doc = {"version": "3"}
        findings = lint_compose([doc])
        assert len(findings) == 0 or all(f.rule.rule_id == "DC008" for f in findings)
