#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit test suite for FreeExile Marketing Infrastructure.
Ensures document existence, hygiene standards, 9-language parity,
and CLI dashboard integrity.
"""

import sys
import unittest
from pathlib import Path

# Add project root and marketing/tools to path
ROOT_DIR = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT_DIR))

from marketing.tools.marketing_hub_manager import (
    MarketingHubManager,
    REQUIRED_LANGUAGES,
    CORE_MARKETING_FILES,
)


class TestMarketingPipeline(unittest.TestCase):
    """Test suite for validating the entire marketing pipeline."""

    def setUp(self) -> None:
        self.manager = MarketingHubManager(ROOT_DIR)

    def test_all_marketing_documents_exist(self) -> None:
        """Every core document defined in CORE_MARKETING_FILES must exist on disk."""
        audit_res = self.manager.audit_documents()
        self.assertTrue(
            audit_res["all_passed"],
            f"Documents failed audit: {audit_res['details']}"
        )
        self.assertEqual(
            audit_res["passed_items"],
            audit_res["total_items"],
            "Not all marketing documents passed the audit."
        )

    def test_markdown_hygiene_line_caps(self) -> None:
        """Markdown files must respect the 500-line hard cap."""
        for item in CORE_MARKETING_FILES:
            file_path = ROOT_DIR / item.target_path
            self.assertTrue(file_path.exists(), f"File missing: {item.target_path}")
            line_count = len(file_path.read_text(encoding="utf-8").splitlines())
            self.assertLessEqual(
                line_count,
                500,
                f"File {item.target_path} exceeds hard cap of 500 lines ({line_count} lines)."
            )

    def test_9_language_parity(self) -> None:
        """Store pitches must feature all 9 target languages."""
        loc_res = self.manager.verify_localization_parity()
        self.assertTrue(
            loc_res["success"],
            f"Missing languages in store pitches: {loc_res.get('missing_languages')}"
        )
        self.assertEqual(
            loc_res["languages_found"],
            len(REQUIRED_LANGUAGES),
            "Expected exactly 9 languages in the store pitches."
        )

    def test_roster_contains_all_agents(self) -> None:
        """Roster must define all 6 dedicated marketing agents."""
        roster_path = ROOT_DIR / "marketing/ROSTER.md"
        content = roster_path.read_text(encoding="utf-8")
        expected_agents = [
            "marketing_lead",
            "viral_content_creator",
            "arpg_outreach_scout",
            "community_builder",
            "storefront_optimizer",
            "global_localization_pr",
        ]
        for agent_id in expected_agents:
            self.assertIn(
                agent_id,
                content,
                f"Agent {agent_id} not found in marketing/ROSTER.md"
            )

    def test_hub_manager_execution(self) -> None:
        """MarketingHubManager must run cleanly and return code 0."""
        code = self.manager.print_dashboard()
        self.assertEqual(code, 0, "Dashboard execution returned non-zero status.")


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