from __future__ import annotations

import unittest
from pathlib import Path


PROJECT_ROOT = Path(__file__).resolve().parents[1]
STATIC_ROOT = PROJECT_ROOT / "arr_web" / "static"


class HistoryMonthUiContractTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.html = (STATIC_ROOT / "index.html").read_text(encoding="utf-8")
        cls.script = (STATIC_ROOT / "app.js").read_text(encoding="utf-8")
        cls.styles = (STATIC_ROOT / "styles.css").read_text(encoding="utf-8")
        cls.i18n = (STATIC_ROOT / "i18n.js").read_text(encoding="utf-8")

    def test_all_three_history_surfaces_have_independent_month_controls(self) -> None:
        for scope in ("jobs", "monthly", "company"):
            with self.subTest(scope=scope):
                self.assertIn(f'id="{scope}-history-month"', self.html)
                self.assertIn(f'id="{scope}-month-previous"', self.html)
                self.assertIn(f'id="{scope}-month-next"', self.html)
                self.assertIn(f'id="{scope}-month-current"', self.html)
        self.assertIn('<span>生成月份</span>', self.html)
        self.assertIn('id="company-report-month"', self.html)
        self.assertNotEqual(
            self.html.index('id="company-report-month"'),
            self.html.index('id="company-history-month"'),
        )

    def test_latest_non_empty_month_is_loaded_by_record_type(self) -> None:
        self.assertIn('api("/api/history-months")', self.script)
        self.assertIn('jobs: { stateKey: "jobsMonth", countKey: "daily_count" }', self.script)
        self.assertIn('monthly: { stateKey: "monthlyMonth", countKey: "monthly_count" }', self.script)
        self.assertIn('company: { stateKey: "companyHistoryMonth", countKey: "company_count" }', self.script)
        self.assertIn('latestHistoryMonth(scope) || localMonth()', self.script)
        self.assertIn('encodeURIComponent(historyMonth("jobs"))', self.script)
        self.assertIn('const month = historyMonth("monthly")', self.script)
        self.assertIn('const month = historyMonth("company")', self.script)

    def test_history_navigation_has_localized_empty_and_responsive_states(self) -> None:
        for key in (
            "history.business_month",
            "history.view_month",
            "history.generation_month",
            "history.previous_month",
            "history.next_month",
            "history.current_month",
            "history.daily_empty",
            "history.monthly_empty",
            "history.company_empty",
            "history.view_recent",
        ):
            self.assertIn(f'"{key}"', self.i18n)
        self.assertIn(".history-month-control", self.styles)
        self.assertIn(".history-empty-jump", self.styles)
        self.assertIn(".history-heading-actions { align-items: flex-start; flex-direction: column; }", self.styles)

    def test_company_generation_month_change_does_not_reload_history(self) -> None:
        marker = '$("#company-report-month").addEventListener("change",'
        start = self.script.index(marker)
        end = self.script.index("});", start) + 3
        handler = self.script[start:end]
        self.assertIn("updateCompanyReportControls()", handler)
        self.assertNotIn("loadCompanyReportHistory", handler)
        self.assertNotIn("companyHistoryMonth", handler)


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