<?php

namespace Database\Seeders;

use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        DB::disableQueryLog();
        Model::unsetEventDispatcher();

        $chunkSize = 50;
        $tableName = 'permissions';

        // Define all modules and their permissions
        $modules = [
            // Core
            'admins'     => ['list', 'view', 'create', 'edit', 'delete'],
            'roles'      => ['list', 'view', 'create', 'edit', 'delete'],
            'users'      => ['list', 'view', 'create', 'edit', 'delete'],
            'countries'  => ['list', 'view', 'create', 'edit', 'delete'],
            'cities'     => ['list', 'view', 'create', 'edit', 'delete'],
            'areas'      => ['list', 'view', 'create', 'edit', 'delete'],
            'languages'  => ['list', 'view', 'create', 'edit', 'delete'],
            'settings'   => ['view', 'edit'],
            'banners'    => ['list', 'view', 'create', 'edit', 'delete'],
            'categories' => ['list', 'view', 'create', 'edit', 'delete'],
            'championships' => ['list', 'view', 'create', 'edit', 'delete'],

            // Restaurant Module
            'restaurants'        => ['list', 'view', 'create', 'edit', 'delete', 'approve'],
            'restaurant_owners'  => ['list', 'view', 'create', 'edit', 'delete'],
            'cuisine_types'      => ['list', 'view', 'create', 'edit', 'delete'],
            'menu_categories'    => ['list', 'view', 'create', 'edit', 'delete'],
            'menu_items'         => ['list', 'view', 'create', 'edit', 'delete'],
            'food_orders'        => ['list', 'view', 'edit', 'delete', 'cancel'],
            'delivery_persons'   => ['list', 'view', 'create', 'edit', 'delete'],
            'restaurant_coupons' => ['list', 'view', 'create', 'edit', 'delete'],
            'restaurant_reviews' => ['list', 'view', 'approve', 'reject', 'delete'],

            // Reports
            'reports' => ['view', 'export'],
        ];

        $rows = [];

        $upsert = function (array $records) use ($tableName) {
            foreach ($records as $record) {
                DB::table($tableName)->updateOrInsert(
                    ['name' => $record['name'], 'guard_name' => $record['guard_name']],
                    $record
                );
            }
        };

        $count = 0;
        foreach ($modules as $module => $actions) {
            foreach ($actions as $action) {
                $now = CarbonImmutable::now();

                $rows[] = [
                    'name'       => "{$module}.{$action}",
                    'guard_name' => 'admin',
                    'created_at' => $now,
                    'updated_at' => $now,
                ];

                $count++;

                if (count($rows) >= $chunkSize) {
                    $upsert($rows);
                    $rows = [];
                }
            }
        }

        if (!empty($rows)) {
            $upsert($rows);
        }

        // Clear Spatie permission cache
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        $this->command->info("Permissions seeded: {$count} records");
    }
}
