<?php

namespace App\Services\Auth;

use App\Entities\UserEntity;
use App\Forms\Auth\LocalForm;
use App\Models\UserModel;
use RuntimeException;

class LocalService extends AuthService
{
	protected string $formClass = LocalForm::class;

	public function __construct(UserModel $model)
	{
		parent::__construct($model);
		$this->addClassPaths('Local');
	}

	protected function getEntity_process(mixed $entity): UserEntity
	{
		return $entity;
	}
	protected function login_process(array $formDatas): UserEntity
	{
		$this->getActionForm()->form_init_process('login', $formDatas);
		//입력값 검증
		$this->getActionForm()->validate($formDatas);
		//로그인 정보확인
		$entity = $this->getEntity(['id' => $formDatas['id'], 'status' => 'AVAILABLE']);
		if (!$entity instanceof UserEntity) {
			throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 {$formDatas['id']}에 대한 로그인 정보를 찾을수 없습니다.");
		}
		if (!password_verify($formDatas['passwd'], $entity->getPassword())) {
			throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 암호가 맞지 않습니다.");
		}
		return $entity;
	}
}
