<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RolesController extends Controller
{
    public function index()
    {
        $roles = Role::all();
        return response()->json(['data' => $roles], 200);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:roles',
            'description' => 'nullable|string',
            'status' => 'boolean',
            'permissions' => 'nullable|array' // Changed from 'json' to 'array'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

        $role = Role::create([
            'name' => $request->name,
            'description' => $request->description,
            'status' => $request->status ?? true,
            'permissions' => $request->permissions // No need to json_decode since it's already an array
        ]);

        return response()->json(['data' => $role, 'message' => 'Role created successfully'], 201);
    }

    public function show($id)
    {
        $role = Role::find($id);
        
        if (!$role) {
            return response()->json(['error' => 'Role not found'], 404);
        }

        return response()->json(['data' => $role], 200);
    }

    public function update(Request $request, $id)
    {
        $role = Role::find($id);
        
        if (!$role) {
            return response()->json(['error' => 'Role not found'], 404);
        }

        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:roles,name,' . $id,
            'description' => 'nullable|string',
            'status' => 'boolean',
            'permissions' => 'nullable|array' // Changed from 'json' to 'array'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

        $role->update([
            'name' => $request->name,
            'description' => $request->description,
            'status' => $request->status,
            'permissions' => $request->permissions // No need to json_decode since it's already an array
        ]);

        return response()->json(['data' => $role, 'message' => 'Role updated successfully'], 200);
    }

    public function destroy($id)
    {
        $role = Role::find($id);
        
        if (!$role) {
            return response()->json(['error' => 'Role not found'], 404);
        }

        $role->delete();

        return response()->json(['message' => 'Role deleted successfully'], 200);
    }

public function search(Request $request)
{
    $search = $request->input('search');
    
    $roles = Role::when($search, function ($query, $search) {
        return $query->where('name', 'like', "%{$search}%");
    })
    ->where('status', 1)
    ->get();
    
    return response()->json(['data' => $roles]);
}
}