<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers\Api;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Modules\SportsActivity\Services\PricingCalculatorService;

class BookingApiController extends Controller
{
    public function pricePreview(Request $request): Response
    {
        $unitId = (int) $request->get('unit_id', 0);
        $date = $request->get('date', '');
        $startTime = $request->get('start_time', '');
        $endTime = $request->get('end_time', '');
        $participants = (int) $request->get('participants', 1);
        $isMember = (bool) $request->get('is_member', 0);

        if (!$unitId || !$date || !$startTime || !$endTime) {
            return $this->json(['error' => 'معاملات ناقصة'], 400);
        }

        $pricing = PricingCalculatorService::calculate($unitId, $date, $startTime, $endTime, $participants, $isMember);

        if (!$pricing) {
            return $this->json(['error' => 'لا توجد قاعدة تسعير لهذه المعاملات', 'pricing' => null]);
        }

        return $this->json(['pricing' => $pricing]);
    }
}
