<?php

namespace Database\Seeders;

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

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

        $permissions = [
            'access admin panel',
            'vendors.view',
            'vendors.manage',
            'vendors.test',
            'plans.view',
            'plans.manage',
            'mappings.manage',
            'transactions.view',
            'transactions.requery',
            'transactions.refund',
            'reports.view',
            'coupons.view',
            'coupons.manage',
            'cashback.view',
            'cashback.manage',
            'tickets.view',
            'tickets.manage',
            'tickets.create',
        ];

        foreach ($permissions as $permissionName) {
            Permission::findOrCreate($permissionName, 'web');
        }

        $superAdmin = Role::findOrCreate('super-admin', 'web');
        $admin = Role::findOrCreate('admin', 'web');
        $support = Role::findOrCreate('support', 'web');
        $customer = Role::findOrCreate('customer', 'web');

        $superAdmin->syncPermissions(Permission::all());

        $admin->syncPermissions([
            'access admin panel',
            'vendors.view',
            'vendors.manage',
            'vendors.test',
            'plans.view',
            'plans.manage',
            'mappings.manage',
            'transactions.view',
            'transactions.requery',
            'transactions.refund',
            'reports.view',
            'coupons.view',
            'coupons.manage',
            'cashback.view',
            'cashback.manage',
            'tickets.view',
            'tickets.manage',
        ]);

        $support->syncPermissions([
            'access admin panel',
            'transactions.view',
            'coupons.view',
            'cashback.view',
            'tickets.view',
            'tickets.manage',
        ]);

        $customer->syncPermissions([
            'tickets.create',
        ]);
    }
}
