<?php

namespace App\Controller\Api;

use App\Enum\Role;
use App\Entity\User;
use App\Entity\UserAddress;
use App\Request\Response200;
use App\Request\Response422;
use OpenApi\Annotations as OA;
use App\Request\Response200Json;
use App\Request\Response200Message;
use App\Request\User\RequestUpdate;
use App\Controller\Api\ApiController;
use App\Request\User\RequestRegister;
use App\Request\User\RequestUserList;
use App\Repository\PositionRepository;
use App\Repository\DepartmentRepository;
use App\Request\User\RequestUpdateByDni;
use Nelmio\ApiDocBundle\Annotation\Model;
use App\Request\User\RequestChangePassword;
use Nelmio\ApiDocBundle\Annotation\Security;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use App\Repository\CompanyLocationRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use App\Request\UserAddress\RequestAddAddress;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Repository\CompanyClassificationRepository;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;

class AddressController extends ApiController implements Role
{
    /**
     * List adress by user
     *
     * @Rest\Get("/api/user/address", name="api_address_list")
     * @Security(name="Bearer")
     * @OA\Tag(name="address")
     * @OA\Response(
     *     response=200,
     *     description="Ok response",
     *     @Model(type=Response200::class)
     * )
     * @OA\Response(
     *     response=422,
     *     description="Error response",
     *     @Model(type=Response422::class)
     * )
     *
     * @param UserInterface|null $user
     * @param ValidatorInterface $validator
     *
     * @return Response
     */
    public function list (?UserInterface $user, ValidatorInterface $validator): Response
    {
        $user = $this->getUser();

        $data = $this->getDoctrine()
            ->getRepository(UserAddress::class)
            ->addressByUser($user->getId())
        ;

        $response = [
            'status' => $data ? Response::HTTP_OK : Response::HTTP_NOT_FOUND,
            'error'  => !is_array($data) ? true : false,
            'data'   => $data,
        ];
        return $this->sendResponse($response);
    }

    /**
     * Add address
     *
     * @Rest\Post("/api/address", name="api_address_add")
     * @Security(name="Bearer")
     * @OA\Tag(name="address")
     * @OA\Response(
     *     response=200,
     *     description="Ok response",
     *     @Model(type=Response200Message::class)
     * )
     * @OA\Response(
     *     response=422,
     *     description="Error response",
     *     @Model(type=Response422::class)
     * )
     * @OA\RequestBody(
     *       @OA\MediaType(
     *           mediaType="multipart/form-data",
     *           @OA\Schema(ref=@Model(type=RequestAddAddress::class))
     *      )
     * )
     *
     * @param Request $request
     * @param ValidatorInterface $validator
     *
     * @return Response
     */
    public function add(Request $request, ValidatorInterface $validator): Response
    {
        $errors = $validator->validate(
                new RequestAddAddress($request)
        );
        if (count($errors) > 0)
        {
            return $this->respondValidationError($errors);
        }

        $entity = new UserAddress();
        $entity
            ->setName($request->get('name'))
            ->setPostalCode($request->get('postalCode'))
            ->setApartment($request->get('apartment'))
            ->setHouseNumber($request->get('houseNumber'))
            ->setStreet($request->get('street'))
            ->setLatitude($request->get('latitude'))
            ->setLongitude($request->get('longitude'))
            ->setUser($this->getUser())
        ;

        $this->em->persist($entity);
        $this->em->flush();

        return $this->respondWithSuccess(sprintf($this->trans('general.successfullRegistration'), $entity->getId()));
    }
}
