<?php

namespace Database\Seeders;

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

class LiveTrackingPermissionSeeder extends Seeder
{
    /** @var array<string, string> */
    private const PERMISSIONS = [
        'tracking.view' => 'View tracking overview',
        'tracking.live' => 'View live locations',
        'tracking.history' => 'View location history',
        'tracking.export' => 'Export tracking data',
        'tracking.geofence' => 'Manage geofences',
        'tracking.settings' => 'Manage tracking settings',
        'tracking.analytics' => 'View movement analytics',
    ];

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

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