import { apiRequest } from "@/lib/api/api-service";
import type { SportType } from "@/lib/api/matches/types";
import { ApiResponse } from "@/types/api-response";
import {
  MatchPredictionDto,
  UpcomingPredictionsDto,
  ValueBetDto,
  PredictionHistoryResponseDto,
  AIHealthDto,
  SmartCouponRequestDto,
  SmartCouponResponseDto,
} from "./types";

/**
 * Predictions Service
 * Backend: /api/predictions/*
 */

const getPrediction = (matchId: string) => {
  return apiRequest<ApiResponse<MatchPredictionDto>>({
    url: `/predictions/${matchId}?nocache=true`,
    client: "core",
    method: "get",
  });
};

const generatePrediction = (body: { matchId: string; sport?: SportType }) => {
  return apiRequest<ApiResponse<MatchPredictionDto>>({
    url: "/predictions/generate",
    client: "core",
    method: "post",
    data: body,
  });
};

const getUpcoming = () => {
  return apiRequest<ApiResponse<UpcomingPredictionsDto>>({
    url: "/predictions/upcoming",
    client: "core",
    method: "get",
  });
};

const getValueBets = () => {
  return apiRequest<ApiResponse<ValueBetDto[]>>({
    url: "/predictions/value-bets",
    client: "core",
    method: "get",
  });
};

const getHistory = () => {
  return apiRequest<ApiResponse<PredictionHistoryResponseDto>>({
    url: "/predictions/history",
    client: "core",
    method: "get",
  });
};

const checkHealth = () => {
  return apiRequest<ApiResponse<AIHealthDto>>({
    url: "/predictions/health",
    client: "core",
    method: "get",
  });
};

const getCommentary = (matchId: string) => {
  return apiRequest<ApiResponse<{ commentary: string | null }>>({
    url: `/predictions/${matchId}/commentary`,
    client: "core",
    method: "get",
  });
};

const generateSmartCoupon = (body: SmartCouponRequestDto) => {
  return apiRequest<ApiResponse<SmartCouponResponseDto>>({
    url: "/predictions/smart-coupon",
    client: "core",
    method: "post",
    data: body,
  });
};

export const predictionsService = {
  getPrediction,
  generatePrediction,
  getUpcoming,
  getValueBets,
  getHistory,
  checkHealth,
  generateSmartCoupon,
  getCommentary,
};
