import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]


def source(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


class InvoiceSettingsTests(unittest.TestCase):
    def test_backend_exposes_invoice_template_payment_methods_and_logo_config(self) -> None:
        config_api = source("backend/app/api/config.py")

        self.assertIn('"/invoice-template"', config_api)
        self.assertIn('"/payment-methods"', config_api)
        self.assertIn('"/system"', config_api)
        self.assertIn('"/system/logo"', config_api)
        self.assertIn("InvoiceTemplateConfigResponse", config_api)
        self.assertIn("PaymentMethodsConfigResponse", config_api)
        self.assertIn("SystemSettingsResponse", config_api)
        self.assertIn("system_name", config_api)
        self.assertIn("logo_url", config_api)
        self.assertIn("st_mtime_ns", config_api)
        self.assertIn("UploadFile", config_api)
        self.assertIn("FileResponse", config_api)
        self.assertIn("email_subject_template", config_api)
        self.assertIn("email_body_template", config_api)
        self.assertIn("invoice_email_subject_template", config_api)
        self.assertIn("invoice_email_body_template", config_api)

    def test_invoice_pdf_uses_configurable_template(self) -> None:
        pdf_service = source("backend/app/services/invoice_pdf.py")

        self.assertIn("invoice_template_html", pdf_service)
        self.assertIn("DEFAULT_INVOICE_TEMPLATE", pdf_service)
        self.assertIn("StrictUndefined", pdf_service)
        self.assertIn("DEFAULT_INVOICE_EMAIL_SUBJECT_TEMPLATE", pdf_service)
        self.assertIn("DEFAULT_INVOICE_EMAIL_BODY_TEMPLATE", pdf_service)
        self.assertIn("render_invoice_email", pdf_service)

    def test_frontend_exposes_invoice_settings_page_payment_modal_and_brand_logo(self) -> None:
        router = source("frontend/src/router/index.js")
        nav = source("frontend/src/components/AppLayout.vue")
        detail = source("frontend/src/pages/InvoiceDetailPage.vue")
        settings = source("frontend/src/pages/InvoiceSettingsPage.vue")
        finance = source("frontend/src/pages/FinanceSummaryPage.vue")
        login = source("frontend/src/pages/LoginPage.vue")

        self.assertIn("InvoiceSettings", router)
        self.assertIn("invoice-settings", router)
        self.assertIn("/invoice-settings", nav)
        self.assertIn("systemSettings", nav)
        self.assertIn("system-settings-updated", nav)
        self.assertIn("brand-logo", nav)
        self.assertIn('v-if="!systemSettings.logo_url"', nav)
        self.assertIn("logo_url", nav)
        self.assertIn("showPaymentModal", detail)
        self.assertIn("paymentForm", detail)
        self.assertIn("emailSubjectTemplate", settings)
        self.assertIn("emailBodyTemplate", settings)
        self.assertIn("systemForm", settings)
        self.assertIn("logoPreviewUrl", settings)
        self.assertIn("pendingLogoFile", settings)
        self.assertIn("configApi.uploadSystemLogo", settings)
        self.assertIn("system-settings-updated", settings)
        self.assertIn("gstSettings", settings)
        self.assertIn("brand-logo", login)
        self.assertIn("login-card-logo", login)
        self.assertLess(login.index("login-card-logo"), login.index("<h2>Welcome back</h2>"))
        self.assertIn("width: 180px;", login)
        self.assertIn("height: 72px;", login)
        self.assertIn("text-align: center;", login)
        self.assertIn("logo_url", login)
        self.assertNotIn("12.4k", login)
        self.assertNotIn("Save GST settings", finance)

    def test_logo_file_url_is_cache_busted_and_publicly_readable(self) -> None:
        config_api = source("backend/app/api/config.py")

        self.assertIn('"/api/config/system/logo?v=', config_api)
        self.assertIn("path.stat().st_mtime_ns", config_api)
        logo_get_route = config_api.split('@router.get("/system/logo")', 1)[1].split("# ===== SMTP =====", 1)[0]
        self.assertNotIn('require_permission("settings.read")', logo_get_route)

    def test_login_page_can_read_public_branding_settings(self) -> None:
        config_api = source("backend/app/api/config.py")

        system_get_route = config_api.split('@router.get("/system", response_model=SystemSettingsResponse)', 1)[1].split('@router.put("/system"', 1)[0]
        self.assertNotIn('require_permission("settings.read")', system_get_route)
        self.assertIn('require_permission("settings.write")', config_api)


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