<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class PermissionsSeeder extends Seeder
{
    /**
     * A starter capability set so role management is real, not cosmetic.
     * `access_admin` is honoured by User::canAccessPanel(); the rest are
     * assignable now and ready to gate individual resources later.
     */
    public const PERMISSIONS = [
        'access_admin' => 'Access the admin panel',
        'manage_users' => 'Create, edit and delete members',
        'manage_roles' => 'Manage roles and permissions',
        'manage_content' => 'Manage posts, guides and testimonials',
        'manage_courses' => 'Manage courses and lessons',
        'manage_assessments' => 'Manage assessments and questions',
        'manage_consultations' => 'View and manage consultations & inquiries',
        'manage_leads' => 'View and export captured leads',
        'view_analytics' => 'View the analytics dashboard',
    ];

    public function run(): void
    {
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        foreach (array_keys(self::PERMISSIONS) as $name) {
            Permission::findOrCreate($name, 'web');
        }

        // Admins get everything; members get nothing extra (front-end only).
        Role::findOrCreate('admin', 'web')->syncPermissions(array_keys(self::PERMISSIONS));
        Role::findOrCreate('member', 'web');

        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
}
