<?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": [
                    "View:AdminVerification",

                    "ViewAny:Announcement",
                    "View:Announcement",
                    "Create:Announcement",
                    "Update:Announcement",
                    "Delete:Announcement",
                    "Restore:Announcement",
                    "ForceDelete:Announcement",
                    "ForceDeleteAny:Announcement",
                    "RestoreAny:Announcement",
                    "Replicate:Announcement",
                    "Reorder:Announcement",

                    "ViewAny:Category",
                    "View:Category",
                    "Create:Category",
                    "Update:Category",
                    "Delete:Category",
                    "Restore:Category",
                    "ForceDelete:Category",
                    "ForceDeleteAny:Category",
                    "RestoreAny:Category",
                    "Replicate:Category",
                    "Reorder:Category",

                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",

                    "ViewAny:Contact",
                    "View:Contact",
                    "Create:Contact",
                    "Update:Contact",
                    "Delete:Contact",
                    "Restore:Contact",
                    "ForceDelete:Contact",
                    "ForceDeleteAny:Contact",
                    "RestoreAny:Contact",
                    "Replicate:Contact",
                    "Reorder:Contact",

                    "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",

                    "View:CooperationStatusChart",

                    "View:Dashboard",

                    "ViewAny:DataChangeRequest",
                    "View:DataChangeRequest",
                    "Create:DataChangeRequest",
                    "Update:DataChangeRequest",
                    "Delete:DataChangeRequest",
                    "Restore:DataChangeRequest",
                    "ForceDelete:DataChangeRequest",
                    "ForceDeleteAny:DataChangeRequest",
                    "RestoreAny:DataChangeRequest",
                    "Replicate:DataChangeRequest",
                    "Reorder:DataChangeRequest",

                    "ViewAny:Department",
                    "View:Department",
                    "Create:Department",
                    "Update:Department",
                    "Delete:Department",
                    "Restore:Department",
                    "ForceDelete:Department",
                    "ForceDeleteAny:Department",
                    "RestoreAny:Department",
                    "Replicate:Department",
                    "Reorder:Department",

                    "View:IssueLocationChart",

                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",

                    "View:JournalistRegistrationChart",

                    "View:LogTable",

                    "ViewAny:Location",
                    "View:Location",
                    "Create:Location",
                    "Update:Location",
                    "Delete:Location",
                    "Restore:Location",
                    "ForceDelete:Location",
                    "ForceDeleteAny:Location",
                    "RestoreAny:Location",
                    "Replicate:Location",
                    "Reorder:Location",

                    "View:ManageGeneralSettings",

                    "View:ManageSeoSettings",

                    "View:ManageSocialMediaSettings",

                    "View:MediaClassificationChart",

                    "View:MediaCooperationRankingChart",

                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",

                    "View:MediaRegistrationChart",

                    "View:MediaTypeChart",

                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News",

                    "View:OverviewStats",

                    "View:PopularNewsChart",

                    "ViewAny:Role",
                    "View:Role",
                    "Create:Role",
                    "Update:Role",
                    "Delete:Role",
                    "Restore:Role",
                    "ForceDelete:Role",
                    "ForceDeleteAny:Role",
                    "RestoreAny:Role",
                    "Replicate:Role",
                    "Reorder:Role",

                    "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",

                    "View:TaskRealizationChart",

                    "ViewAny:Theme",
                    "View:Theme",
                    "Create:Theme",
                    "Update:Theme",
                    "Delete:Theme",
                    "Restore:Theme",
                    "ForceDelete:Theme",
                    "ForceDeleteAny:Theme",
                    "RestoreAny:Theme",
                    "Replicate:Theme",
                    "Reorder:Theme",

                    "View:TopMediaJournalistChart",

                    "ViewAny:User",
                    "View:User",
                    "Create:User",
                    "Update:User",
                    "Delete:User",
                    "Restore:User",
                    "ForceDelete:User",
                    "ForceDeleteAny:User",
                    "RestoreAny:User",
                    "Replicate:User",
                    "Reorder:User",

                    "View:VerificationPerformanceChart",

                    "ViewAny:Visitor",
                    "View:Visitor",
                    "Create:Visitor",
                    "Update:Visitor",
                    "Delete:Visitor",
                    "Restore:Visitor",
                    "ForceDelete:Visitor",
                    "ForceDeleteAny:Visitor",
                    "RestoreAny:Visitor",
                    "Replicate:Visitor",
                    "Reorder:Visitor",

                    "View:VisitorTrafficChart"
                ]
            },
            {
                "name": "Administrator",
                "guard_name": "web",
                "permissions": [
                    "View:AdminVerification",

                    "ViewAny:Announcement",
                    "View:Announcement",
                    "Create:Announcement",
                    "Update:Announcement",
                    "Delete:Announcement",
                    "Restore:Announcement",
                    "ForceDelete:Announcement",
                    "ForceDeleteAny:Announcement",
                    "RestoreAny:Announcement",
                    "Replicate:Announcement",
                    "Reorder:Announcement",

                    "ViewAny:Category",
                    "View:Category",
                    "Create:Category",
                    "Update:Category",
                    "Delete:Category",
                    "Restore:Category",
                    "ForceDelete:Category",
                    "ForceDeleteAny:Category",
                    "RestoreAny:Category",
                    "Replicate:Category",
                    "Reorder:Category",

                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",

                    "ViewAny:Contact",
                    "View:Contact",
                    "Create:Contact",
                    "Update:Contact",
                    "Delete:Contact",
                    "Restore:Contact",
                    "ForceDelete:Contact",
                    "ForceDeleteAny:Contact",
                    "RestoreAny:Contact",
                    "Replicate:Contact",
                    "Reorder:Contact",

                    "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",

                    "View:CooperationStatusChart",

                    "View:Dashboard",

                    "ViewAny:DataChangeRequest",
                    "View:DataChangeRequest",
                    "Create:DataChangeRequest",
                    "Update:DataChangeRequest",
                    "Delete:DataChangeRequest",
                    "Restore:DataChangeRequest",
                    "ForceDelete:DataChangeRequest",
                    "ForceDeleteAny:DataChangeRequest",
                    "RestoreAny:DataChangeRequest",
                    "Replicate:DataChangeRequest",
                    "Reorder:DataChangeRequest",

                    "ViewAny:Department",
                    "View:Department",
                    "Create:Department",
                    "Update:Department",
                    "Delete:Department",
                    "Restore:Department",
                    "ForceDelete:Department",
                    "ForceDeleteAny:Department",
                    "RestoreAny:Department",
                    "Replicate:Department",
                    "Reorder:Department",

                    "View:IssueLocationChart",

                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",

                    "View:JournalistRegistrationChart",

                    "ViewAny:Location",
                    "View:Location",
                    "Create:Location",
                    "Update:Location",
                    "Delete:Location",
                    "Restore:Location",
                    "ForceDelete:Location",
                    "ForceDeleteAny:Location",
                    "RestoreAny:Location",
                    "Replicate:Location",
                    "Reorder:Location",

                    "View:ManageGeneralSettings",

                    "View:ManageSeoSettings",

                    "View:ManageSocialMediaSettings",

                    "View:MediaClassificationChart",

                    "View:MediaCooperationRankingChart",

                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",

                    "View:MediaRegistrationChart",

                    "View:MediaTypeChart",

                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News",

                    "View:OverviewStats",

                    "View:PopularNewsChart",

                    "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",

                    "View:TaskRealizationChart",

                    "ViewAny:Theme",
                    "View:Theme",
                    "Create:Theme",
                    "Update:Theme",
                    "Delete:Theme",
                    "Restore:Theme",
                    "ForceDelete:Theme",
                    "ForceDeleteAny:Theme",
                    "RestoreAny:Theme",
                    "Replicate:Theme",
                    "Reorder:Theme",

                    "View:TopMediaJournalistChart",

                    "ViewAny:User",
                    "View:User",
                    "Create:User",
                    "Update:User",
                    "Delete:User",
                    "Restore:User",
                    "ForceDelete:User",
                    "ForceDeleteAny:User",
                    "RestoreAny:User",
                    "Replicate:User",
                    "Reorder:User",

                    "View:VerificationPerformanceChart",

                    "ViewAny:Visitor",
                    "View:Visitor",
                    "Create:Visitor",
                    "Update:Visitor",
                    "Delete:Visitor",
                    "Restore:Visitor",
                    "ForceDelete:Visitor",
                    "ForceDeleteAny:Visitor",
                    "RestoreAny:Visitor",
                    "Replicate:Visitor",
                    "Reorder:Visitor",

                    "View:VisitorTrafficChart"
                ]
            },
            {
                "name": "Admin Monitoring",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",
                    
                    "View:Dashboard",

                    "ViewAny:IssueManagement",
                    "View:IssueManagement",
                    "Create:IssueManagement",
                    "Update:IssueManagement",
                    "Delete:IssueManagement",
                    "Restore:IssueManagement",
                    "ForceDelete:IssueManagement",
                    "ForceDeleteAny:IssueManagement",
                    "RestoreAny:IssueManagement",
                    "Replicate:IssueManagement",
                    "Reorder:IssueManagement",
                    
                    "ViewAny:Location",
                    "View:Location",
                    "Create:Location",
                    "Update:Location",
                    "Delete:Location",
                    "Restore:Location",
                    "ForceDelete:Location",
                    "ForceDeleteAny:Location",
                    "RestoreAny:Location",
                    "Replicate:Location",
                    "Reorder:Location",

                    "ViewAny:MediaMonitoring",
                    "View:MediaMonitoring",
                    "Create:MediaMonitoring",
                    "Update:MediaMonitoring",
                    "Delete:MediaMonitoring",
                    "Restore:MediaMonitoring",
                    "ForceDelete:MediaMonitoring",
                    "ForceDeleteAny:MediaMonitoring",
                    "RestoreAny:MediaMonitoring",
                    "Replicate:MediaMonitoring",
                    "Reorder:MediaMonitoring",

                    "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:Theme",
                    "View:Theme",
                    "Create:Theme",
                    "Update:Theme",
                    "Delete:Theme",
                    "Restore:Theme",
                    "ForceDelete:Theme",
                    "ForceDeleteAny:Theme",
                    "RestoreAny:Theme",
                    "Replicate:Theme",
                    "Reorder:Theme",

                    "ViewAny:Visitor",
                    "View:Visitor",
                    "Create:Visitor",
                    "Update:Visitor",
                    "Delete:Visitor",
                    "Restore:Visitor",
                    "ForceDelete:Visitor",
                    "ForceDeleteAny:Visitor",
                    "RestoreAny:Visitor",
                    "Replicate:Visitor",
                    "Reorder:Visitor"
                ]
            },
            {
                "name": "Admin Konten",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Category",

                    "View:Category",
                    "Create:Category",
                    "Update:Category",
                    "Delete:Category",
                    "Restore:Category",
                    "ForceDelete:Category",
                    "ForceDeleteAny:Category",
                    "RestoreAny:Category",
                    "Replicate:Category",
                    "Reorder:Category",

                    "ViewAny:Classification",
                    "View:Classification",
                    "Create:Classification",
                    "Update:Classification",
                    "Delete:Classification",
                    "Restore:Classification",
                    "ForceDelete:Classification",
                    "ForceDeleteAny:Classification",
                    "RestoreAny:Classification",
                    "Replicate:Classification",
                    "Reorder:Classification",

                    "ViewAny:ContentRecap",
                    "View:ContentRecap",
                    "Create:ContentRecap",
                    "Update:ContentRecap",
                    "Delete:ContentRecap",
                    "Restore:ContentRecap",
                    "ForceDelete:ContentRecap",
                    "ForceDeleteAny:ContentRecap",
                    "RestoreAny:ContentRecap",
                    "Replicate:ContentRecap",
                    "Reorder:ContentRecap",

                    "View:Dashboard",

                    "ViewAny:News",
                    "View:News",
                    "Create:News",
                    "Update:News",
                    "Delete:News",
                    "Restore:News",
                    "ForceDelete:News",
                    "ForceDeleteAny:News",
                    "RestoreAny:News",
                    "Replicate:News",
                    "Reorder:News"
                ]
            },
            {
                "name": "Admin Keuangan",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Cooperation",
                    "View:Cooperation",

                    "View:Dashboard"
                ]
            },
            {
                "name": "Perusahaan",
                "guard_name": "web",
                "permissions": [
                    "ViewAny:Cooperation",
                    "View:Cooperation",

                    "View:Company",

                    "View:Dashboard",

                    "ViewAny:DataChangeRequest",
                    "View:DataChangeRequest",

                    "ViewAny:Journalist",
                    "View:Journalist",
                    "Create:Journalist",
                    "Update:Journalist",
                    "Delete:Journalist",

                    "View:Media",

                    "View:Verification"
                ]
            }
        ]
        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'],
                    ]);
                }
            }
        }
    }
}
