from __future__ import annotations

from datetime import date
from http import HTTPStatus
from urllib.parse import parse_qs
from backend.data.providers.tushare_client import TushareError


class AuctionRoutesMixin:
    def _handle_auction_get(self, parsed) -> bool:
        if parsed.path == "/api/auction":
            query = parse_qs(parsed.query)
            try:
                self.send_json(
                    self.application_service.auction_center(
                        query.get("trade_date", [date.today().isoformat()])[0],
                        query.get("force", ["0"])[0] == "1",
                    )
                )
            except (ValueError, TushareError) as exc:
                self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
            return True
        return False
