<?php

namespace App\Services\Auth;

use App\DTOs\Auth\GoogleDTO;
use App\DTOs\Auth\LocalDTO;
use App\Entities\UserEntity;
use App\Helpers\AuthHelper;
use App\Models\CommonModel;
use App\Services\CommonService;

abstract class AuthService extends CommonService
{
	private $_helper = null;
	protected function __construct(CommonModel $model)
	{
		parent::__construct($model);
		$this->addClassPaths('Auth');
	}
	public function getEntityClass(): string
	{
		return UserEntity::class;
	}
	abstract public function getFormService(): mixed;
	final public function getHelper(): AuthHelper
	{
		if ($this->_helper === null) {
			$this->_helper = new AuthHelper();
			$this->_helper->setAttributes([
				'pk_field'         => $this->model->getPKField(),
				'title_field'      => $this->model->getTitleField(),
				'class_path'       => $this->getClassPaths(false)
			]);
		}
		return $this->_helper;
	}
	//로그인
	abstract protected function login_process(array $formDatas): UserEntity;
	final public function login(LocalDTO|GoogleDTO  $dto): UserEntity
	{
		$entity = $this->login_process($dto->toArray());
		//인증 세션처리
		$this->getAuthContext()->setAuthSession($entity);
		return $entity;
	}
	//로그아웃
	final public function logout(): void
	{
		$this->getAuthContext()->destroyAuthSession();
	}
}
