<?php

// src/Controller/ProductController.php

namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Entity\Member;
use ReallySimpleJWT\Token;

class Auth
{
    private $session;
    private $entityManager;

    public function __construct(
        EntityManagerInterface $entityManager,
        RequestStack $requestStack
    ) {
        $this->entityManager = $entityManager;
        $this->session       = $requestStack->getSession();
        $this->secret        = '!$ec7eT$l0w*';
    }

    public function authCheck($handle, $password)
    {
        $response = [];
        $member   = new Member();
        $members  = $this->entityManager->getRepository(Member::class);
        $member   = $members->findOneBy(["handle" => $handle]);
        if (!$member) {
            $response = ["status" => false, "message" => "Member Not Found"];
        } else {
            if (!password_verify($password, $member->getPassword())) {
                $response = ["status" => false, "message" => "Check that password"];
            } else {
                $this->session->set("member", $member);

                $secret     = $this->secret;
                $expiration = time() + 3600;
                $token      = Token::create(
                    $member->getId(),
                    $secret,
                    $expiration,
                    "bad_space_admin"
                );

                $this->session->set("token", $token);
                $response = ["status" => true, "message" => "Welcome Back"];
            }
        }

        return $response;
    }

    public function logOut()
    {
        $this->session->set("member", null);
        $this->session->set("token", null);
    }

    public function APIStatus()
    {
        $response = [];
        $verify   = Token::validate($this->session->get("token"), $this->secret);

        if ($verify) {
            $response = [
                "status"  => true,
                "message" => "Token is good",
                "token"   => $this->session->get("token"),
            ];
        } else {
            $response = ["status" => false, "message" => "Bad Token, champ."];
        }

        return $response;
    }

    public function status()
    {
        $response = [];
        //checks to see if member session exists
        if ($this->session->get("member")) {
            //checks if token is still valid
            $verify = Token::validateExpiration($this->session->get("token"), $this->secret);
            if ($verify) {
                $response = [
                    "status" => true,
                    "role"   => $this->session->get("member")->getRole(),
                    "id"     => $this->session->get("member")->getId(),
                    "handle" => $this->session->get("member")->getHandle(),
                    "token"  => $this->session->get("token"),
                ];
            } else {
                $response = ["status" => false, "role" => null];
            }
        } else {
            $response = ["status" => false, "role" => null];
        }

        return $response;
    }
}
