<?php

namespace Modules\User\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Modules\User\Services\PublicMemberProfileService;
use Modules\User\Transformers\PublicMemberProfileResource;

class PublicMemberProfileController extends Controller
{
    public function __construct(
        protected PublicMemberProfileService $publicMemberProfileService
    ) {}

    public function show(string $token): JsonResponse
    {
        $member = $this->publicMemberProfileService->findMemberByToken($token);

        if (! $member) {
            return response()->json([
                'success' => false,
                'message' => 'Anggota tidak dijumpai atau tidak sah.',
                'code' => 'public_profile_not_found',
                'data' => null,
            ], 404);
        }

        $member = $this->publicMemberProfileService->loadVerifiableRelations($member);

        return response()->json([
            'success' => true,
            'message' => 'Anggota disahkan.',
            'data' => new PublicMemberProfileResource($member),
        ]);
    }
}
