<?php

namespace App\Http\Controllers\Admin;

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

class PermissionController extends Controller
{
    /**
     * Afficher la liste des permissions
     */
    public function index()
    {
        $permissions = Permission::orderBy('group')->orderBy('name')->get()->groupBy('group');
        $roles = Role::all();
        
        return view('admin.permissions.index', compact('permissions', 'roles'));
    }

    /**
     * Afficher le formulaire de création
     */
    public function create()
    {
        $groups = Permission::distinct('group')->pluck('group')->filter();
        return view('admin.permissions.create', compact('groups'));
    }

    /**
     * Créer une nouvelle permission
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:permissions',
            'slug' => 'required|string|max:255|unique:permissions',
            'group' => 'nullable|string|max:255',
            'description' => 'nullable|string|max:500',
        ]);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }

        Permission::create([
            'name' => $request->name,
            'slug' => $request->slug,
            'group' => $request->group ?? 'general',
            'description' => $request->description,
        ]);

        return redirect()->route('admin.permissions.index')
            ->with('success', 'Permission créée avec succès.');
    }

    /**
     * Afficher une permission
     */
    public function show(Permission $permission)
    {
        $permission->load('roles');
        return view('admin.permissions.show', compact('permission'));
    }

    /**
     * Afficher le formulaire de modification
     */
    public function edit(Permission $permission)
    {
        $groups = Permission::distinct('group')->pluck('group')->filter();
        return view('admin.permissions.edit', compact('permission', 'groups'));
    }

    /**
     * Mettre à jour une permission
     */
    public function update(Request $request, Permission $permission)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:permissions,name,' . $permission->id,
            'slug' => 'required|string|max:255|unique:permissions,slug,' . $permission->id,
            'group' => 'nullable|string|max:255',
            'description' => 'nullable|string|max:500',
        ]);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }

        $permission->update([
            'name' => $request->name,
            'slug' => $request->slug,
            'group' => $request->group ?? 'general',
            'description' => $request->description,
        ]);

        return redirect()->route('admin.permissions.index')
            ->with('success', 'Permission mise à jour avec succès.');
    }

    /**
     * Supprimer une permission
     */
    public function destroy(Permission $permission)
    {
        // Vérifier si la permission est système (non supprimable)
        if ($permission->is_system ?? false) {
            return back()->with('error', 'Cette permission système ne peut pas être supprimée.');
        }

        $permission->delete();

        return back()->with('success', 'Permission supprimée avec succès.');
    }
}
