from dataclasses import dataclass

from http_tools import AbmServiceConnector


class MarketplaceServerConnector:
    Config = AbmServiceConnector.Config

    @dataclass(slots=True)
    class Context(AbmServiceConnector.Context):
        server_name: str

    def __init__(self, config: Config, context: Context) -> None:
        self._context = context
        self._http_connector = AbmServiceConnector(config, context)

    async def create_account(
        self,
        portal_id: int,
        provider: str,
        flow: str = "access",
        credentials: dict | None = None,
        is_active: bool = True,
    ) -> int:
        answer = await self._http_connector.post(
            "/v1/catalog/accounts",
            payload={
                "portal_id": portal_id,
                "provider": provider,
                "flow": flow,
                "credentials": credentials or {},
                "is_active": is_active,
            },
            headers={"server_name": self._context.server_name},
        )
        return answer["id"]

    async def create_product(
        self,
        portal_id: int,
        portal_to_marketplace_id: int,
        provider: str,
        external_product_id: str,
        title: str = "",
    ) -> int:
        answer = await self._http_connector.post(
            "/v1/catalog/products",
            payload={
                "portal_id": portal_id,
                "portal_to_marketplace_id": portal_to_marketplace_id,
                "provider": provider,
                "external_product_id": external_product_id,
                "title": title,
            },
            headers={"server_name": self._context.server_name},
        )
        return answer["id"]
