"""
SellerLegend API Schema Resource Client

Handles schema introspection endpoints for discovering filterable columns on datatables.
"""

from typing import Dict, Any, Literal
from ..base import BaseClient
from ..validators import validate_enum, ValidationError

# Valid non-PPC datatable identifiers
BASE_DATATABLES = [
    'products',
    'inventory',
    'inventory_history',
    'sales_statistics',
    'restock_suggestions',
    'warehouses',
    'inbound_shipments',
    'feedback',
]

# PPC datatable identifiers (ppc_{type}_{entity_slug})
PPC_DATATABLES = [
    'ppc_sp_campaigns',
    'ppc_sp_ad_groups',
    'ppc_sp_products',
    'ppc_sp_products_by_campaign',
    'ppc_sp_ad_keywords',
    'ppc_sp_search_terms',
    'ppc_sp_targets',
    'ppc_sb_campaigns',
    'ppc_sb_ad_groups',
    'ppc_sb_ad_keywords',
    'ppc_sd_campaigns',
    'ppc_sd_ad_groups',
    'ppc_sd_products',
]

# All valid datatable identifiers
ALL_DATATABLES = BASE_DATATABLES + PPC_DATATABLES


class SchemaClient:
    """Client for schema introspection endpoints."""

    def __init__(self, base_client: BaseClient):
        """
        Initialize schema client.

        Args:
            base_client: Base API client instance
        """
        self.client = base_client

    def get_filterable_columns(
        self,
        datatable: str,
        account_title: str = None,
        seller_id: str = None,
        marketplace_id: str = None,
    ) -> Dict[str, Any]:
        """
        Get filterable columns for a specific datatable.

        Returns the list of columns that can be used in custom_filter parameters,
        along with their data types and descriptions.

        Args:
            datatable: Datatable identifier. Valid values include:
                - Base: products, inventory, inventory_history, sales_statistics,
                  restock_suggestions, warehouses, inbound_shipments, feedback
                - PPC: ppc_sp_campaigns, ppc_sp_ad_groups, ppc_sp_products,
                  ppc_sp_products_by_campaign, ppc_sp_ad_keywords,
                  ppc_sp_search_terms, ppc_sp_targets, ppc_sb_campaigns,
                  ppc_sb_ad_groups, ppc_sb_ad_keywords, ppc_sd_campaigns,
                  ppc_sd_ad_groups, ppc_sd_products
            account_title: Filter by account title
            seller_id: Amazon seller ID (requires marketplace_id)
            marketplace_id: Amazon marketplace ID (requires seller_id)

        Returns:
            Dict with 'datatable' name and 'columns' list. Each column has:
            - name: Column identifier for use in filters
            - title: Human-readable column title
            - type: Filter type (text, number, date, select, bool)
            - description: Column description

        Raises:
            ValidationError: If datatable identifier is invalid
        """
        validate_enum(datatable, ALL_DATATABLES, "datatable")

        params = {
            "datatable": datatable,
        }

        if account_title:
            params["account_title"] = account_title
        if seller_id:
            params["seller_id"] = seller_id
        if marketplace_id:
            params["marketplace_id"] = marketplace_id

        return self.client.get("schema/get-filterable-columns", params=params)

    @staticmethod
    def get_available_datatables() -> list:
        """
        Get list of all available datatable identifiers.

        Returns:
            List of valid datatable identifier strings
        """
        return list(ALL_DATATABLES)
