<?php

namespace App\Services\Auth;

use App\Entities\UserEntity;
use App\Helpers\AuthHelper;
use App\Models\CommonModel;
use App\Services\CommonService;
use CodeIgniter\Exceptions\PageNotFoundException;

abstract class AuthService extends CommonService
{
	protected string $helperClass = AuthHelper::class;

	protected function __construct(CommonModel $model)
	{
		parent::__construct($model);
		$this->addClassPaths('Auth');
	}
	public function getEntityClass(): string
	{
		return UserEntity::class;
	}
	//로그인
	abstract protected function login_process(array $formDatas): UserEntity;
	final public function login(array $formDatas): UserEntity
	{
		try {
			$entity = $this->login_process($formDatas);
			//인증 세션처리
			$this->getAuthContext()->setAuthSession($entity);
			return $entity;
		} catch (\Exception $e) {
			log_message('error', $e->getMessage());
			throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.\n{$e->getMessage()}");
		}
	}
	//로그아웃
	final public function logout(): void
	{
		$this->getAuthContext()->destroyAuthSession();
	}
}
