from __future__ import annotations

import unittest
from pathlib import Path


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


class MonthlyOssArtifactMigrationTests(unittest.TestCase):
    def test_forward_migration_allows_oss_and_keeps_local_compatibility(self):
        sql = (PROJECT_ROOT / "database" / "016_monthly_report_oss_artifacts.sql").read_text(
            encoding="utf-8"
        )
        self.assertIn("current_database() <> 'booking_test'", sql)
        self.assertIn("COALESCE(workbook_provider, '') NOT IN ('oss', 's3', 'local')", sql)
        self.assertIn("COALESCE(result_provider, '') NOT IN ('oss', 's3', 'local')", sql)
        self.assertIn("CREATE OR REPLACE FUNCTION reporting.validate_monthly_run_publication()", sql)
        self.assertIn("existing private OSS", sql)

    def test_down_migration_refuses_published_nonlocal_artifacts(self):
        sql = (
            PROJECT_ROOT / "database" / "016_monthly_report_oss_artifacts.down.sql"
        ).read_text(encoding="utf-8")
        self.assertIn("artifact.storage_provider <> 'local'", sql)
        self.assertIn("rollback refused", sql)
        self.assertIn("workbook_provider IS DISTINCT FROM 'local'", sql)


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