import { apiRequest } from "@/lib/api/api-service";
import { ApiResponse } from "@/types/api-response";
import type { MatchResponseDto } from "@/lib/api/matches/types";
import type {
  LeagueDto,
  CountryDto,
  TeamDto,
  HeadToHeadDto,
  LeagueQueryParams,
  TeamSearchParams,
  HeadToHeadParams,
  TeamMatchesParams,
  PaginatedMatchesResponse,
} from "./types";

/**
 * Leagues Service
 * Backend: /api/leagues/*
 */

const getLeagues = (params?: LeagueQueryParams) => {
  return apiRequest<ApiResponse<LeagueDto[]>>({
    url: "/leagues",
    client: "core",
    method: "get",
    params,
  });
};

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

const getCountries = () => {
  return apiRequest<ApiResponse<CountryDto[]>>({
    url: "/leagues/countries",
    client: "core",
    method: "get",
  });
};

const getCountryById = (id: string) => {
  return apiRequest<ApiResponse<CountryDto>>({
    url: `/leagues/countries/${id}`,
    client: "core",
    method: "get",
  });
};

const getTeamById = (id: string) => {
  return apiRequest<ApiResponse<TeamDto>>({
    url: `/leagues/teams/${id}`,
    client: "core",
    method: "get",
  });
};

const getTeamMatches = (id: string, params?: TeamMatchesParams) => {
  return apiRequest<PaginatedMatchesResponse>({
    url: `/leagues/teams/${id}/matches`,
    client: "core",
    method: "get",
    params,
  });
};

const getHeadToHead = (params: HeadToHeadParams) => {
  return apiRequest<ApiResponse<HeadToHeadDto>>({
    url: "/leagues/teams/h2h",
    client: "core",
    method: "get",
    params,
  });
};

const searchTeams = (params: TeamSearchParams) => {
  return apiRequest<ApiResponse<TeamDto[]>>({
    url: "/leagues/teams/search",
    client: "core",
    method: "get",
    params,
  });
};

export const leaguesService = {
  getLeagues,
  getLeagueById,
  getCountries,
  getCountryById,
  getTeamById,
  getTeamMatches,
  getHeadToHead,
  searchTeams,
};
