<?php

namespace Modules\User\Http\Controllers\Api\Service;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\User\Http\Resources\UserServiceResource;
use Modules\User\Models\Identity;

class IndexController extends Controller
{

    public function index(Request $request): JsonResponse
    {
        $identityId = $request->identity_id ?? 0;
        if ($identityId) {
            $identity = Identity::findOrFail($identityId);
            $service  = $identity->identities()->shown()->inRandomOrder()->first();
        } else {
            $user    = Api::user();
            $service = $user->identities->first()->identities()->shown()->inRandomOrder()->first();
        }
        if (empty($service)) {
            return $this->success([]);
        }

        return $this->success(new UserServiceResource($service));
    }

}
