<?php

namespace Database\Seeders;

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

class WalletSubscriptionPermissionSeeder extends Seeder
{
    /** @var array<string, string> */
    private const PERMISSIONS = [
        'subscription.view' => 'View subscriptions',
        'subscription.create' => 'Create subscription plans',
        'subscription.edit' => 'Edit subscription plans',
        'subscription.delete' => 'Delete subscription plans',
        'wallet.view' => 'View wallets',
        'wallet.manage' => 'Manage wallets',
        'wallet.recharge' => 'Recharge wallets',
        'payment.view' => 'View payments',
        'payment.manage' => 'Manage payments and refunds',
        'coupon.manage' => 'Manage coupons',
        'invoice.view' => 'View invoices',
        'reports.export' => 'Export financial reports',
    ];

    public function run(): void
    {
        $permissionIds = collect(self::PERMISSIONS)->map(function (string $displayName, string $name): int {
            return Permission::query()->firstOrCreate(
                ['name' => $name],
                ['display_name' => $displayName, 'module' => 'wallet_subscription'],
            )->getKey();
        });

        Role::query()->where('name', 'super-admin')->first()?->permissions()->syncWithoutDetaching($permissionIds);
    }
}
