<?php

// src/Controller/DataImport.php
// Grab data from transfer app

namespace App\Controller\Routes\Back;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\Auth;
use App\Service\Render;

class Index extends AbstractController
{
    /**
     * @Route("/den", name="back-index")
     */
    public function enterTheDen(Request $request, Auth $auth, RequestStack $requestStack, Render $render): Response
    {
        if ($request->getMethod() == "GET") {
            return $render->page([], "The Bad Space | Den", "back/index.twig");
        } else {
            //handles login
            $handle = $request->request->get("handle");
            $pass   = $request->request->get("password");
            $result = $auth->authCheck($handle, $pass);
            if ($result["status"]) {
                header("Location:/den");
                return new Response("<html><body>LOGGED IN</body></html>");
            } else {
                return $render->page(["notice" => $result["message"]], "The Bad Space | Den", "back/index.twig");
            }
        }
    }

    /**
       * @Route("/logout", name="logout-page")
       */
    public function leaveTheDen(Auth $auth)
    {
        $auth->logout();
        header("Location:/den");
        return new Response("<html><body>LOGGED OUT</body></html>");
    }
}
