<?php declare(strict_types=1);

/*
 * This file is part of the Rapsys BlogBundle package.
 *
 * (c) Raphaël Gertz <symfony@rapsys.eu>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rapsys\BlogBundle\Repository;

use Doctrine\ORM\Query\ResultSetMapping;

/**
 * ArticleRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ArticleRepository extends EntityRepository {
	/**
	 * Find article count as int
	 *
	 * @return integer The articles count
	 */
	public function findCountAsInt(): int {
		//Set the request
		$req = <<<SQL
SELECT COUNT(at.article_id) AS count
FROM RapsysBlogBundle:ArticleTranslation AS at
WHERE at.locale = :locale
SQL;

		//Get result set mapping instance
		$req = $this->replace($req);

		//Get result set mapping instance
		//XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
		$rsm = new ResultSetMapping();

		//Declare all fields
		//XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
		//addScalarResult($sqlColName, $resColName, $type = 'string');
		$rsm->addScalarResult('count', 'count', 'integer');

		//Get result
		return $this->_em
			->createNativeQuery($req, $rsm)
			->getSingleScalarResult();
	}

	/**
	 * Find articles as array
	 *
	 * @param integer $page The page
	 * @param integer $count The count
	 * @return array The articles array
	 */
	public function findAllAsArray(int $page, int $count): array {
		//Set the request
		$req = <<<SQL
SELECT
	a.id,
	GREATEST(a.created, at.created, k.created, kt.created) AS created,
	GREATEST(a.updated, at.updated, k.updated, kt.updated) AS updated,
	GREATEST(a.created, at.created, k.created, kt.created, a.updated, at.updated, k.updated, kt.updated) as modified,
	at.body,
	at.description,
	at.slug,
	at.title,
	GROUP_CONCAT(k.id ORDER BY k.id SEPARATOR "\\n") AS k_ids,
	GROUP_CONCAT(kt.slug ORDER BY k.id SEPARATOR "\\n") AS k_slugs,
	GROUP_CONCAT(kt.title ORDER BY k.id SEPARATOR "\\n") AS k_titles
FROM RapsysBlogBundle:Article AS a
JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
GROUP BY a.id
ORDER BY updated, created DESC, a.id
LIMIT :offset, :count
SQL;

		//Replace bundle entity name by table name
		$req = $this->replace($req);

		//Get result set mapping instance
		//XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
		$rsm = new ResultSetMapping();

		//Declare all fields
		//XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
		//addScalarResult($sqlColName, $resColName, $type = 'string');
		$rsm->addScalarResult('id', 'id', 'integer')
			->addScalarResult('body', 'body', 'string')
			->addScalarResult('description', 'description', 'string')
			->addScalarResult('slug', 'slug', 'string')
			->addScalarResult('title', 'title', 'string')
			->addScalarResult('k_ids', 'k_ids', 'string')
			->addScalarResult('k_slugs', 'k_slugs', 'string')
			->addScalarResult('k_titles', 'k_titles', 'string')
			->addScalarResult('created', 'created', 'datetime')
			->addScalarResult('updated', 'updated', 'datetime')
			->addScalarResult('modified', 'modified', 'datetime')
			->addIndexByScalar('id');

		//Get result
		$result = $this->_em
			->createNativeQuery($req, $rsm)
			->setParameter('offset', $page * $count)
			->setParameter('count', $count)
			->getArrayResult();

		//Set return
		$return = [];

		//Iterate on each city
		foreach($result as $data) {
			//Add to return
			$return[$data['id']] = [
				'id' => $id = $data['id'],
				'body' => $data['body'],
				'description' => $data['description'],
				'slug' => $slug = $data['slug'],
				'title' => $data['title'],
				'created' => $data['created'],
				'updated' => $data['updated'],
				'modified' => $data['modified'],
				'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slug]),
				'keywords' => []
			];

			//Explode keyword ids
			$data['k_ids'] = explode("\n", $data['k_ids']);

			//Explode keyword slugs
			$data['k_slugs'] = explode("\n", $data['k_slugs']);

			//Explode keyword titles
			$data['k_titles'] = explode("\n", $data['k_titles']);

			foreach($data['k_ids'] as $k => $id) {
				$return[$data['id']]['keywords'][$id] = [
					'id' => $id,
					'slug' => $slug = $data['k_slugs'][$k],
					'title' => $data['k_titles'][$k],
					'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $id, 'slug' => $slug]),
				];
			}
		}

		//Return return
		return $return;
	}

	/**
	 * Find article by id as array
	 *
	 * @param integer $id The article id
	 * @return array The article array
	 */
	public function findByIdAsArray(int $id): array {
		//Set the request
		$req = <<<SQL
SELECT
	b.id,
	GREATEST(b.created, kat.created) AS created,
	GREATEST(b.updated, kat.updated) AS updated,
	GREATEST(b.created, b.updated, kat.created, kat.updated) AS modified,
	b.body,
	b.description,
	b.slug,
	b.title,
	b.k_ids,
	b.k_slugs,
	b.k_titles,
	GROUP_CONCAT(kat.locale ORDER BY kat.locale SEPARATOR "\\n") AS kat_locales,
	GROUP_CONCAT(kat.slug ORDER BY kat.locale SEPARATOR "\\n") AS kat_slugs,
	GROUP_CONCAT(kat.title ORDER BY kat.locale SEPARATOR "\\n") AS kat_titles
FROM (
	SELECT
		a.id,
		GREATEST(a.created, at.created, k.created, kt.created) AS created,
		GREATEST(a.updated, at.updated, k.updated, kt.updated) AS updated,
		at.body,
		at.description,
		at.slug,
		at.title,
		GROUP_CONCAT(k.id ORDER BY k.id SEPARATOR "\\n") AS k_ids,
		GROUP_CONCAT(kt.slug ORDER BY k.id SEPARATOR "\\n") AS k_slugs,
		GROUP_CONCAT(kt.title ORDER BY k.id SEPARATOR "\\n") AS k_titles
	FROM RapsysBlogBundle:Article AS a
	JOIN RapsysBlogBundle:ArticleTranslation AS at ON (at.article_id = a.id AND at.locale = :locale)
	LEFT JOIN RapsysBlogBundle:ArticleKeyword AS ak ON (ak.article_id = a.id)
	LEFT JOIN RapsysBlogBundle:Keyword AS k ON (k.id = ak.keyword_id)
	LEFT JOIN RapsysBlogBundle:KeywordTranslation AS kt ON (kt.keyword_id = k.id AND kt.locale = :locale)
	WHERE a.id = :id
	GROUP BY a.id
	ORDER BY NULL
	LIMIT 0, :limit
) AS b
LEFT JOIN RapsysBlogBundle:ArticleTranslation AS kat ON (kat.article_id = b.id AND kat.locale <> :locale)
GROUP BY b.id
SQL;

		//Replace bundle entity name by table name
		$req = $this->replace($req);

		//Get result set mapping instance
		//XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
		$rsm = new ResultSetMapping();

		//Declare all fields
		//XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
		//addScalarResult($sqlColName, $resColName, $type = 'string');
		$rsm->addScalarResult('id', 'id', 'integer')
			->addScalarResult('body', 'body', 'string')
			->addScalarResult('description', 'description', 'string')
			->addScalarResult('slug', 'slug', 'string')
			->addScalarResult('title', 'title', 'string')
			->addScalarResult('k_ids', 'k_ids', 'string')
			->addScalarResult('k_slugs', 'k_slugs', 'string')
			->addScalarResult('k_titles', 'k_titles', 'string')
			->addScalarResult('kat_locales', 'kat_locales', 'string')
			->addScalarResult('kat_slugs', 'kat_slugs', 'string')
			->addScalarResult('kat_titles', 'kat_titles', 'string')
			->addScalarResult('created', 'created', 'datetime')
			->addScalarResult('updated', 'updated', 'datetime')
			->addScalarResult('modified', 'modified', 'datetime');

		//Get data
		$data = $this->_em
			->createNativeQuery($req, $rsm)
			->setParameter('id', $id)
			->getOneOrNullResult();

		//Set return
		$return = [
			'id' => $id = $data['id'],
			'body' => $data['body'],
			'description' => $data['description'],
			'slug' => $slug = $data['slug'],
			'title' => $data['title'],
			'created' => $data['created'],
			'updated' => $data['updated'],
			'modified' => $data['modified'],
			'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slug]),
			'alternates' => [],
			'keywords' => []
		];

		//Explode keyword ids
		$data['k_ids'] = explode("\n", $data['k_ids']);

		//Explode keyword slugs
		$data['k_slugs'] = explode("\n", $data['k_slugs']);

		//Explode keyword titles
		$data['k_titles'] = explode("\n", $data['k_titles']);

		foreach($data['k_ids'] as $k => $kid) {
			$return['keywords'][$kid] = [
				'id' => $kid,
				'slug' => $slug = $data['k_slugs'][$k],
				'title' => $data['k_titles'][$k],
				'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slug]),
			];
		}

		//Explode alternate locales
		$data['kat_locales'] = explode("\n", $data['kat_locales']);

		//Explode alternate slugs
		$data['kat_slugs'] = explode("\n", $data['kat_slugs']);

		//Explode alternate titles
		$data['kat_titles'] = explode("\n", $data['kat_titles']);

		foreach($data['kat_locales'] as $k => $locale) {
			$return['alternates'][$locale] = [
				'locale' => $locale,
				'slug' => $slug = $data['kat_slugs'][$k],
				'title' => $data['kat_titles'][$k],
				'link' => $this->router->generate('rapsysblog_article_view', ['_locale' => $locale, 'id' => $id, 'slug' => $slug]),
			];
		}

		//Return return
		return $return;
	}
}
