from types import SimpleNamespace
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings, tag
from django.urls import reverse
from waffle.testutils import override_flag

from api.models import EntitlementDefinition, Plan, PlanVersion, PlanVersionEntitlement
from constants.feature_flags import PRICING_FREE_OSS_PLAN
from constants.plans import PLAN_SLUG_BY_LEGACY_CODE, PlanNames
from pages.models import CallToAction


@tag("batch_pages_signals")
class PricingPageCtaCopyTests(TestCase):
    def _set_monthly_task_credits(self, plan_code: str, credits: int) -> None:
        plan_slug = PLAN_SLUG_BY_LEGACY_CODE[plan_code]
        plan, _ = Plan.objects.get_or_create(
            slug=plan_slug,
            defaults={"is_org": False, "is_active": True},
        )
        update_fields: list[str] = []
        if plan.is_org:
            plan.is_org = False
            update_fields.append("is_org")
        if not plan.is_active:
            plan.is_active = True
            update_fields.append("is_active")
        if update_fields:
            plan.save(update_fields=update_fields)

        plan_version = (
            PlanVersion.objects
            .filter(plan=plan, is_active_for_new_subs=True)
            .order_by("-created_at")
            .first()
        )
        if plan_version is None:
            plan_version = PlanVersion.objects.create(
                plan=plan,
                version_code="test-pricing",
                legacy_plan_code=plan_code,
                is_active_for_new_subs=True,
                display_name=plan_slug.title(),
                description="",
                marketing_features=[],
            )
        elif plan_version.legacy_plan_code != plan_code:
            plan_version.legacy_plan_code = plan_code
            plan_version.save(update_fields=["legacy_plan_code"])

        entitlement, _ = EntitlementDefinition.objects.get_or_create(
            key="monthly_task_credits",
            defaults={
                "display_name": "Monthly task credits",
                "description": "Included monthly task credits.",
                "value_type": "int",
                "unit": "credits",
            },
        )
        PlanVersionEntitlement.objects.update_or_create(
            plan_version=plan_version,
            entitlement=entitlement,
            defaults={"value_int": credits},
        )

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_includes_stripe_js(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "https://js.stripe.com/dahlia/stripe.js")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_unauthenticated_pricing_cta_uses_trial_copy(self, mock_get_stripe_settings):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Start 7-day Free Trial")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Start 14-day Free Trial")
        self.assertIsNone(plans[PlanNames.STARTUP]["trial_cancel_text"])
        self.assertIsNone(plans[PlanNames.SCALE]["trial_cancel_text"])

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_renders_signup_modal_config_when_flag_enabled_for_anonymous_users(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_signup_modal", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "gobii-cta-signup-modal-config")
        self.assertContains(response, 'id="cta-signup-modal"')
        self.assertContains(response, 'data-cta-signup-modal="pricing-plan"')

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_enables_cta_tracking_and_plan_analytics_attributes(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'data-analytics-cta-tracking-enabled="true"')
        self.assertContains(response, 'data-analytics-cta-id="pricing_startup_plan"')
        self.assertContains(response, 'data-analytics-placement="pricing_grid"')
        self.assertContains(response, 'data-analytics-intent="select_plan"')
        self.assertContains(response, "Simple Pricing")
        self.assertContains(response, "Choose the plan that fits your team.")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_omits_free_oss_plan_when_flag_disabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag(PRICING_FREE_OSS_PLAN, active=False):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plan_codes = [
            plan["code"]
            for plan in response.context["pricing_plans"]
        ]
        self.assertEqual(plan_codes, [PlanNames.STARTUP, PlanNames.SCALE])
        self.assertFalse(response.context["pricing_grid_has_free_oss_plan"])
        self.assertNotContains(response, "View on GitHub")
        self.assertNotContains(response, 'data-analytics-cta-id="pricing_free_oss_plan"')

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_renders_free_oss_plan_when_flag_enabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag(PRICING_FREE_OSS_PLAN, active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = response.context["pricing_plans"]
        self.assertEqual(
            [plan["code"] for plan in plans],
            ["free_oss", PlanNames.STARTUP, PlanNames.SCALE],
        )
        self.assertTrue(response.context["pricing_grid_has_free_oss_plan"])

        free_plan = plans[0]
        self.assertEqual(free_plan["name"], "Free")
        self.assertEqual(free_plan["desc"], "Self-Hosted Agents")
        self.assertEqual(free_plan["price_label"], "$0")
        self.assertEqual(free_plan["price_amount"], 0)
        self.assertIsNone(free_plan["tasks"])
        self.assertEqual(free_plan["pricing_model"], "Self-hosted, open source")
        self.assertIn("Run on your own computer or server", free_plan["features"])
        self.assertIn("Bring your own AI models", free_plan["features"])
        self.assertEqual(free_plan["cta"], "View on GitHub")
        self.assertEqual(free_plan["cta_url"], "https://github.com/gobii-ai/gobii-platform")
        self.assertEqual(free_plan["cta_icon"], "github")
        self.assertEqual(free_plan["cta_variant"], "outline")
        self.assertTrue(free_plan["external"])
        self.assertFalse(free_plan["signup_modal"])
        self.assertEqual(free_plan["analytics_cta_id"], "pricing_free_oss_plan")
        self.assertEqual(free_plan["analytics_intent"], "view_open_source")

        self.assertEqual(plans[1]["cta_url"], reverse("proprietary:startup_checkout"))
        self.assertEqual(plans[2]["cta_url"], reverse("proprietary:scale_checkout"))

        content = response.content.decode()
        analytics_pos = content.index('data-analytics-cta-id="pricing_free_oss_plan"')
        anchor_start = content.rfind("<a ", 0, analytics_pos)
        anchor_end = content.index("</a>", analytics_pos)
        free_anchor = content[anchor_start:anchor_end]
        self.assertIn('href="https://github.com/gobii-ai/gobii-platform"', free_anchor)
        self.assertIn('target="_blank"', free_anchor)
        self.assertIn('rel="noopener noreferrer"', free_anchor)
        self.assertIn('data-analytics-intent="view_open_source"', free_anchor)
        self.assertNotIn("data-cta-signup-modal", free_anchor)
        self.assertNotContains(response, "None tasks included")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_omits_signup_modal_config_when_flag_disabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, "gobii-cta-signup-modal-config")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_uses_cta_registry_for_enterprise_request_call_text(self, mock_get_stripe_settings):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )
        cta, _ = CallToAction.objects.get_or_create(
            slug="cta_enterprise_request_call",
            defaults={"description": "Enterprise request call CTA."},
        )
        cta.add_version("Talk to sales")

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Talk to sales")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_unauthenticated_pricing_uses_generic_trial_cta_when_flag_enabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_start_free_trial", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Start Free Trial")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Start Free Trial")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_unauthenticated_pricing_renders_no_charge_trial_text_when_flag_enabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_no_charge_during_trial", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(
            plans[PlanNames.STARTUP]["trial_cancel_text"],
            "No charge if you cancel during the 7-day trial. Takes 30 seconds.",
        )
        self.assertEqual(
            plans[PlanNames.SCALE]["trial_cancel_text"],
            "No charge if you cancel during the 14-day trial. Takes 30 seconds.",
        )
        self.assertContains(response, "No charge if you cancel during the 7-day trial. Takes 30 seconds.")
        self.assertContains(response, "No charge if you cancel during the 14-day trial. Takes 30 seconds.")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_unauthenticated_pricing_renders_trial_cancel_text_when_flag_enabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_pricing_cancel_text_under_btn", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(
            plans[PlanNames.STARTUP]["trial_cancel_text"],
            "Cancel anytime during the 7-day trial",
        )
        self.assertEqual(
            plans[PlanNames.SCALE]["trial_cancel_text"],
            "Cancel anytime during the 14-day trial",
        )
        self.assertContains(response, "Cancel anytime during the 7-day trial")
        self.assertContains(response, "Cancel anytime during the 14-day trial")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_unauthenticated_pricing_uses_unlock_copy_when_flag_enabled(
        self,
        mock_get_stripe_settings,
    ):
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_unlock_agent_copy", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Start for Free")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Start for Free")
        self.assertEqual(plans[PlanNames.STARTUP]["trial_cancel_text"], "No charge today. Cancel anytime.")
        self.assertEqual(plans[PlanNames.SCALE]["trial_cancel_text"], "No charge today. Cancel anytime.")
        self.assertContains(response, "No charge today. Cancel anytime.")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="no_trial"))
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_cta_uses_subscribe_copy_with_prior_subscription_history(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        _mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricingfree@example.com",
            email="pricingfree@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Subscribe to Pro")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Subscribe to Scale")
        self.assertIsNone(plans[PlanNames.STARTUP]["trial_cancel_text"])
        self.assertIsNone(plans[PlanNames.SCALE]["trial_cancel_text"])

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.STARTUP})
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="eligible"))
    @patch("proprietary.views.get_stripe_settings")
    def test_paid_plan_pricing_keeps_non_trial_cta_copy_when_unlock_flag_enabled(
        self,
        mock_get_stripe_settings,
        _mock_trial_eligibility,
        _mock_get_user_plan,
    ):
        user = get_user_model().objects.create_user(
            username="pricingstartup@example.com",
            email="pricingstartup@example.com",
            password="pw",
        )
        self.client.force_login(user)
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("cta_unlock_agent_copy", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Current Plan")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Upgrade to Scale")
        self.assertIsNone(plans[PlanNames.STARTUP]["trial_cancel_text"])
        self.assertIsNone(plans[PlanNames.SCALE]["trial_cancel_text"])

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="no_trial"))
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_uses_trial_copy_when_enforcement_flag_disabled(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricingflagoff@example.com",
            email="pricingflagoff@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("user_trial_eligibility_enforcement", active=False):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Start 7-day Free Trial")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Start 14-day Free Trial")
        mock_trial_eligibility.assert_not_called()

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="review"))
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_uses_trial_copy_when_review_decision_allowed(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        _mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricingreview@example.com",
            email="pricingreview@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with override_flag("user_trial_review_allows_trial", active=True):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Start 7-day Free Trial")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Start 14-day Free Trial")

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="eligible"))
    @patch("proprietary.views.user_has_prior_individual_history", return_value=True)
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_hides_trial_copy_when_one_per_user_flag_blocks_user(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        mock_prior_history,
        mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricing-one-per-user@example.com",
            email="pricing-one-per-user@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with (
            override_flag("user_trial_eligibility_enforcement", active=False),
            override_flag("user_trial_eligibility_enforcement_one_per_user", active=True),
        ):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Subscribe to Pro")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Subscribe to Scale")
        mock_prior_history.assert_called_once_with(user)
        mock_trial_eligibility.assert_not_called()

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="no_trial"))
    @patch("proprietary.views.user_has_prior_individual_history", return_value=False)
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_blocks_no_trial_before_one_per_user(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        mock_prior_history,
        mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricing-no-trial-before-one-per-user@example.com",
            email="pricing-no-trial-before-one-per-user@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with (
            override_flag("user_trial_eligibility_enforcement", active=True),
            override_flag("user_trial_eligibility_enforcement_one_per_user", active=True),
        ):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Subscribe to Pro")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Subscribe to Scale")
        mock_trial_eligibility.assert_called_once_with(user)
        mock_prior_history.assert_not_called()

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.evaluate_user_trial_eligibility", return_value=SimpleNamespace(decision="review"))
    @patch("proprietary.views.user_has_prior_individual_history", return_value=False)
    @patch("proprietary.views.get_user_plan", return_value={"id": PlanNames.FREE})
    @patch("proprietary.views.get_stripe_settings")
    def test_free_user_pricing_blocks_review_before_one_per_user_when_review_flag_disabled(
        self,
        mock_get_stripe_settings,
        _mock_get_user_plan,
        mock_prior_history,
        mock_trial_eligibility,
    ):
        user = get_user_model().objects.create_user(
            username="pricing-review-before-one-per-user@example.com",
            email="pricing-review-before-one-per-user@example.com",
            password="pw",
        )
        self.client.force_login(user)

        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        with (
            override_flag("user_trial_eligibility_enforcement", active=True),
            override_flag("user_trial_eligibility_enforcement_one_per_user", active=True),
            override_flag("user_trial_review_allows_trial", active=False),
        ):
            response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["cta"], "Subscribe to Pro")
        self.assertEqual(plans[PlanNames.SCALE]["cta"], "Subscribe to Scale")
        mock_trial_eligibility.assert_called_once_with(user)
        mock_prior_history.assert_not_called()

    @override_settings(GOBII_PROPRIETARY_MODE=True)
    @patch("proprietary.views.get_stripe_settings")
    def test_pricing_page_uses_db_backed_task_credit_amounts(self, mock_get_stripe_settings):
        self._set_monthly_task_credits(PlanNames.STARTUP, 750)
        self._set_monthly_task_credits(PlanNames.SCALE, 12500)
        mock_get_stripe_settings.return_value = SimpleNamespace(
            startup_trial_days=7,
            scale_trial_days=14,
        )

        response = self.client.get(reverse("proprietary:pricing"))

        self.assertEqual(response.status_code, 200)
        plans = {
            plan["code"]: plan
            for plan in response.context["pricing_plans"]
        }
        self.assertEqual(plans[PlanNames.STARTUP]["task_credits"], 750)
        self.assertEqual(plans[PlanNames.STARTUP]["tasks"], "750")
        self.assertEqual(plans[PlanNames.SCALE]["task_credits"], 12500)
        self.assertEqual(plans[PlanNames.SCALE]["tasks"], "12,500")
        self.assertContains(
            response,
            '<span><span class="font-semibold">750</span> tasks included</span>',
            html=True,
        )
        self.assertContains(
            response,
            '<span><span class="font-semibold">12,500</span> tasks included</span>',
            html=True,
        )
        self.assertContains(response, "$0.10 per task beyond 750")
        self.assertContains(response, "$0.04 per task beyond 12,500")
        self.assertContains(response, "750/month")
        self.assertContains(response, "12,500/month")
        self.assertContains(
            response,
            "Pro includes 750 tasks per month, then charges $0.10 for each additional task. "
            "Scale includes 12,500 tasks per month with $0.04 pricing after that.",
        )
        self.assertContains(
            response,
            "On the Pro tier, additional tasks are $0.10 each, while Scale brings that down "
            "to $0.04 once you pass the included 12,500 tasks.",
        )
