<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\PromotionalWheelService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PromotionalWheelController extends Controller
{
    public function spin(Request $request): JsonResponse
    {
        $request->validate([
            'order_type' => 'required|string|in:food,gas,taxi,trip,extra_service',
            'order_id' => 'required|integer|min:1',
        ]);

        try {
            $data = PromotionalWheelService::spinForUser(
                Auth::user(),
                $request->string('order_type')->toString(),
                (int) $request->input('order_id')
            );

            return response()->json([
                'success' => true,
                'message' => 'تم تدوير العجلة',
                'data' => $data,
            ]);
        } catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage() ?: 'غير مصرح',
            ], $e->getStatusCode());
        } catch (\Throwable $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage(),
            ], 400);
        }
    }
}
