"""
PlanSelectScenario — Find plan picker, select a plan, optionally enter coupon,
verify checkout button becomes active.

config keys:
    plan_url (str)                  — URL of pricing/plan selection page (if navigation needed)
    plan_card_selector (str)        — CSS selector for the target plan card / radio button
    coupon_selector (str)           — optional CSS selector for coupon input field
    coupon_code (str)               — optional coupon code string
    coupon_apply_selector (str)     — optional CSS selector for coupon apply button
    checkout_button_selector (str)  — CSS selector for checkout button
    plan_name (str)                 — human-readable name for logging (e.g. "Professional")
"""

from .base import BaseScenario, ScenarioResult


class PlanSelectScenario(BaseScenario):
    name = "plan_select"

    async def run(self, page, config: dict) -> None:
        # -- Step 1: Navigate to plan URL (if provided)
        plan_url = config.get("plan_url", "")
        if plan_url:
            await self._navigate(plan_url, step_name="navigate_to_plans")

        # -- Step 2: Find and click the plan card
        plan_card_sel = config.get("plan_card_selector", "")
        if not plan_card_sel:
            step = self._new_step("check_plan_card_config")
            self._finish_step(step, "error", "plan_card_selector not provided in config")
            return

        plan_name = config.get("plan_name", "selected plan")
        await self._click(plan_card_sel, step_name=f"select_plan_{plan_name.lower().replace(' ', '_')}")

        # -- Step 3: Enter coupon code (optional)
        coupon_code = config.get("coupon_code", "")
        coupon_sel = config.get("coupon_selector", "")
        if coupon_code and coupon_sel:
            await self._fill(coupon_sel, coupon_code, step_name="enter_coupon_code")

            coupon_apply_sel = config.get("coupon_apply_selector", "")
            if coupon_apply_sel:
                await self._click(coupon_apply_sel, step_name="apply_coupon")

        # -- Step 4: Verify checkout button is active (enabled)
        checkout_sel = config.get("checkout_button_selector", "")
        if not checkout_sel:
            step = self._new_step("check_checkout_button_config")
            self._finish_step(step, "error", "checkout_button_selector not provided in config")
            return

        step = self._new_step("verify_checkout_button_active")
        try:
            await self._page.wait_for_selector(checkout_sel, timeout=self._timeout, state="visible")
            is_disabled = await self._page.is_disabled(checkout_sel)
            if is_disabled:
                from .base import StepResult
                step.screenshot_path = await self._screenshot("checkout_button_disabled")
                self._finish_step(step, "fail", "Checkout button is still disabled after plan selection")
            else:
                self._finish_step(step, "pass", "Checkout button is active/enabled")
        except Exception as exc:
            step.screenshot_path = await self._screenshot("checkout_button_error")
            self._finish_step(step, "error", str(exc))

        # -- Step 5: Screenshot of selected plan state
        await self._capture_screenshot_step(f"plan_selected_{plan_name.lower().replace(' ', '_')}")
