"""Salesforce CPQ / Revenue Cloud adapter for the seller quote loop."""

from __future__ import annotations

from typing import Any

from .base import QuoteAdapter, QuoteAdapterSupportResult


class SalesforceCpqQuoteAdapter(QuoteAdapter):
    adapter_key = "salesforce_cpq"
    detected_engine = "salesforce_cpq"

    def seller_quote_support(self, *, environment_profile: Any) -> QuoteAdapterSupportResult:
        capability_profile = getattr(environment_profile, "adapter_capability_profile", None)
        supported_capabilities = set(getattr(capability_profile, "supported_capabilities", []) or [])
        if capability_profile is not None and "read_status" not in supported_capabilities:
            return QuoteAdapterSupportResult(
                status="unsupported",
                reason=(
                    "The detected quote stack does not currently advertise read_status support for "
                    "seller quote status in Slack."
                ),
                capability_gaps=list(getattr(capability_profile, "notes", []) or []),
            )
        return QuoteAdapterSupportResult(status="supported")

    def live_approval_supported(self, *, environment_profile: Any) -> bool:
        return bool(getattr(environment_profile, "approval_objects", None))

    def load_quote_records(
        self,
        *,
        opportunity_id: str,
        org_alias: str | None,
    ) -> list[Any]:
        from g_gremlin.dealdesk import service as service_mod

        return service_mod._load_salesforce_cpq_quote_records(
            opportunity_id=opportunity_id,
            org_alias=org_alias,
        )

    def load_quote_line_items(
        self,
        *,
        quote_id: str,
        org_alias: str | None,
        reference_type: str,
    ) -> list[Any]:
        from g_gremlin.dealdesk import service as service_mod

        return service_mod._load_salesforce_cpq_quote_line_items(
            quote_id=quote_id,
            org_alias=org_alias,
            reference_type=reference_type,
        )

    def quote_line_item_object_name(self) -> str:
        return "SBQQ__QuoteLine__c"

    def preview_apply_support(self, *, preview: Any) -> tuple[bool, str]:
        if getattr(preview, "line_item_preview", None) is not None:
            return (
                False,
                "Guided line-item apply currently supports Salesforce Standard Quotes only. This stack remains preview-only.",
            )
        return (
            False,
            "Guided quote apply currently supports Salesforce Standard Quotes only. Salesforce CPQ / Revenue Cloud remains preview-only in this slice.",
        )

    def build_quote_revision_plan(
        self,
        *,
        preview: Any,
        org_alias: str | None,
    ) -> Any:
        del preview, org_alias
        raise RuntimeError(
            "Guided quote apply currently supports Salesforce Standard Quotes only. Salesforce CPQ / Revenue Cloud remains preview-only in this slice."
        )


__all__ = ["SalesforceCpqQuoteAdapter"]
