# app/modules/loyalty/services/loyalty_widgets.py
"""
Loyalty dashboard widget provider.

Provides storefront dashboard cards for loyalty-related data.
Implements get_storefront_dashboard_cards from DashboardWidgetProviderProtocol.
"""

import logging

from sqlalchemy.orm import Session

from app.modules.contracts.widgets import (
    DashboardWidget,
    StorefrontDashboardCard,
    WidgetContext,
)

logger = logging.getLogger(__name__)


class LoyaltyWidgetProvider:
    """Widget provider for loyalty module."""

    @property
    def widgets_category(self) -> str:
        return "loyalty"

    def get_store_widgets(
        self,
        db: Session,
        store_id: int,
        context: WidgetContext | None = None,
    ) -> list[DashboardWidget]:
        return []

    def get_platform_widgets(
        self,
        db: Session,
        platform_id: int,
        context: WidgetContext | None = None,
    ) -> list[DashboardWidget]:
        return []

    def get_storefront_dashboard_cards(
        self,
        db: Session,
        store_id: int,
        customer_id: int,
        context: WidgetContext | None = None,
    ) -> list[StorefrontDashboardCard]:
        """Provide the Loyalty Rewards card for the customer dashboard."""
        from app.modules.loyalty.models.loyalty_card import LoyaltyCard

        card = (
            db.query(LoyaltyCard)
            .filter(
                LoyaltyCard.customer_id == customer_id,
                LoyaltyCard.is_active.is_(True),
            )
            .first()
        )

        points = card.points_balance if card else None
        subtitle = "View your points & rewards" if card else "Join our rewards program"

        return [
            StorefrontDashboardCard(
                key="loyalty.rewards",
                icon="gift",
                title="Loyalty Rewards",
                subtitle=subtitle,
                route="account/loyalty",
                value=points,
                value_label="Points Balance" if points is not None else None,
                order=30,
            ),
        ]


loyalty_widget_provider = LoyaltyWidgetProvider()
