<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;


class SportClientService
{

    //
    public static function get($type, $endpoint, $params = [])
    {
        if ($type == 1) {
            $host = env('API_FOOTBALL_HOST');
            $key = env('API_FOOTBALL_KEY');
        } elseif ($type == 2) {
            $host = env('API_BASKETBALL_HOST');
            $key = env('API_BASKETBALL_KEY');
        } else {
            throw new \Exception("Invalid sport type");
        }
        $response = Http::withHeaders([
            'x-apisports-key' => $key,
        ])
            ->withoutVerifying()  // 临时跳过 SSL 验证
            ->get($host . '/' . $endpoint, $params);

        if ($response->successful()) {
            return $response->json();
        }

        // Handle errors as needed
        throw new \Exception("API request failed: " . $response->body());
    }

    public static function post($type, $endpoint, $data = [])
    {
        if ($type == 1) {
            $host = env('API_FOOTBALL_HOST');
            $key = env('API_FOOTBALL_KEY');
        } elseif ($type == 2) {
            $host = env('API_BASKETBALL_HOST');
            $key = env('API_BASKETBALL_KEY');
        } else {
            throw new \Exception("Invalid sport type");
        }
        $response = Http::withHeaders([
            'x-apisports-key' => $key,
        ])->post($host . '/' . $endpoint, $data);

        if ($response->successful()) {
            return $response->json();
        }

        // Handle errors as needed
        throw new \Exception("API request failed: " . $response->body());
    }

    // 时区
    public static function timezone()
    {
        return self::get(1, 'timezone');
    }

    // 国家/地区
    public static function countries($params = [])
    {
        return self::get(1, 'countries', $params);
    }

    //足球 -  联赛  获取可用的联赛和杯赛名单。
    public static function leagues($params = [])
    {
        return static::get(1, 'leagues', $params);
    }

    //足球 - 联赛赛季  获取特定联赛的赛季列表。
    public static function leaguesSeasons($params = [])
    {
        return static::get(1, 'leagues/seasons', $params);
    }


    //  This endpoint returns in-play odds for fixtures in progress.
    //  此端点会返回正在进行的比赛的实时赔率。
    //  Update Frequency : This endpoint is updated every 5 seconds.
    // 足球 -  更新频率：此端点每 5 秒钟更新一次。
    public static function oddsLive($params = [])
    {
        return static::get(1, 'odds/live', $params);
    }

    //足球 - 赛事赔率
    public static function odds($params = [])
    {
        return static::get(1, 'odds', $params);
    }

    public static function fixturesRounds($params = [])
    {
        return static::get(1, 'fixtures/rounds', $params);
    }


    //足球 -  赛程
    public static function fixtures($params = [])
    {
        return self::get(1, 'fixtures', $params);
    }

    //足球 - 赛事统计数据
    public static function statistics($params = [])
    {
        return self::get(1, 'fixtures/statistics', $params);
    }

    //足球 - 赛事预测
    public static function predictions($params = [])
    {
        return self::get(1, 'predictions', $params);
    }


    /**********篮球**************** */
    //篮球 - 赛事
    public static function basketballGames($params = [])
    {
        return self::get(2, 'games', $params);
    }

    //篮球 - 球队统计数据
    public static function basketballStatisticsTeams($params = [])
    {
        return self::get(2, 'games/statistics/teams', $params);
    }

    //篮球 - 获取比赛或联赛的赔率
    public static function basketballOdds($params = [])
    {
        return self::get(2, 'odds', $params);
    }

    //篮球 - 获取可参加的联赛和杯赛列表
    public static function basketballLeagues($params = [])
    {
        return self::get(2, 'leagues', $params);
    }
}
