<?php

namespace App\Controller\UDS;

use App\Entity\Image;
use App\Entity\OrganizationalUnit;
use App\Model\OrganizationalUnitTypes;
use Doctrine\ORM\EntityManagerInterface;
use Random\RandomException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;

#[AsController]
class OrganizationalUnitController extends AbstractController
{
    public function __construct(
        protected readonly EntityManagerInterface $entityManager
    )
    {
    }

    /**
     * @throws RandomException
     */
    #[Route('/opengnsys/rest//login', methods: ['POST'])]
    public function login(Request $request): JsonResponse
    {
        $data = [
            'userid' => 1,
            'apikey' => bin2hex(random_bytes(16))
        ];

        return new JsonResponse($data, Response::HTTP_OK);
    }


    #[Route('/opengnsys/rest//ous', methods: ['GET'])]
    public function getOUs(Request $request): JsonResponse
    {
        $organizationalUnits = $this->entityManager
            ->getRepository(OrganizationalUnit::class)
            ->findBy(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT], ['name' => 'ASC']);

        $data = [];
        foreach ($organizationalUnits as $center) {
            $data[] = [
                'id' => $center->getId(),
                'name' => $center->getName(),
                'description' => $center->getDescription(),
            ];
        }

        return new JsonResponse($data, Response::HTTP_OK);
    }

    #[Route('/opengnsys/rest/ous/{centerId}/labs', name: 'get_classrooms', methods: ['GET'])]
    #[Route('/opengnsys/rest//ous/{centerId}/labs', methods: ['GET'])]
    public function getClassrooms(int $centerId): JsonResponse
    {
        $classrooms = $this->entityManager
            ->getRepository(OrganizationalUnit::class)
            ->findBy([
                'type' => OrganizationalUnitTypes::CLASSROOM,
                'parent' => $centerId
            ], ['name' => 'ASC']);

        $data = [];

        foreach ($classrooms as $classroom) {
            $data[] = [
                'id' => $classroom->getId(),
                'name' => $classroom->getName(),
                'inremotepc' => true,
                'ou' => [
                    'id' => 1
                ],
                'classroom' => []
            ];
        }

        return new JsonResponse($data, Response::HTTP_OK);
    }

    #[Route('/opengnsys/rest/ous/{centerId}/images', name: 'getImages', methods: ['GET'])]
    #[Route('/opengnsys/rest//ous/{centerId}/images', methods: ['GET'])]
    public function getImages(int $centerId): JsonResponse
    {
        $parent = $this->entityManager
            ->getRepository(OrganizationalUnit::class)
            ->find($centerId);

        $images = $this->entityManager
            ->getRepository(Image::class)
            ->findAll();

        $data = [];

        foreach ($images as $image) {
            $data[] = [
                'id' => $image->getId(),
                'name' => $image->getName(),
                'inremotepc' => true,
                'ou' => [
                    'id' => 1
                ]
            ];
        }

        return new JsonResponse($data, Response::HTTP_OK);
    }

    #[Route('/opengnsys/rest//info', methods: ['GET'])]
    public function getOpengnsysInfo(Request $request): JsonResponse
    {
        $data = [
            "project" => "OpenGnsys",
            "version" =>  "1.1.1d",
            "codename" =>  "Espeto",
            "definition" =>  "http://www.andalucia.com/gastronomy/espeto.htm",
            "release" =>  "",
            "ogagent" =>  "1.1.2",
            "services" =>  [
                    "server",
                    "repository"
            ],
            "oglive" =>
                [
                    "distribution" =>  "focal",
                    "kernel" =>  "5.13.0-27-beta",
                    "architecture" => "amd64",
                    "revision" =>  "r20210706",
                    "directory" =>  "ogLive-5.13.0-r20210706",
                    "iso" =>  "ogLive-focal-5.13.0-27-beta-amd64-r20210706.5b4bf5f.iso"
                ]
        ];

        return new JsonResponse($data, Response::HTTP_OK);
    }
}