"""PostgreSQL Repository for Zalo CRM implementing ZaloCrmRepositoryPort."""

from __future__ import annotations

from typing import Any

from app.core.postgres import PostgresClient
from app.modules.zalo_crm.domain.ports.zalo_crm_repository_port import ZaloCrmRepositoryPort


class PostgresZaloCrmRepository(ZaloCrmRepositoryPort):
    """Encapsulates PostgreSQL operations for contacts, conversations, and messages."""

    def __init__(self, pg_client: PostgresClient | None = None) -> None:
        self.client = pg_client or PostgresClient()

    def upsert_contact(self, contact: dict[str, Any]) -> Any:
        return self.client.upsert_contact(contact)

    def upsert_zalo_account(self, account: dict[str, Any]) -> Any:
        return self.client.upsert_zalo_account(account)

    def upsert_conversation(self, conversation: dict[str, Any]) -> Any:
        return self.client.upsert_conversation(conversation)

    def insert_message(self, message: dict[str, Any]) -> Any:
        return self.client.insert_message(message)

    def get_all_contacts(self) -> list[dict[str, Any]]:
        return self.client.get_all_contacts() or []

    def get_messages_by_conversation(self, conv_id: str) -> list[dict[str, Any]]:
        return self.client.get_messages_by_conversation(conv_id) or []
