<?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\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\HandleMembers;
use App\Service\Render;
use App\Service\Auth;

class Members extends AbstractController
{
    /**
     * @Route("/den/members/page/{pageNum}", name="den-members-index")
     */
    public function showMembers(
        Request $request,
        Auth $auth,
        Render $render,
        HandleMembers $members,
        int $pageNum = 1
    ) {
        $result = $auth->status();
        if ($result["status"]) {
            $self = $members->getYou();
            $you  = $self->getId();
            $list = $members->getMemberPage($pageNum);

            return $render->page([
                "mode" => "index",
                "you"  => $you,
                "list" => $list], "Bad Space | Members", "back/members.twig");
        } else {
        }
    }

    /**
    * @Route("/den/members/{action}/{id}", name="den-members")
    */
    public function handleMemberActions(
        Request $request,
        Auth $auth,
        Render $render,
        HandleMembers $members,
        string $action = "index",
        int $id = 0
    ): Response {
        $result = $auth->status();
        if ($result["status"]) {
            if ($request->getMethod() == "GET") {
                $self   = $members->getYou();
                $you    = $self->getId();
                $member = [];
                if ($id != 0) {
                    $member = $members->getMemberById($id);
                }
                return $render->page([
                    "mode"          => $action,
                    "you"           => $you,
                    "currentMember" => $member], "Bad Space | Members", "back/members.twig");
            } else {
                //add new member
                $token  = $request->get("token");
                $notice = "";
                $mode   = $request->get("mode");
                $id     = $request->get("member_id");

                //token check
                if (!$this->isCsrfTokenValid("upload", $token)) {
                    $logger->info("CSRF failure");

                    return new Response(
                        "Operation not allowed",
                        Response::HTTP_BAD_REQUEST,
                        [
                            "content-type" => "text/plain",
                        ]
                    );
                }
                $self   = $members->getYou();
                $you    = $self->getId();
                $member = [];
                if ($id != 0) {
                    $member = $members->getMemberById($id);
                }
                if (
                    $request->request->get("handle") == "" ||
                    $request->request->get("role") == "" ||
                    $request->request->get("gender") == "" ||
                    $request->request->get("email") == "" ||
                    $request->request->get("pronoun") == ""
                ) {
                    $notice = "All fields are required, champ.";
                    return $render->page([
                        "notice"        => $notice,
                        "mode"          => $mode,
                        "you"           => $you,
                        "currentMember" => $member], "Bad Space | Members", "back/members.twig");
                }
                //empty valid email check
                if (
                    !filter_var($request->request->get("email"), FILTER_VALIDATE_EMAIL)
                ) {
                    $notice = "Need a valid email, slick.";
                    return $render->page([
                        "notice"        => $notice,
                        "mode"          => $mode,
                        "you"           => $you,
                        "currentMember" => $member], "Bad Space | Members", "back/members.twig");
                }
                // password confirm check
                if ($request->request->get("new_pass") != "") {
                    if ($request->request->get("new_pass") != $request->request->get("new_pass_confirm")) {
                        $notice = "New password must be confirmed";
                        return $render->page([
                            "notice"        => $notice,
                            "mode"          => $mode,
                            "you"           => $you,
                            "currentMember" => $member], "Bad Space | Members", "back/members.twig");
                    }
                }

                //check clear, call add method
                $response = $members->modifyMember($request, $id, $mode);
                if ($response["status"]) {
                    return $render->page([
                        "notice"        => $response["message"],
                        "mode"          => $mode,
                        "you"           => $you,
                        "currentMember" => $member], "Bad Space | Members", "back/members.twig");
                } else {
                    return new Response("<html><body>" . $response["message"] . "</body></html>");
                    /*
                    return $render->renderPage(
                        ["bgImage" => "", "message" => $response["message"]],
                        "The Nile List | Uh Oh Time",
                        "front/error.html.twig"
                    );
                    */
                }
            }
        } else {
            //back to index to login
            header("Location:/den");
            return new Response("<html><body>TO THE FRONT</body></html>");
        }
    }
}
