<?php

namespace App\Http\Middleware;

use App\Enums\UserRole;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureUserIsBoatOwner
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (!$request->user()) {
            return response()->json([
                'message' => 'Kimlik doğrulama gerekli.',
            ], 401);
        }

        $user = $request->user();
        
        // Allow both boat owners and admins
        if ($user->role !== UserRole::BOAT_OWNER && $user->role !== UserRole::ADMIN) {
            return response()->json([
                'message' => 'Bu işlem için tekne sahibi olmanız gerekiyor.',
            ], 403);
        }

        return $next($request);
    }
}
