<?php

namespace App\Http\Controllers\Superadmin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $roles = Role::with('users')->get();
        return view('superadmin.roles.index', compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('superadmin.roles.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:50|unique:roles',
            'slug' => 'required|string|max:50|unique:roles|regex:/^[a-z_]+$/',
            'description' => 'nullable|string|max:255',
        ], [
            'name.required' => 'Nama role harus diisi',
            'name.unique' => 'Nama role sudah terdaftar',
            'slug.required' => 'Slug harus diisi',
            'slug.unique' => 'Slug sudah terdaftar',
            'slug.regex' => 'Slug hanya boleh mengandung huruf kecil dan underscore',
        ]);

        Role::create($validated);

        return redirect()->route('superadmin.roles.index')
            ->with('success', 'Role berhasil ditambahkan');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Role $role)
    {
        return view('superadmin.roles.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Role $role)
    {
        // Prevent editing critical roles
        if (in_array($role->slug, ['superadmin', 'admin', 'user'])) {
            return back()->with('error', 'Role sistem tidak dapat diubah');
        }

        $validated = $request->validate([
            'name' => ['required', 'string', 'max:50', Rule::unique('roles')->ignore($role->id)],
            'slug' => ['required', 'string', 'max:50', 'regex:/^[a-z_]+$/', Rule::unique('roles')->ignore($role->id)],
            'description' => 'nullable|string|max:255',
        ], [
            'name.required' => 'Nama role harus diisi',
            'name.unique' => 'Nama role sudah terdaftar',
            'slug.required' => 'Slug harus diisi',
            'slug.unique' => 'Slug sudah terdaftar',
            'slug.regex' => 'Slug hanya boleh mengandung huruf kecil dan underscore',
        ]);

        $role->update($validated);

        return redirect()->route('superadmin.roles.index')
            ->with('success', 'Role berhasil diperbarui');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Role $role)
    {
        // Prevent deleting critical roles
        if (in_array($role->slug, ['superadmin', 'admin', 'user'])) {
            return back()->with('error', 'Role sistem tidak dapat dihapus');
        }

        // Prevent deleting role that has users
        if ($role->users()->exists()) {
            return back()->with('error', 'Role tidak dapat dihapus karena masih memiliki user');
        }

        $role->delete();

        return redirect()->route('superadmin.roles.index')
            ->with('success', 'Role berhasil dihapus');
    }
}
