from __future__ import annotations

from datetime import date
from http import HTTPStatus
from urllib.parse import parse_qs


class SentimentRoutesMixin:
    def _handle_sentiment_get(self, parsed) -> bool:
        if parsed.path == "/api/sentiment/history":
            query = parse_qs(parsed.query)
            trade_date = query.get("trade_date", [date.today().isoformat()])[0]
            try:
                limit = int(query.get("limit", ["20"])[0])
                self.send_json(self.application_service.sentiment_history(trade_date, limit))
            except (TypeError, ValueError) as exc:
                self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
            return True
        return False
