<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\MatchModel;

class MatchController extends BaseController
{
    public function getMatches(Request $request) {
        try {
            $itemsPerPage = min((int) $request->input('per_page', 10), 100);
            $currentPage = $request->input('page', 1);

            $matches = MatchModel::with('streamingUrls')->where('status', 0)->orderBy('created_at', 'desc')->paginate($itemsPerPage, ['*'], 'page', $currentPage); 

            if ($matches->isEmpty()) {
                return $this->formatPaginatedResponse([], 'No matches found', $currentPage, $itemsPerPage, $matches->total());
            }

            return $this->formatPaginatedResponse($matches->items(), 'Matches retrieved successfully', $currentPage, $itemsPerPage, $matches->total());

        }catch(\Exception $e){
            return $this->handleException($e);
        }
 
    }
}
