<?php
declare (strict_types=1);

namespace App\Controller\User\API\Store;

use App\Controller\User\Base;
use App\Interceptor\Group;
use App\Interceptor\PostDecrypt;
use App\Interceptor\User;
use App\Interceptor\Waf;
use App\Validator\Store\Captcha;
use Kernel\Annotation\Inject;
use Kernel\Annotation\Interceptor;
use Kernel\Annotation\Validator;
use Kernel\Context\Interface\Response;
use Kernel\Exception\RuntimeException;
use Kernel\Plugin\Plugin;
use Kernel\Validator\Method;

#[Interceptor(class: [PostDecrypt::class, Waf::class, User::class, Group::class], type: Interceptor::API)]
class Auth extends Base
{

    #[Inject]
    private \App\Service\Store\Auth $auth;


    /**
     * @param string $type
     * @return Response
     */
    #[Validator([[Captcha::class, "type"]], Method::GET)]
    public function captcha(string $type): Response
    {
        return $this->response->raw($this->auth->captcha($type))->withHeader("Content-Type", "image/png");
    }


    /**
     * @return Response
     * @throws RuntimeException
     * @throws \ReflectionException
     */
    #[Validator([
        [\App\Validator\Store\Auth::class, ["loginUsername", "loginPassword", "captcha"]]
    ])]
    public function login(): Response
    {
        $login = $this->auth->login($this->request->post("username"), $this->request->post("password"), $this->request->post("captcha"));
        Plugin::inst()->setStoreUser($login->id, $login->key, $this->getUserPath());
        return $this->json();
    }


    /**
     * @return Response
     * @throws RuntimeException
     * @throws \ReflectionException
     */
    #[Validator([
        [\App\Validator\Store\Auth::class, ["registerUsername", "registerPassword", "code", "email", "captcha"]]
    ])]
    public function register(): Response
    {
        $login = $this->auth->register($this->request->post("username"), $this->request->post("password"), $this->request->post("email"), $this->request->post("code"), $this->request->post("captcha"));
        Plugin::inst()->setStoreUser($login->id, $login->key, $this->getUserPath());
        return $this->json();
    }

    /**
     * @return Response
     * @throws RuntimeException
     * @throws \ReflectionException
     */
    #[Validator([
        [\App\Validator\Store\Auth::class, ["email", "registerPassword", "code", "captcha"]]
    ])]
    public function reset(): Response
    {
        $login = $this->auth->reset($this->request->post("email"), $this->request->post("password"), $this->request->post("code"), $this->request->post("captcha"));
        Plugin::inst()->setStoreUser($login->id, $login->key, $this->getUserPath());
        return $this->json();
    }


    /**
     * @return Response
     * @throws RuntimeException
     */
    #[Validator([
        [\App\Validator\Store\Auth::class, ["sendEmail", "type", "captcha"]]
    ])]
    public function sendSms(): Response
    {
        $this->auth->sendEmail(type: $this->request->post("type"), email: $this->request->post("email"), captcha: $this->request->post("captcha"));
        return $this->json();
    }
}