"""Attention-related Oracle service methods."""

from __future__ import annotations

import datetime as dt

from libs.oracle_client.client import OracleClient
from libs.oracle_client.models import (
    CollectionStatusResponse,
    EntityResolveResponse,
    EventAttentionResponse,
)


def _date_to_iso(event_date: str | dt.date) -> str:
    if isinstance(event_date, dt.date):
        return event_date.isoformat()
    return event_date


class AttentionService:
    def __init__(self, client: OracleClient) -> None:
        self._client = client

    async def get_entity(self, ticker: str) -> EntityResolveResponse:
        data = await self._client.get(f"/api/v1/attention/entity/{ticker}")
        return EntityResolveResponse.model_validate(data)

    async def resolve_entity(self, ticker: str) -> EntityResolveResponse:
        data = await self._client.post(f"/api/v1/attention/admin/resolve/{ticker}")
        return EntityResolveResponse.model_validate(data)

    async def get_event_attention(
        self,
        ticker: str,
        event_date: str | dt.date,
    ) -> EventAttentionResponse:
        data = await self._client.get(
            f"/api/v1/attention/event/{ticker}",
            params={"event_date": _date_to_iso(event_date)},
        )
        return EventAttentionResponse.model_validate(data)

    async def collect_wiki(
        self,
        ticker: str,
        event_date: str | dt.date,
    ) -> CollectionStatusResponse:
        data = await self._client.post(
            f"/api/v1/attention/admin/collect/wiki/{ticker}",
            params={"event_date": _date_to_iso(event_date)},
        )
        return CollectionStatusResponse.model_validate(data)

    async def collect_gdelt(
        self,
        ticker: str,
        event_date: str | dt.date,
    ) -> CollectionStatusResponse:
        data = await self._client.post(
            f"/api/v1/attention/admin/collect/gdelt/{ticker}",
            params={"event_date": _date_to_iso(event_date)},
        )
        return CollectionStatusResponse.model_validate(data)
