<?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',
        ];

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

        // ✅ 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
        $allPermissionNames = Permission::pluck('name')->toArray();
        $masterAdminRole->syncPermissions($allPermissionNames);

        // ✅ Give limited permissions to teacher (same as your admin role logic)
        $teacherRole->syncPermissions([
            'create role', 'view role', 'update role',
            'create permission', 'view permission',
            'create user', 'view user', 'update user',
        ]);

        // ✅ Student role (no permissions by default)
        $studentRole->syncPermissions([]);

        // ✅ 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();
    }
}