"""Billing actions panel: tabbed Grace Period / Permanent Waiver forms."""

from __future__ import annotations

from prefab_ui.actions import SetState
from prefab_ui.components import (
    Button,
    CardContent,
    Column,
    Div,
    Input,
    Row,
    Select,
    Tab,
    Tabs,
    Text,
    Textarea,
)
from prefab_ui.components.control_flow import If
from prefab_ui.rx import EVENT, STATE

from airbyte_ops_webapp.pages.customer_billing._helpers import render_select_options
from airbyte_ops_webapp.theme import (
    BUTTON_DESTRUCTIVE_CLASS,
    PANEL_CARD_CLASS,
    _card_style,
)


def render_billing_actions() -> None:
    """Render the right-panel tabbed billing actions."""
    with (
        Div(css_class=PANEL_CARD_CLASS, style=_card_style()),
        CardContent(),
        Column(gap=3),
    ):
        Text(
            "Billing Actions",
            style={"fontWeight": "700", "fontSize": "1.125rem"},
        )
        with Tabs(name="billing_action_tab", value="grace_period"):
            with Tab("Grace Period", value="grace_period"):
                _render_grace_period_form()
            with Tab("Permanent Waiver", value="permanent_waiver"):
                _render_permanent_waiver_form()


def _render_grace_period_form() -> None:
    """Grace period set/extend/cancel form."""
    with Column(gap=3):
        with Column(gap=1):
            Text(
                "End date or days", style={"fontSize": "0.875rem", "fontWeight": "500"}
            )
            Input(
                name="grace_period_value",
                value=STATE.grace_period_value,
                placeholder="YYYY-MM-DD, number of days (1-90), or 'cancel'",
            )
        with Column(gap=1):
            Text("Reason", style={"fontSize": "0.875rem", "fontWeight": "500"})
            Textarea(
                name="grace_period_reason",
                value=STATE.grace_period_reason,
                placeholder="e.g. 7-day extension per #ask-finance request",
                rows=2,
            )
        with Column(gap=1):
            Text("Approval URL", style={"fontSize": "0.875rem", "fontWeight": "500"})
            Input(
                name="grace_period_approval_url",
                value=STATE.grace_period_approval_url,
                placeholder="Slack approval record URL",
            )
        with Row(justify="end", gap=2):
            Button(
                "Apply",
                variant="destructive",
                css_class=BUTTON_DESTRUCTIVE_CLASS,
                disabled=STATE.is_loading,
                on_click=[
                    SetState("confirm_action_type", "grace_period"),
                    SetState("confirm_dialog_open", True),
                ],
            )


def _render_permanent_waiver_form() -> None:
    """Permanent waiver set/remove form."""
    with Column(gap=3):
        with Column(gap=1):
            Text("Waiver Type", style={"fontSize": "0.875rem", "fontWeight": "500"})
            with Select(
                name="waiver_type",
                value="free",
                onChange=SetState("waiver_type", EVENT.target.value),
            ):
                render_select_options(
                    [
                        {"label": "Free (Partner)", "value": "free"},
                        {"label": "Internal", "value": "internal"},
                        {"label": "Remove Waiver", "value": "none"},
                    ]
                )
        with Column(gap=1):
            Text("Reason", style={"fontSize": "0.875rem", "fontWeight": "500"})
            Textarea(
                name="waiver_reason",
                value=STATE.waiver_reason,
                placeholder="e.g. Partner account per SOW",
                rows=2,
            )
        with Column(gap=1):
            Text("Approval URL", style={"fontSize": "0.875rem", "fontWeight": "500"})
            Input(
                name="waiver_approval_url",
                value=STATE.waiver_approval_url,
                placeholder="Slack approval record URL",
            )
        with If(STATE.waiver_type), If(STATE.waiver_type != "none"):
            Text(
                "Also changes Orb plan + Stigg entitlement automatically.",
                style={
                    "fontSize": "0.8125rem",
                    "opacity": "0.6",
                    "fontStyle": "italic",
                },
            )

        with Row(justify="end", gap=2):
            Button(
                "Apply",
                variant="destructive",
                css_class=BUTTON_DESTRUCTIVE_CLASS,
                disabled=STATE.is_loading,
                on_click=[
                    SetState("confirm_action_type", "permanent_waiver"),
                    SetState("confirm_dialog_open", True),
                ],
            )
