<?php

namespace Database\Seeders\Global;

use App\Master\Models\ApiUser;
use App\Master\Models\ApiUserRoute;
use App\Services\ApiAuthenticatedRoutes;
use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;

class ApiUserRouteSeeder extends Seeder
{
    public function run(): void
    {
        $routeNames = $this->getApiAuthenticatedRouteNames();

        ApiUser::all()->each(function (ApiUser $apiUser) use ($routeNames) {
            $routeNames->each(function (string $routeName) use ($apiUser) {
                ApiUserRoute::query()->updateOrCreate([
                    'api_user_id' => $apiUser->id,
                    'route_name' => $routeName,
                ]);
            });
        });
    }

    public function getApiAuthenticatedRouteNames(): Collection
    {
        return app(ApiAuthenticatedRoutes::class)->names();
    }
}
