<?php

namespace App\Http\Strategies\AuthVerification;

use App\Http\Enums\AuthStrategies;
use App\Actions\ActivationCodeAction;
use App\Http\Repositories\UserRepository;
use App\Http\Repositories\AuthVerificationTokenRepository;

class PhoneStrategy implements IAuthVerificationStrategy
{
    public function request($identifier)
    {
        $user = (new UserRepository())->userByNotVerifiedPhone($identifier);

        $activation_code = 1234; // ActivationCodeAction::action();

        (new AuthVerificationTokenRepository)->create($user->phone, AuthStrategies::Phone->value, $activation_code);

        // TODO: Send verification code via SMS
    }

    public function active($identifier, $code)
    {
        $user = (new UserRepository())->userByNotVerifiedPhone($identifier);

        if(!(new AuthVerificationTokenRepository)->reset($identifier, AuthStrategies::Phone->value, $code))
            respond()->unauthorized(__('Code is incorrect'))->throwResponse();

        $user->update(['phone_verified_at' => now()]);

        return $user;
    }
}
