<?php
declare(strict_types=1);

namespace App\Modules\Reservations\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\App;
use App\Modules\Reservations\Models\PrivateMatchBooking;
use App\Modules\Reservations\Services\PrivateMatchService;

class PrivateMatchController extends Controller
{
    public function index(Request $request): mixed
    {
        $this->authorize('reservation.view');
        $filters = [
            'status'      => $_GET['status'] ?? '',
            'date_from'   => $_GET['date_from'] ?? '',
            'date_to'     => $_GET['date_to'] ?? '',
            'facility_id' => $_GET['facility_id'] ?? '',
        ];
        $bookings = PrivateMatchBooking::search($filters);
        $matchTypes = PrivateMatchBooking::getMatchTypes();
        return $this->view('Reservations.Views.private_matches', compact('bookings', 'filters', 'matchTypes'));
    }

    public function create(Request $request): mixed
    {
        $this->authorize('reservation.create');
        $db = App::getInstance()->db();
        $facilities = $db->select("SELECT id, name_ar, facility_type FROM facilities WHERE is_archived = 0 AND facility_type IN ('pitch','court','hall') ORDER BY name_ar");
        $matchTypes = PrivateMatchBooking::getMatchTypes();
        return $this->view('Reservations.Views.private_match_create', compact('facilities', 'matchTypes'));
    }

    public function store(Request $request): mixed
    {
        $this->authorize('reservation.create');
        $data = $_POST;

        $this->validate($data, [
            'facility_id'  => 'required',
            'booking_date' => 'required',
            'start_time'   => 'required',
            'end_time'     => 'required',
        ]);

        try {
            $bookingId = PrivateMatchService::createBooking($data);
            return $this->redirect('/private-matches')->withSuccess('تم حجز الملعب بنجاح');
        } catch (\RuntimeException $e) {
            return $this->redirect('/private-matches/create')->withError($e->getMessage());
        }
    }

    public function confirm(Request $request, int $id): mixed
    {
        $this->authorize('reservation.create');
        PrivateMatchService::confirmBooking($id);
        return $this->redirect('/private-matches')->withSuccess('تم تأكيد الحجز');
    }

    public function cancel(Request $request, int $id): mixed
    {
        $this->authorize('reservation.create');
        $reason = $_POST['reason'] ?? '';
        PrivateMatchService::cancelBooking($id, $reason);
        return $this->redirect('/private-matches')->withSuccess('تم إلغاء الحجز');
    }

    public function complete(Request $request, int $id): mixed
    {
        $this->authorize('reservation.create');
        PrivateMatchService::completeBooking($id);
        return $this->redirect('/private-matches')->withSuccess('تم إتمام المباراة');
    }

    public function recordPayment(Request $request, int $id): mixed
    {
        $this->authorize('reservation.create');
        $amount = (float) ($_POST['amount'] ?? 0);
        if ($amount <= 0) {
            return $this->redirect('/private-matches')->withError('المبلغ غير صحيح');
        }
        PrivateMatchService::recordPayment($id, $amount);
        return $this->redirect('/private-matches')->withSuccess('تم تسجيل الدفعة');
    }
}
