<?php

/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Deck\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IDBConnection;

/** @template-extends DeckMapper<Stack> */
class StackMapper extends DeckMapper implements IPermissionMapper {
	private CappedMemoryCache $stackCache;
	private CardMapper $cardMapper;
	private ICache $cache;

	public function __construct(
		IDBConnection $db,
		CardMapper $cardMapper,
		ICacheFactory $cacheFactory,
	) {
		parent::__construct($db, 'deck_stacks', Stack::class);
		$this->cardMapper = $cardMapper;
		$this->stackCache = new CappedMemoryCache();
		$this->cache = $cacheFactory->createDistributed('deck-stackMapper');
	}


	/**
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 * @throws \OCP\DB\Exception
	 */
	public function find(int $id): Stack {
		if (isset($this->stackCache[(string)$id])) {
			return $this->stackCache[(string)$id];
		}

		$qb = $this->db->getQueryBuilder();
		$qb->select('*')
			->from($this->getTableName())
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));

		$this->stackCache[(string)$id] = $this->findEntity($qb);
		return $this->stackCache[(string)$id];
	}

	/**
	 * Fetch multiple stacks by their IDs in a single query.
	 *
	 * @param int[] $ids
	 * @return array<int, Stack> Map of stackId => Stack
	 * @throws \OCP\DB\Exception
	 */
	public function findByIds(array $ids): array {
		if (empty($ids)) {
			return [];
		}

		$stacks = [];
		$uncachedIds = [];
		foreach ($ids as $id) {
			if (isset($this->stackCache[(string)$id])) {
				$stacks[$id] = $this->stackCache[(string)$id];
			} else {
				$uncachedIds[] = $id;
			}
		}

		if (!empty($uncachedIds)) {
			$qb = $this->db->getQueryBuilder();
			$qb->select('*')
				->from($this->getTableName())
				->where($qb->expr()->in('id', $qb->createNamedParameter($uncachedIds, IQueryBuilder::PARAM_INT_ARRAY)));

			foreach ($this->findEntities($qb) as $stack) {
				$this->stackCache[(string)$stack->getId()] = $stack;
				$stacks[$stack->getId()] = $stack;
			}
		}

		return $stacks;
	}

	/**
	 * @throws \OCP\DB\Exception
	 */
	public function findStackFromCardId(int $cardId): ?Stack {
		$qb = $this->db->getQueryBuilder();
		$qb->select('s.*')
			->from($this->getTableName(), 's')
			->innerJoin('s', 'deck_cards', 'c', 's.id = c.stack_id')
			->where($qb->expr()->eq('c.id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)));

		try {
			return $this->findEntity($qb);
		} catch (MultipleObjectsReturnedException|DoesNotExistException $e) {
		}

		return null;
	}

	/**
	 * @return Stack[]
	 * @throws \OCP\DB\Exception
	 */
	public function findAll(int $boardId, ?int $limit = null, int $offset = 0): array {
		$qb = $this->db->getQueryBuilder();
		$qb->select('*')
			->from($this->getTableName())
			->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
			->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
			->setFirstResult($offset)
			->setMaxResults($limit);

		return $this->findEntities($qb);
	}

	/**
	 * @throws \OCP\DB\Exception
	 */
	public function findDeleted(int $boardId, ?int $limit = null, int $offset = 0): array {
		$qb = $this->db->getQueryBuilder();
		$qb->select('*')
			->from($this->getTableName())
			->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
			->andWhere($qb->expr()->neq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
			->setFirstResult($offset)
			->setMaxResults($limit);

		return $this->findEntities($qb);
	}

	public function update(Entity $entity): Entity {
		$result = parent::update($entity);
		$this->stackCache[(string)$entity->getId()] = $result;
		return $result;
	}

	public function clearDoneColumnForBoard(int $boardId): void {
		$qb = $this->db->getQueryBuilder();
		$qb->update($this->getTableName())
			->set('is_done_column', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
			->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
			->executeStatement();
	}

	public function setIsDoneColumn(int $stackId, bool $isDone): void {
		$qb = $this->db->getQueryBuilder();
		$qb->update($this->getTableName())
			->set('is_done_column', $qb->createNamedParameter($isDone, IQueryBuilder::PARAM_BOOL))
			->where($qb->expr()->eq('id', $qb->createNamedParameter($stackId, IQueryBuilder::PARAM_INT)))
			->executeStatement();
	}

	public function findDoneColumnForBoard(int $boardId): ?Stack {
		$qb = $this->db->getQueryBuilder();
		$qb->select('*')
			->from($this->getTableName())
			->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT)))
			->andWhere($qb->expr()->eq('is_done_column', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
			->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
		try {
			return $this->findEntity($qb);
		} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
			return null;
		}
	}

	public function delete(Entity $entity): Entity {
		// delete cards on stack
		$this->cardMapper->deleteByStack($entity->getId());
		unset($this->stackCache[(string)$entity->getId()]);
		return parent::delete($entity);
	}

	/**
	 * @throws \OCP\DB\Exception
	 */
	public function isOwner(string $userId, int $id): bool {
		$qb = $this->db->getQueryBuilder();
		$qb->select('s.id')
			->from($this->getTableName(), 's')
			->innerJoin('s', 'deck_boards', 'b', 'b.id = s.board_id')
			->where($qb->expr()->eq('s.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
			->andWhere($qb->expr()->eq('owner', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));

		return count($qb->executeQuery()->fetchAll()) > 0;
	}

	/**
	 * @throws \OCP\DB\Exception
	 */
	public function findBoardId(int $id): ?int {
		$result = $this->cache->get('findBoardId:' . $id);
		if ($result !== null) {
			return $result !== false ? $result : null;
		}
		try {
			$entity = $this->find($id);
			$result = $entity->getBoardId();
		} catch (DoesNotExistException $e) {
			$result = false;
		} catch (MultipleObjectsReturnedException $e) {
		}
		$this->cache->set('findBoardId:' . $id, $result);

		return $result !== false ? $result : null;
	}

	/**
	 * @return array<Stack>
	 * @throws \OCP\DB\Exception
	 */
	public function findToDelete(int $timeLimit): array {
		$qb = $this->db->getQueryBuilder();
		$qb->select('*')
			->from($this->getTableName())
			->where($qb->expr()->neq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
			->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($timeLimit, IQueryBuilder::PARAM_INT)));
		return $this->findEntities($qb);
	}
}
