# app/modules/catalog/services/catalog_features.py
"""
Catalog feature provider for the billing feature system.

Declares catalog-related billable features (product limits, import/export)
and provides usage tracking queries for feature gating.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import func

from app.modules.contracts.features import (
    FeatureDeclaration,
    FeatureScope,
    FeatureType,
    FeatureUsage,
)

if TYPE_CHECKING:
    from sqlalchemy.orm import Session


class CatalogFeatureProvider:
    """Feature provider for the catalog module.

    Declares:
    - products_limit: quantitative per-store limit on product count
    - product_import_export: binary merchant-level feature for import/export
    """

    @property
    def feature_category(self) -> str:
        return "catalog"

    def get_feature_declarations(self) -> list[FeatureDeclaration]:
        return [
            FeatureDeclaration(
                code="products_limit",
                name_key="catalog.features.products_limit.name",
                description_key="catalog.features.products_limit.description",
                category="catalog",
                feature_type=FeatureType.QUANTITATIVE,
                scope=FeatureScope.STORE,
                default_limit=200,
                unit_key="catalog.features.products_limit.unit",
                ui_icon="package",
                display_order=10,
            ),
            FeatureDeclaration(
                code="product_import_export",
                name_key="catalog.features.product_import_export.name",
                description_key="catalog.features.product_import_export.description",
                category="catalog",
                feature_type=FeatureType.BINARY,
                scope=FeatureScope.MERCHANT,
                ui_icon="upload-download",
                display_order=20,
            ),
        ]

    def get_store_usage(
        self,
        db: Session,
        store_id: int,
    ) -> list[FeatureUsage]:
        from app.modules.catalog.models.product import Product

        count = (
            db.query(func.count(Product.id))
            .filter(Product.store_id == store_id)
            .scalar()
            or 0
        )
        return [
            FeatureUsage(
                feature_code="products_limit",
                current_count=count,
                label="Products",
            ),
        ]

    def get_merchant_usage(
        self,
        db: Session,
        merchant_id: int,
        platform_id: int,
    ) -> list[FeatureUsage]:
        from app.modules.catalog.models.product import Product
        from app.modules.tenancy.services.platform_service import platform_service
        from app.modules.tenancy.services.store_service import store_service

        merchant_stores = store_service.get_stores_by_merchant_id(db, merchant_id)
        platform_store_ids = platform_service.get_store_ids_for_platform(db, platform_id)
        store_ids = [s.id for s in merchant_stores if s.id in platform_store_ids]

        count = (
            db.query(func.count(Product.id))
            .filter(Product.store_id.in_(store_ids))
            .scalar()
            or 0
        )
        return [
            FeatureUsage(
                feature_code="products_limit",
                current_count=count,
                label="Products",
            ),
        ]


# Singleton instance for module registration
catalog_feature_provider = CatalogFeatureProvider()

__all__ = [
    "CatalogFeatureProvider",
    "catalog_feature_provider",
]
