<?php

namespace Database\Seeders;

use BezhanSalleh\FilamentShield\Support\Utils;
use Illuminate\Database\Seeder;
use Spatie\Permission\PermissionRegistrar;

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

        $rolesWithPermissions = <<<'JSON'
        [
            {
                "name": "Developer",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Role",
                    "View:Role",
                    "Create:Role",
                    "Update:Role",
                    "Delete:Role",
                    "Restore:Role",
                    "ForceDelete:Role",
                    "ForceDeleteAny:Role",
                    "RestoreAny:Role",
                    "Replicate:Role",
                    "Reorder:Role",
                    "ViewAny:Theme",
                    "View:Theme",
                    "Create:Theme",
                    "Update:Theme",
                    "Delete:Theme",
                    "Restore:Theme",
                    "ForceDelete:Theme",
                    "ForceDeleteAny:Theme",
                    "RestoreAny:Theme",
                    "Replicate:Theme",
                    "Reorder:Theme",
                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",
                    "ViewAny:Department",
                    "View:Department",
                    "Create:Department",
                    "Update:Department",
                    "Delete:Department",
                    "Restore:Department",
                    "ForceDelete:Department",
                    "ForceDeleteAny:Department",
                    "RestoreAny:Department",
                    "Replicate:Department",
                    "Reorder:Department",
                    "ViewAny:Location",
                    "View:Location",
                    "Create:Location",
                    "Update:Location",
                    "Delete:Location",
                    "Restore:Location",
                    "ForceDelete:Location",
                    "ForceDeleteAny:Location",
                    "RestoreAny:Location",
                    "Replicate:Location",
                    "Reorder:Location",
                    "ViewAny:SubClassification",
                    "View:SubClassification",
                    "Create:SubClassification",
                    "Update:SubClassification",
                    "Delete:SubClassification",
                    "Restore:SubClassification",
                    "ForceDelete:SubClassification",
                    "ForceDeleteAny:SubClassification",
                    "RestoreAny:SubClassification",
                    "Replicate:SubClassification",
                    "Reorder:SubClassification",
                    "ViewAny:SubLocation",
                    "View:SubLocation",
                    "Create:SubLocation",
                    "Update:SubLocation",
                    "Delete:SubLocation",
                    "Restore:SubLocation",
                    "ForceDelete:SubLocation",
                    "ForceDeleteAny:SubLocation",
                    "RestoreAny:SubLocation",
                    "Replicate:SubLocation",
                    "Reorder:SubLocation",
                    "ViewAny:User",
                    "View:User",
                    "Create:User",
                    "Update:User",
                    "Delete:User",
                    "Restore:User",
                    "ForceDelete:User",
                    "ForceDeleteAny:User",
                    "RestoreAny:User",
                    "Replicate:User",
                    "Reorder:User",
                    "ViewAny:Announcement",
                    "View:Announcement",
                    "Create:Announcement",
                    "Update:Announcement",
                    "Delete:Announcement",
                    "Restore:Announcement",
                    "ForceDelete:Announcement",
                    "ForceDeleteAny:Announcement",
                    "RestoreAny:Announcement",
                    "Replicate:Announcement",
                    "Reorder:Announcement",
                    "ViewAny:ContentRecap",
                    "View:ContentRecap",
                    "Create:ContentRecap",
                    "Update:ContentRecap",
                    "Delete:ContentRecap",
                    "Restore:ContentRecap",
                    "ForceDelete:ContentRecap",
                    "ForceDeleteAny:ContentRecap",
                    "RestoreAny:ContentRecap",
                    "Replicate:ContentRecap",
                    "Reorder:ContentRecap",
                    "ViewAny:Cooperation",
                    "View:Cooperation",
                    "Create:Cooperation",
                    "Update:Cooperation",
                    "Delete:Cooperation",
                    "Restore:Cooperation",
                    "ForceDelete:Cooperation",
                    "ForceDeleteAny:Cooperation",
                    "RestoreAny:Cooperation",
                    "Replicate:Cooperation",
                    "Reorder:Cooperation",
                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",
                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",
                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News"
                ]
            },
            {
                "name": "Administrator",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Role",
                    "View:Role",
                    "Create:Role",
                    "Update:Role",
                    "Delete:Role",
                    "Restore:Role",
                    "ForceDelete:Role",
                    "ForceDeleteAny:Role",
                    "RestoreAny:Role",
                    "Replicate:Role",
                    "Reorder:Role",
                    "ViewAny:Theme",
                    "View:Theme",
                    "Create:Theme",
                    "Update:Theme",
                    "Delete:Theme",
                    "Restore:Theme",
                    "ForceDelete:Theme",
                    "ForceDeleteAny:Theme",
                    "RestoreAny:Theme",
                    "Replicate:Theme",
                    "Reorder:Theme",
                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",
                    "ViewAny:Department",
                    "View:Department",
                    "Create:Department",
                    "Update:Department",
                    "Delete:Department",
                    "Restore:Department",
                    "ForceDelete:Department",
                    "ForceDeleteAny:Department",
                    "RestoreAny:Department",
                    "Replicate:Department",
                    "Reorder:Department",
                    "ViewAny:Location",
                    "View:Location",
                    "Create:Location",
                    "Update:Location",
                    "Delete:Location",
                    "Restore:Location",
                    "ForceDelete:Location",
                    "ForceDeleteAny:Location",
                    "RestoreAny:Location",
                    "Replicate:Location",
                    "Reorder:Location",
                    "ViewAny:SubClassification",
                    "View:SubClassification",
                    "Create:SubClassification",
                    "Update:SubClassification",
                    "Delete:SubClassification",
                    "Restore:SubClassification",
                    "ForceDelete:SubClassification",
                    "ForceDeleteAny:SubClassification",
                    "RestoreAny:SubClassification",
                    "Replicate:SubClassification",
                    "Reorder:SubClassification",
                    "ViewAny:SubLocation",
                    "View:SubLocation",
                    "Create:SubLocation",
                    "Update:SubLocation",
                    "Delete:SubLocation",
                    "Restore:SubLocation",
                    "ForceDelete:SubLocation",
                    "ForceDeleteAny:SubLocation",
                    "RestoreAny:SubLocation",
                    "Replicate:SubLocation",
                    "Reorder:SubLocation",
                    "ViewAny:User",
                    "View:User",
                    "Create:User",
                    "Update:User",
                    "Delete:User",
                    "Restore:User",
                    "ForceDelete:User",
                    "ForceDeleteAny:User",
                    "RestoreAny:User",
                    "Replicate:User",
                    "Reorder:User",
                    "ViewAny:Announcement",
                    "View:Announcement",
                    "Create:Announcement",
                    "Update:Announcement",
                    "Delete:Announcement",
                    "Restore:Announcement",
                    "ForceDelete:Announcement",
                    "ForceDeleteAny:Announcement",
                    "RestoreAny:Announcement",
                    "Replicate:Announcement",
                    "Reorder:Announcement",
                    "ViewAny:ContentRecap",
                    "View:ContentRecap",
                    "Create:ContentRecap",
                    "Update:ContentRecap",
                    "Delete:ContentRecap",
                    "Restore:ContentRecap",
                    "ForceDelete:ContentRecap",
                    "ForceDeleteAny:ContentRecap",
                    "RestoreAny:ContentRecap",
                    "Replicate:ContentRecap",
                    "Reorder:ContentRecap",
                    "ViewAny:Cooperation",
                    "View:Cooperation",
                    "Create:Cooperation",
                    "Update:Cooperation",
                    "Delete:Cooperation",
                    "Restore:Cooperation",
                    "ForceDelete:Cooperation",
                    "ForceDeleteAny:Cooperation",
                    "RestoreAny:Cooperation",
                    "Replicate:Cooperation",
                    "Reorder:Cooperation",
                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",
                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",
                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News"
                ]
            },
            {
                "name": "Admin Monitoring",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",
                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",
                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News",
                    "ViewAny:Classification",
                    "View:Classification",
                    "ViewAny:SubClassification",
                    "View:SubClassification",
                    "ViewAny:Location",
                    "View:Location",
                    "ViewAny:SubLocation",
                    "View:SubLocation",
                    "ViewAny:Department",
                    "View:Department"
                ]
            },
            {
                "name": "Admin Konten",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:ContentRecap",
                    "View:ContentRecap",
                    "Create:ContentRecap",
                    "Update:ContentRecap",
                    "Delete:ContentRecap",
                    "Restore:ContentRecap",
                    "ForceDelete:ContentRecap",
                    "ForceDeleteAny:ContentRecap",
                    "RestoreAny:ContentRecap",
                    "Replicate:ContentRecap",
                    "Reorder:ContentRecap",
                    "ViewAny:Announcement",
                    "View:Announcement",
                    "Create:Announcement",
                    "Update:Announcement",
                    "Delete:Announcement",
                    "Restore:Announcement",
                    "ForceDelete:Announcement",
                    "ForceDeleteAny:Announcement",
                    "RestoreAny:Announcement",
                    "Replicate:Announcement",
                    "Reorder:Announcement",
                    "ViewAny:Classification",
                    "View:Classification",
                    "ViewAny:SubClassification",
                    "View:SubClassification",
                    "ViewAny:Theme",
                    "View:Theme",
                    "ViewAny:News",
                    "View:News"
                ]
            },
            {
                "name": "Admin Keuangan",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Cooperation",
                    "View:Cooperation",
                    "Create:Cooperation",
                    "Update:Cooperation",
                    "Delete:Cooperation",
                    "Restore:Cooperation",
                    "ForceDelete:Cooperation",
                    "ForceDeleteAny:Cooperation",
                    "RestoreAny:Cooperation",
                    "Replicate:Cooperation",
                    "Reorder:Cooperation"
                ]
            },
            {
                "name": "Perusahaan",
                "guard_name": "web",
                "permissions": [
                    "View:CustomCompany",
                    "View:CustomMedia",
                    "View:CustomVerification",
                    "ViewAny:Cooperation",
                    "View:Cooperation",
                    "Create:Cooperation",
                    "Update:Cooperation",
                    "Delete:Cooperation",
                    "Restore:Cooperation",
                    "ForceDelete:Cooperation",
                    "ForceDeleteAny:Cooperation",
                    "RestoreAny:Cooperation",
                    "Replicate:Cooperation",
                    "Reorder:Cooperation",
                    "ViewAny:Journalist",
                    "View:Journalist",
                    "Create:Journalist",
                    "Update:Journalist",
                    "Delete:Journalist",
                    "Restore:Journalist",
                    "ForceDelete:Journalist",
                    "ForceDeleteAny:Journalist",
                    "RestoreAny:Journalist",
                    "Replicate:Journalist",
                    "Reorder:Journalist"
                ]
            }
        ]
        JSON;

        $directPermissions = '[]';

        static::makeRolesWithPermissions($rolesWithPermissions);
        static::makeDirectPermissions($directPermissions);

        $this->command->info('Shield Seeding Completed.');
    }

    protected static function makeRolesWithPermissions(string $rolesWithPermissions): void
    {
        if (! blank($rolePlusPermissions = json_decode($rolesWithPermissions, true))) {
            /** @var Model $roleModel */
            $roleModel = Utils::getRoleModel();
            /** @var Model $permissionModel */
            $permissionModel = Utils::getPermissionModel();

            foreach ($rolePlusPermissions as $rolePlusPermission) {
                $role = $roleModel::firstOrCreate([
                    'name' => $rolePlusPermission['name'],
                    'guard_name' => $rolePlusPermission['guard_name'],
                ]);

                if (! blank($rolePlusPermission['permissions'])) {
                    $permissionModels = collect($rolePlusPermission['permissions'])
                        ->map(fn ($permission) => $permissionModel::firstOrCreate([
                            'name' => $permission,
                            'guard_name' => $rolePlusPermission['guard_name'],
                        ]))
                        ->all();

                    $role->syncPermissions($permissionModels);
                }
            }
        }
    }

    public static function makeDirectPermissions(string $directPermissions): void
    {
        if (! blank($permissions = json_decode($directPermissions, true))) {
            /** @var Model $permissionModel */
            $permissionModel = Utils::getPermissionModel();

            foreach ($permissions as $permission) {
                if ($permissionModel::whereName($permission)->doesntExist()) {
                    $permissionModel::create([
                        'name' => $permission['name'],
                        'guard_name' => $permission['guard_name'],
                    ]);
                }
            }
        }
    }
}
