import { apiRequest } from "@/lib/api/api-service";
import { ApiResponse, PaginatedData } from "@/types/api-response";
import {
  MatchResponseDto,
  MatchListParams,
  MatchQueryDto,
  ActiveLeagueDto,
  LeagueWithMatchesDto,
} from "./types";

/**
 * Matches Service
 * Backend: /api/matches/*
 */

const listMatches = (params?: MatchListParams) => {
  return apiRequest<ApiResponse<PaginatedData<MatchResponseDto>>>({
    url: "/matches",
    client: "core",
    method: "get",
    params,
  });
};

const getMatchDetails = (id: string) => {
  return apiRequest<ApiResponse<MatchResponseDto>>({
    url: `/matches/${id}`,
    client: "core",
    method: "get",
  });
};

const queryMatches = (queryDto: MatchQueryDto) => {
  return apiRequest<ApiResponse<LeagueWithMatchesDto[]>>({
    url: "/matches/query",
    client: "core",
    method: "post",
    data: queryDto,
  });
};

const getActiveLeagues = (sport?: string) => {
  return apiRequest<ApiResponse<ActiveLeagueDto[]>>({
    url: "/matches/leagues/active",
    client: "core",
    method: "get",
    params: sport ? { sport } : undefined,
  });
};

export type OddsMovementDto = Record<
  string,
  Record<string, { open: number; close: number }>
>;

const getOddsMovement = (id: string) => {
  return apiRequest<ApiResponse<OddsMovementDto>>({
    url: `/matches/${id}/odds-movement`,
    client: "core",
    method: "get",
  });
};

export const matchesService = {
  listMatches,
  getMatchDetails,
  queryMatches,
  getActiveLeagues,
  getOddsMovement,
};
