<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RolePermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Create permissions
        $permissions = [
            // Admin dashboard
            'admin.dashboard',

            // User management
            'users.manager',
            'users.menu',
            'users.dashboard',
            'users.view',
            'users.create',
            'users.edit',
            'users.delete',

            // Role management
            'roles.manager',
            'roles.menu',
            'roles.dashboard',
            'roles.view',
            'roles.create',
            'roles.edit',
            'roles.delete',

            // Mattock ===================================================
            // Article management
            'articles.mattock.manager',
            'articles.mattock.menu',
            'articles.mattock.dashboard',
            'articles.mattock.view',
            'articles.mattock.create',
            'articles.mattock.edit',
            'articles.mattock.delete',
            'articles.mattock.publish',
            'articles.mattock.owner.view',
            'articles.mattock.owner.edit',
            'articles.mattock.owner.delete',
            'articles.mattock.owner.publish',

            // Category management
            'categories.mattock.manager',
            'categories.mattock.menu',
            'categories.mattock.dashboard',
            'categories.mattock.view',
            'categories.mattock.create',
            'categories.mattock.edit',
            'categories.mattock.delete',
            // End Mattock ===============================================

            // Pipopa ====================================================
            // Article management
            'articles.pipopa.manager',
            'articles.pipopa.menu',
            'articles.pipopa.dashboard',
            'articles.pipopa.view',
            'articles.pipopa.create',
            'articles.pipopa.edit',
            'articles.pipopa.delete',
            'articles.pipopa.publish',
            'articles.pipopa.owner.view',
            'articles.pipopa.owner.edit',
            'articles.pipopa.owner.delete',
            'articles.pipopa.owner.publish',

            // Category management
            'categories.pipopa.manager',
            'categories.pipopa.menu',
            'categories.pipopa.dashboard',
            'categories.pipopa.view',
            'categories.pipopa.create',
            'categories.pipopa.edit',
            'categories.pipopa.delete',
            // End Pipopa ================================================
        ];

        foreach ($permissions as $permission) {
            Permission::create(['name' => $permission]);
        }

        // Create roles and assign permissions

        // Administrator role - has all permissions
        $adminRole = Role::create(['name' => 'administrator']);
        $adminRole->givePermissionTo(Permission::all());

        // Editor role - can manage all articles and categories
        $editorRole = Role::create(['name' => 'editor']);
        $editorRole->givePermissionTo([
            'admin.dashboard',
            'users.manager',
            'users.menu',
            'users.dashboard',
            'users.view',
            'users.create',
            'users.edit',
            'users.delete',
            'articles.mattock.manager',
            'articles.mattock.menu',
            'articles.mattock.dashboard',
            'articles.mattock.view',
            'articles.mattock.create',
            'articles.mattock.edit',
            'articles.mattock.delete',
            'articles.mattock.publish',
            'articles.mattock.owner.view',
            'articles.mattock.owner.edit',
            'articles.mattock.owner.delete',
            'articles.mattock.owner.publish',
            'categories.mattock.manager',
            'categories.mattock.menu',
            'categories.mattock.dashboard',
            'categories.mattock.view',
            'categories.mattock.create',
            'categories.mattock.edit',
            'categories.mattock.delete',
            'articles.pipopa.manager',
            'articles.pipopa.menu',
            'articles.pipopa.dashboard',
            'articles.pipopa.view',
            'articles.pipopa.create',
            'articles.pipopa.edit',
            'articles.pipopa.delete',
            'articles.pipopa.publish',
            'articles.pipopa.owner.view',
            'articles.pipopa.owner.edit',
            'articles.pipopa.owner.delete',
            'articles.pipopa.owner.publish',
            'categories.pipopa.manager',
            'categories.pipopa.menu',
            'categories.pipopa.dashboard',
            'categories.pipopa.view',
            'categories.pipopa.create',
            'categories.pipopa.edit',
            'categories.pipopa.delete',
        ]);

        // Contributor role - has all permissions util role management
        $contributorRole = Role::create(['name' => 'contributor']);
        $contributorRole->givePermissionTo([
            'admin.dashboard',
            'articles.mattock.manager',
            'articles.mattock.menu',
            'articles.mattock.dashboard',
            'articles.mattock.view',
            'articles.mattock.create',
            'articles.mattock.edit',
            'articles.mattock.delete',
            'articles.mattock.publish',
            'articles.mattock.owner.view',
            'articles.mattock.owner.edit',
            'articles.mattock.owner.delete',
            'articles.mattock.owner.publish',
            'categories.mattock.manager',
            'categories.mattock.menu',
            'categories.mattock.dashboard',
            'categories.mattock.view',
            'categories.mattock.create',
            'categories.mattock.edit',
            'categories.mattock.delete',
            'articles.pipopa.manager',
            'articles.pipopa.menu',
            'articles.pipopa.dashboard',
            'articles.pipopa.view',
            'articles.pipopa.create',
            'articles.pipopa.edit',
            'articles.pipopa.delete',
            'articles.pipopa.publish',
            'articles.pipopa.owner.view',
            'articles.pipopa.owner.edit',
            'articles.pipopa.owner.delete',
            'articles.pipopa.owner.publish',
            'categories.pipopa.manager',
            'categories.pipopa.menu',
            'categories.pipopa.dashboard',
            'categories.pipopa.view',
            'categories.pipopa.create',
            'categories.pipopa.edit',
            'categories.pipopa.delete',
        ]);

        // Author role - can manage their own articles
        $authorRole = Role::create(['name' => 'author']);
        $authorRole->givePermissionTo([
            'admin.dashboard',
            'articles.mattock.menu',
            'articles.mattock.dashboard',
            'articles.mattock.create',
            'articles.mattock.owner.view',
            'articles.mattock.owner.edit',
            'articles.mattock.owner.delete',
            'articles.mattock.owner.publish',
            'articles.pipopa.menu',
            'articles.pipopa.dashboard',
            'articles.pipopa.create',
            'articles.pipopa.owner.view',
            'articles.pipopa.owner.edit',
            'articles.pipopa.owner.delete',
            'articles.pipopa.owner.publish',
            'categories.mattock.view',
            'categories.pipopa.view',
        ]);

        // Subscriber role - can only create new articles
        $subscriberRole = Role::create(['name' => 'subscriber']);
        $subscriberRole->givePermissionTo([
            'admin.dashboard',
            'articles.mattock.create',
            'articles.pipopa.create',
            'categories.mattock.view',
            'categories.pipopa.view',
        ]);

        // Assign Admin role to existing users with email containing 'admin' or first user
        $users = User::all();
        if ($users->count() === 0) {
            User::create([
                'name'              => 'Admin',
                'email'             => 'admin@admin.dev',
                'password'          => bcrypt('abc1234$'),
                'email_verified_at' => now(),
            ]);
            $users = User::all();
        }

        foreach ($users as $user) {
            if (str_contains(strtolower($user->email), 'admin')) {
                $user->assignRole('administrator');
            } elseif ($user->id === 1) {
                $user->assignRole('administrator'); // First user is admin
            } else {
                $user->assignRole('subscriber'); // Default role
            }
        }
    }
}
