<?php

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

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    // public function run()
    // {
    //     // Create main module permission
    //     $operation = Permission::create(['name' => 'operation', 'guard_name' => 'web', 'parent_id' => 0]);

    //     // Create sub-module permissions
    //     $permissions = [
    //         ['name' => 'customers', 'parent_id' => $operation->id],
    //         ['name' => 'purchase', 'parent_id' => $operation->id],
    //         ['name' => 'sale', 'parent_id' => $operation->id],
    //         ['name' => 'service purchase', 'parent_id' => $operation->id]
    //     ];

    //     foreach ($permissions as $permissionData) {
    //         Permission::create(array_merge($permissionData, ['guard_name' => 'web']));
    //     }

    //     // Create roles and assign existing permissions
    //     $testingRole = Role::create(['name' => 'testing']);
    //     $testingRole->givePermissionTo(['operation', 'customers', 'purchase']);

    //     // $testingRole->givePermissionTo(Permission::all());

    //     // $editorRole = Role::create(['name' => 'editor']);
    //     // $editorRole->givePermissionTo(['customers', 'purchase']);
    // }

    // public function run()
    // {
    //     // Create main module permission
    //     // $moduels = Permission::create(['name' => 'preferences', 'guard_name' => 'web', 'parent_id' => 0]);

    //     // Define sub-modules and their associated permissions
    //     $subModules = [
    //         'Employees',
    //         'Product Categories',
    //         'invoice preference',
    //         'Unit Types',
    //     ];

    //     $additionalPermissions = ['list', 'edit', 'delete'];

    //     foreach ($subModules as $subModule) {
    //         // Create sub-module permission
    //         $subModulePermission = Permission::create([
    //             'name' => $subModule,
    //             'guard_name' => 'web',
    //             'parent_id' => $moduels->id
    //         ]);

    //         // Create additional permissions for each sub-module
    //         foreach ($additionalPermissions as $action) {
    //             Permission::create([
    //                 'name' => "{$subModule} {$action}",
    //                 'guard_name' => 'web',
    //                 'parent_id' => $subModulePermission->id
    //             ]);
    //         }
    //     }

    //     // Create roles and assign existing permissions
    //     $adminRole = Role::create(['name' => 'super-admin']);
    //     $adminRole->givePermissionTo(Permission::all());

    //     // $editorRole = Role::create(['name' => 'editor']);
    //     // $editorRole->givePermissionTo([
    //     //     'customers list', 'customers edit', 'customers delete', 'customers view',
    //     //     'purchase list', 'purchase edit'
    //     // ]);
    // }



    public function run()
    {
        // // Truncate the permissions table
        // Schema::disableForeignKeyConstraints();
        // Permission::truncate();
        // Schema::enableForeignKeyConstraints();

        // // Define modules and sub-modules
        // $modules = [
        //     'Dashboard' => [],
        //     'Operations' => [
        //         'Customers' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Purchase' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Sale' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Service Purchase' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        //     'Inventory' => [
        //         'Brands' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Products' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Stocks' => ['list', 'add', 'edit', 'delete', 'view', 'increase', 'damage', 'transfer'],
        //         'Supplier' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Sale Rate Application' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        //     'Accounts' => [
        //         'Chart of Account' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Control Account' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Sub Control Account' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Transaction Action' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Goods Receipts Billing / B-Cheque' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Goods Receipts' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Journal Vouchers' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Payment Voucher' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Receivables / Payables' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'General Ledger' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Payments Invoice' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        //     'Report' => [
        //         'Sale Report' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Stock Report' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Purchase Report' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Ageing Report' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Customer Wise Report' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        //     'Administration' => [
        //         'Users' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Roles' => ['list', 'add', 'edit', 'delete', 'view', 'permissions'],
        //         'Permissions' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Escalation Management' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Departments' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        //     'Preferences' => [
        //         'Measurement Units' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Discounts' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Notifications Management' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'WhatsApp / SMS Casting Preferences' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Unit Types' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Invoice Preferences' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Employees' => ['list', 'add', 'edit', 'delete', 'view'],
        //         'Product Categories' => ['list', 'add', 'edit', 'delete', 'view'],
        //     ],
        // ];

        // // Seed permissions
        // foreach ($modules as $mainModule => $subModules) {
        //     // Create main module permission
        //     $mainModulePermission = Permission::create([
        //         'name' => $mainModule,
        //         'guard_name' => 'web',
        //         'parent_id' => 0
        //     ]);

        //     // Create sub-modules and their permissions
        //     foreach ($subModules as $subModule => $actions) {
        //         $subModulePermission = Permission::create([
        //             'name' => $subModule,
        //             'guard_name' => 'web',
        //             'parent_id' => $mainModulePermission->id
        //         ]);

        //         foreach ($actions as $action) {
        //             Permission::create([
        //                 'name' => "{$subModule} {$action}",
        //                 'guard_name' => 'web',
        //                 'parent_id' => $subModulePermission->id
        //             ]);
        //         }
        //     }
        // }
    }


}
