"""Salesforce Standard Quotes adapter for the seller quote loop."""

from __future__ import annotations

from typing import Any

from .base import QuoteAdapter, QuoteAdapterSupportResult


class SalesforceStandardQuoteAdapter(QuoteAdapter):
    adapter_key = "salesforce_standard_quotes"
    detected_engine = "standard_quotes"

    def allow_header_apply_without_capability_profile(self) -> bool:
        return True

    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_standard_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]:
        del reference_type
        from g_gremlin.dealdesk import service as service_mod

        return service_mod._load_standard_quote_line_items(
            quote_id=quote_id,
            org_alias=org_alias,
        )

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

    def preview_apply_support(self, *, preview: Any) -> tuple[bool, str]:
        line_item_preview = getattr(preview, "line_item_preview", None)
        if line_item_preview is not None:
            status = str(getattr(preview, "status", "") or "").strip()
            resolution_status = str(getattr(line_item_preview, "resolution_status", "") or "").strip()
            if status != "preview_ready" or resolution_status != "resolved":
                return (
                    False,
                    "Gremlin can apply a line-item change only after resolving an exact current line or exact active SKU.",
                )
            operation = str(getattr(getattr(preview, "intent", None), "operation", "") or "").strip()
            if operation not in {"add_product", "remove_product", "update_quantity"}:
                return (False, "Guided line-item apply is not available for this requested operation.")
            if str(getattr(line_item_preview, "line_object_name", "") or "").strip() != "QuoteLineItem":
                return (
                    False,
                    "Guided line-item apply currently requires Salesforce QuoteLineItem records on the selected quote stack.",
                )
            return True, ""
        if getattr(preview, "selected_quote", None) is None or getattr(preview, "field_preview", None) is None:
            return (
                False,
                "Guided quote apply requires a selected standard quote and a resolved field preview.",
            )
        return True, ""

    def build_quote_revision_plan(
        self,
        *,
        preview: Any,
        org_alias: str | None,
    ) -> Any:
        from g_gremlin.dealdesk import service as service_mod

        return service_mod._build_standard_quote_revision_plan(
            preview=preview,
            org_alias=org_alias,
        )


__all__ = ["SalesforceStandardQuoteAdapter"]
