from __future__ import annotations

import unittest
from pathlib import Path


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


class MonthlyReportMigrationTests(unittest.TestCase):
    def test_forward_migration_has_manifest_guards_and_verified_backfill(self):
        sql = (PROJECT_ROOT / "database" / "006_report_channel_manifest.sql").read_text(
            encoding="utf-8"
        )
        for required in (
            "CREATE TABLE finance.report_channel_manifest",
            "report_channel_manifest_order_unique",
            "validate_report_channel_manifest_parent",
            "protect_published_report_channel_manifest",
            "validate_monthly_report_channel_manifest",
            "active monthly report requires a continuous channel manifest",
            "592fee9b1e3e8290ed1e115d7eb213f09e780fa2bda08f6e270a34e7eaedd09f",
            "('T- HANATOUR TD CO., L', 6, 24)",
        ):
            with self.subTest(required=required):
                self.assertIn(required, sql)
        self.assertEqual(sql.count("INSERT INTO finance.report_channel_manifest"), 1)

    def test_down_migration_refuses_when_manifest_data_exists(self):
        sql = (
            PROJECT_ROOT / "database" / "006_report_channel_manifest.down.sql"
        ).read_text(encoding="utf-8")
        self.assertIn("refusing rollback: migration 006 data exists", sql)
        self.assertIn("DROP TABLE finance.report_channel_manifest", sql)


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