"""Unit tests for backup ZIP helpers (no database)."""

import unittest

from src.initiative_db.backup_naming import backup_zip_attachment_filename, official_form_pdf_backup_zip_path
from src.initiative_db.application_storage import (
    EVIDENCE_ROLE_RESEARCH,
    STORAGE_FILESYSTEM,
    STORAGE_MINIO_ATTACHMENTS,
    STORAGE_MINIO_EXPORTS,
    effective_storage_kind,
)


class OfficialFormPdfZipPathTests(unittest.TestCase):
    def test_trang_bia_fields(self) -> None:
        obm = {
            "TRANG BÌA": {
                "Tên sáng kiến (Tiếng Việt)": "  Khảo sát thảo dược  ",
                "Tác giả/nhóm tác giả sáng kiến": "Lê Thị A",
                "Thông tin liên hệ (Điện thoại, Email)": "0909, a.b@ump.edu.vn",
            }
        }
        p = official_form_pdf_backup_zip_path(obm)
        self.assertEqual(p, "submitted/Khảo_sát_thảo_dược_Lê_Thị_A_a.b@ump.edu.vn.pdf")

    def test_no_trang_bia_returns_none(self) -> None:
        self.assertIsNone(official_form_pdf_backup_zip_path({}))
        self.assertIsNone(official_form_pdf_backup_zip_path({"OTHER": {}}))

    def test_empty_cover_returns_none(self) -> None:
        self.assertIsNone(
            official_form_pdf_backup_zip_path({"TRANG BÌA": {"Tên sáng kiến (Tiếng Việt)": "   "}})
        )


class BackupZipFilenameTests(unittest.TestCase):
    def test_email_local_part_and_sub_id(self) -> None:
        self.assertEqual(
            backup_zip_attachment_filename(
                owner_email="  nguyen.van.a@ump.edu.vn ",
                owner_full_name="Nguyễn Văn A",
                public_application_id="sub-deadbeef",
            ),
            "nguyen.van.a_sub-deadbeef.zip",
        )

    def test_fallback_name_when_no_email(self) -> None:
        fn = backup_zip_attachment_filename(
            owner_email=None,
            owner_full_name="  Lê Thị B  ",
            public_application_id="sub-001",
        )
        self.assertTrue(fn.endswith("_sub-001.zip"))
        self.assertIn("Lê", fn)

    def test_applicant_fallback(self) -> None:
        self.assertEqual(
            backup_zip_attachment_filename(
                owner_email="",
                owner_full_name="",
                public_application_id="sub-x",
            ),
            "applicant_sub-x.zip",
        )


class EffectiveStorageKindTests(unittest.TestCase):
    def test_full_pdf_minio_key(self) -> None:
        self.assertEqual(
            effective_storage_kind("full_pdf", "initiatives/abcd/2025/01/x-file.pdf", None),
            STORAGE_MINIO_EXPORTS,
        )

    def test_full_pdf_filesystem(self) -> None:
        self.assertEqual(
            effective_storage_kind("full_pdf", "/submitted-initiatives/sub-abc.pdf", None),
            STORAGE_FILESYSTEM,
        )

    def test_evidence_attachments(self) -> None:
        self.assertEqual(
            effective_storage_kind(
                EVIDENCE_ROLE_RESEARCH,
                "initiatives/abcd/2025/01/x-file.pdf",
                None,
            ),
            STORAGE_MINIO_ATTACHMENTS,
        )

    def test_respects_declared(self) -> None:
        self.assertEqual(
            effective_storage_kind("full_pdf", "/any", STORAGE_MINIO_EXPORTS),
            STORAGE_MINIO_EXPORTS,
        )


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