<?php

namespace App\Http\Controllers\v1\Tenancy\Auth;

use App\Http\Controllers\Controller;
use App\Models\Tenancy\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class VerifyEmailController extends Controller
{
    public function __invoke(int $id, string $hash, Request $request): Response
    {
        $user = User::findOrFail($id);

        if (hash('sha256', $user->email) == $hash && ! $user->hasVerifiedEmail()) {
            $user->markEmailAsVerified();
            event(new Verified($user));

            return $this->ok(['message' => 'Email verified successfully']);
        }

        return $this->error(['message' => 'Invalid verification URL'], 400);
    }
}
