"""
Auto-generated comment_automations resource.

DO NOT EDIT THIS FILE MANUALLY.
Run `python scripts/generate_resources.py` to regenerate.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ..client.base import BaseClient


class CommentAutomationsResource:
    """
    comment_automations operations.
    """

    def __init__(self, client: BaseClient) -> None:
        self._client = client

    def _build_params(self, **kwargs: Any) -> dict[str, Any]:
        """Build query parameters, filtering None values."""

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        return {to_camel(k): v for k, v in kwargs.items() if v is not None}

    def _build_payload(self, **kwargs: Any) -> dict[str, Any]:
        """Build request payload, filtering None values."""
        from datetime import datetime

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        result: dict[str, Any] = {}
        for k, v in kwargs.items():
            if v is None:
                continue
            if isinstance(v, datetime):
                result[to_camel(k)] = v.isoformat()
            else:
                result[to_camel(k)] = v
        return result

    def list_comment_automations(
        self, *, profile_id: str | None = None
    ) -> dict[str, Any]:
        """List comment-to-DM automations"""
        params = self._build_params(
            profile_id=profile_id,
        )
        return self._client._get("/v1/comment-automations", params=params)

    def create_comment_automation(
        self,
        profile_id: str,
        account_id: str,
        platform_post_id: str,
        name: str,
        dm_message: str,
        *,
        post_id: str | None = None,
        post_title: str | None = None,
        keywords: list[str] | None = None,
        match_mode: str | None = "contains",
        comment_reply: str | None = None,
    ) -> dict[str, Any]:
        """Create a comment-to-DM automation"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform_post_id=platform_post_id,
            post_id=post_id,
            post_title=post_title,
            name=name,
            keywords=keywords,
            match_mode=match_mode,
            dm_message=dm_message,
            comment_reply=comment_reply,
        )
        return self._client._post("/v1/comment-automations", data=payload)

    def get_comment_automation(self, automation_id: str) -> dict[str, Any]:
        """Get automation details with recent logs"""
        return self._client._get(f"/v1/comment-automations/{automation_id}")

    def update_comment_automation(
        self,
        automation_id: str,
        *,
        name: str | None = None,
        keywords: list[str] | None = None,
        match_mode: str | None = None,
        dm_message: str | None = None,
        comment_reply: str | None = None,
        is_active: bool | None = None,
    ) -> dict[str, Any]:
        """Update automation settings"""
        payload = self._build_payload(
            name=name,
            keywords=keywords,
            match_mode=match_mode,
            dm_message=dm_message,
            comment_reply=comment_reply,
            is_active=is_active,
        )
        return self._client._patch(
            f"/v1/comment-automations/{automation_id}", data=payload
        )

    def delete_comment_automation(self, automation_id: str) -> dict[str, Any]:
        """Delete automation and all logs"""
        return self._client._delete(f"/v1/comment-automations/{automation_id}")

    def list_comment_automation_logs(
        self,
        automation_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List trigger logs for an automation"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return self._client._get(
            f"/v1/comment-automations/{automation_id}/logs", params=params
        )

    async def alist_comment_automations(
        self, *, profile_id: str | None = None
    ) -> dict[str, Any]:
        """List comment-to-DM automations (async)"""
        params = self._build_params(
            profile_id=profile_id,
        )
        return await self._client._aget("/v1/comment-automations", params=params)

    async def acreate_comment_automation(
        self,
        profile_id: str,
        account_id: str,
        platform_post_id: str,
        name: str,
        dm_message: str,
        *,
        post_id: str | None = None,
        post_title: str | None = None,
        keywords: list[str] | None = None,
        match_mode: str | None = "contains",
        comment_reply: str | None = None,
    ) -> dict[str, Any]:
        """Create a comment-to-DM automation (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform_post_id=platform_post_id,
            post_id=post_id,
            post_title=post_title,
            name=name,
            keywords=keywords,
            match_mode=match_mode,
            dm_message=dm_message,
            comment_reply=comment_reply,
        )
        return await self._client._apost("/v1/comment-automations", data=payload)

    async def aget_comment_automation(self, automation_id: str) -> dict[str, Any]:
        """Get automation details with recent logs (async)"""
        return await self._client._aget(f"/v1/comment-automations/{automation_id}")

    async def aupdate_comment_automation(
        self,
        automation_id: str,
        *,
        name: str | None = None,
        keywords: list[str] | None = None,
        match_mode: str | None = None,
        dm_message: str | None = None,
        comment_reply: str | None = None,
        is_active: bool | None = None,
    ) -> dict[str, Any]:
        """Update automation settings (async)"""
        payload = self._build_payload(
            name=name,
            keywords=keywords,
            match_mode=match_mode,
            dm_message=dm_message,
            comment_reply=comment_reply,
            is_active=is_active,
        )
        return await self._client._apatch(
            f"/v1/comment-automations/{automation_id}", data=payload
        )

    async def adelete_comment_automation(self, automation_id: str) -> dict[str, Any]:
        """Delete automation and all logs (async)"""
        return await self._client._adelete(f"/v1/comment-automations/{automation_id}")

    async def alist_comment_automation_logs(
        self,
        automation_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List trigger logs for an automation (async)"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return await self._client._aget(
            f"/v1/comment-automations/{automation_id}/logs", params=params
        )
