<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class UserRolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        // ✅ Clear Spatie permission cache (important)
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        // ✅ PERMISSIONS (use firstOrCreate so seeder can be re-run safely)
        $permissions = [
            // role
            'view role',
            'create role',
            'update role',
            'delete role',

            // permission
            'view permission',
            'create permission',
            'update permission',
            'delete permission',

            // user
            'view user',
            'create user',
            'update user',
            'delete user',

            // lectures
            'lectures view',
            'lectures create',
            'lectures update',
            'lectures delete',
        ];

        foreach ($permissions as $perm) {
            Permission::firstOrCreate(
                ['name' => $perm, 'guard_name' => 'web'],
                ['name' => $perm, 'guard_name' => 'web']
            );
        }

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

        // ✅ ROLES (master-admin same behavior as super-admin)
        $masterAdminRole = Role::firstOrCreate(['name' => 'master-admin', 'guard_name' => 'web']);
        $teacherRole     = Role::firstOrCreate(['name' => 'teacher', 'guard_name' => 'web']);
        $studentRole     = Role::firstOrCreate(['name' => 'student', 'guard_name' => 'web']);

        // ✅ Give ALL permissions to master-admin
        $allPermissionIds = Permission::query()
            ->where('guard_name', 'web')
            ->pluck('id')
            ->all();
        $masterAdminRole->permissions()->sync($allPermissionIds);

        // ✅ Give limited permissions to teacher (same as your admin role logic)
        $teacherRole->permissions()->sync(
            Permission::query()
                ->where('guard_name', 'web')
                ->whereIn('name', [
                    'create role', 'view role', 'update role',
                    'create permission', 'view permission',
                    'create user', 'view user', 'update user',
                    'lectures view', 'lectures create', 'lectures update', 'lectures delete',
                ])
                ->pluck('id')
                ->all()
        );

        // ✅ Student role (no permissions by default)
        $studentRole->permissions()->sync(
            Permission::query()
                ->where('guard_name', 'web')
                ->where('name', 'lectures view')
                ->pluck('id')
                ->all()
        );

        // ✅ USERS + ROLE ASSIGN (firstOrCreate safe)
        $masterAdminUser = User::firstOrCreate(
            ['email' => 'masteradmin@gmail.com'],
            [
                'name' => 'Master Admin',
                'email' => 'masteradmin@gmail.com',
                'password' => Hash::make('12345678'),
            ]
        );
        $masterAdminUser->syncRoles([$masterAdminRole]);

        $teacherUser = User::firstOrCreate(
            ['email' => 'teacher@gmail.com'],
            [
                'name' => 'Teacher',
                'email' => 'teacher@gmail.com',
                'password' => Hash::make('12345678'),
            ]
        );
        $teacherUser->syncRoles([$teacherRole]);

        $studentUser = User::firstOrCreate(
            ['email' => 'student@gmail.com'],
            [
                'name' => 'Student',
                'email' => 'student@gmail.com',
                'password' => Hash::make('12345678'),
            ]
        );
        $studentUser->syncRoles([$studentRole]);

        // ✅ Clear cache again (optional but safe)
        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
}
