<?php
namespace EmotionHero\Api;

use EmotionHero\Application as EH;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Silex\Api\ControllerProviderInterface;
use EmotionHero\Models;
use EmotionHero\Server\Websockets;

class StatsControllerProvider implements ControllerProviderInterface
{
    /** @var EH */
    protected $_eh;

    public function __construct()
    {
        $this->_eh = EH::getInstance();
    }

    public function connect(Application $app)
    {
        // creates a new controller based on the default route
        $controllers = $app['controllers_factory'];
        $gamesRepo = $this->_eh->getEm()->getRepository(Models\Game::class);

        $controllers->get('/', function (Application $app) {
            $stats = [
                'new_games'  => [],
                'new_users'  => [],
            ];

            for ($i=0; $i < 14; $i++) { 
                $day = date('Y-m-d',strtotime("-$i days"));
                $stats['new_games'] = $gameRepo->getCreatedCountOnDate($day);
            }
            return $app['serializer']->serialize($stats, 'json');
        });
        
        return $controllers;
    }
}