from __future__ import annotations

import importlib.util
import json
import re
import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def load_module(name: str, relative_path: str):
    spec = importlib.util.spec_from_file_location(name, ROOT / relative_path)
    module = importlib.util.module_from_spec(spec)
    assert spec and spec.loader
    spec.loader.exec_module(module)
    return module


validator = load_module("validate_resume_system", "resume_builder/helpers/validate_resume_system.py")
length_helper = load_module("char_count", "resume_builder/helpers/char_count.py")


class CanonicalSystemTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.claims = json.loads(
            (ROOT / "resume_builder/canonical/claims.json").read_text(encoding="utf-8")
        )
        cls.history = json.loads(
            (ROOT / "resume_builder/canonical/historical_outputs.json").read_text(encoding="utf-8")
        )

    def test_canonical_and_workflow_checks_have_no_errors(self) -> None:
        errors, _warnings = validator.canonical_checks(self.claims, self.history)
        errors.extend(validator.workflow_checks())
        self.assertEqual([], errors)

    def test_claim_and_employment_ids_are_unique(self) -> None:
        for collection in ("employment", "claims"):
            ids = [item["id"] for item in self.claims[collection]]
            self.assertEqual(len(ids), len(set(ids)), collection)

    def test_swiss_work_authorization_is_verified(self) -> None:
        identity = self.claims["identity"]
        self.assertEqual("B residence permit", identity["swiss_permit"])
        self.assertIn("no visa or employer sponsorship required", identity["swiss_work_authorization"])
        self.assertFalse(identity["swiss_permit"].startswith("UNVERIFIED"))

    def test_historical_outputs_are_classified_once(self) -> None:
        folders = []
        for category in ("unsafe_do_not_reuse", "historical_revalidate"):
            folders.extend(
                item["folder"] if isinstance(item, dict) else item
                for item in self.history[category]
            )
        self.assertEqual(len(folders), len(set(folders)))

    def test_templates_pass_document_claim_scan(self) -> None:
        for relative in (
            "resume_builder/templates/resume_template.tex",
            "resume_builder/templates/coverletter_template.tex",
        ):
            self.assertEqual([], validator.scan_document(ROOT / relative, self.claims), relative)


class WorkflowPolicyTests(unittest.TestCase):
    def test_skills_use_minimal_valid_frontmatter(self) -> None:
        for skill_file in sorted((ROOT / ".agents/skills").glob("*/SKILL.md")):
            text = skill_file.read_text(encoding="utf-8")
            match = re.match(r"^---\n(.*?)\n---", text, re.DOTALL)
            self.assertIsNotNone(match, skill_file)
            fields = {}
            for line in match.group(1).splitlines():
                key, separator, value = line.partition(":")
                self.assertEqual(":", separator, skill_file)
                fields[key.strip()] = value.strip()
            self.assertEqual({"name", "description"}, set(fields), skill_file)
            self.assertRegex(fields["name"], r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
            self.assertLessEqual(len(fields["name"]), 64)
            self.assertTrue(fields["description"])
            self.assertLessEqual(len(fields["description"]), 1024)
            self.assertNotRegex(fields["description"], r"[<>]")

    def test_top_level_instructions_expose_new_controls(self) -> None:
        for filename in ("AGENTS.md", "CLAUDE.md"):
            text = (ROOT / filename).read_text(encoding="utf-8")
            for phrase in (
                "canonical/claims.json",
                "Evidence Fit",
                "Document Quality",
                "Channel Strength",
                "no character targets",
                "cover letter is conditional",
            ):
                self.assertIn(phrase.lower(), text.lower(), f"{filename}: {phrase}")

    def test_resume_template_has_conventional_hierarchy(self) -> None:
        text = (ROOT / "resume_builder/templates/resume_template.tex").read_text(encoding="utf-8")
        self.assertIn("Professional Experience", text)
        self.assertIn("Technical Skills", text)
        self.assertIn("Education", text)
        self.assertNotIn("FLIPPED", text)
        self.assertNotIn("Selected Publications", text)

    def test_length_helper_is_diagnostic_not_target_based(self) -> None:
        source = (ROOT / "resume_builder/helpers/char_count.py").read_text(encoding="utf-8")
        self.assertNotIn("classify_bullet", source)
        rendered, characters, words, clauses = length_helper.diagnose(
            r"\item Built governed data products within the company-wide Data Mesh."
        )
        self.assertEqual("Built governed data products within the company-wide Data Mesh.", rendered)
        self.assertGreater(characters, 0)
        self.assertEqual(9, words)
        self.assertEqual(1, clauses)


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