from __future__ import annotations

import unittest
from pathlib import Path


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


class ReportChannelFactsMigrationTests(unittest.TestCase):
    def test_view_is_retained_only_and_privacy_minimized(self):
        sql = (PROJECT_ROOT / "database" / "007_report_channel_facts_view.sql").read_text(
            encoding="utf-8"
        )
        lowered = sql.lower()
        self.assertIn("create view finance.v_report_channel_facts", lowered)
        self.assertIn("where records.outcome = 'retained'", lowered)
        for required in (
            "records.daily_version_id",
            "records.id as daily_record_id",
            "records.no_of_rooms",
            "records.room_category_label",
            "records.total_price",
            "records.channel_key",
        ):
            self.assertIn(required, lowered)
        select_clause = lowered.split("create view", 1)[1].split("from finance.daily_records", 1)[0]
        for forbidden in (
            "full_name",
            "confirmation_no",
            "disp_room_no",
            "res_comment",
            "trace_text",
            "products",
            "source_location",
            "source_worksheet",
            "source_row_no",
        ):
            self.assertNotIn(forbidden, select_clause)

    def test_down_only_drops_the_derived_view(self):
        sql = (
            PROJECT_ROOT / "database" / "007_report_channel_facts_view.down.sql"
        ).read_text(encoding="utf-8").lower()
        self.assertIn("drop view if exists finance.v_report_channel_facts", sql)
        for forbidden in ("drop table", "delete from", "truncate"):
            self.assertNotIn(forbidden, sql)


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