<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
 * Mapper for the oc_mv_injuries table.
 *
 * Part of Issue #57.
 *
 * @extends QBMapper<Injury>
 */
class InjuryMapper extends QBMapper {

    public function __construct(IDBConnection $db) {
        parent::__construct($db, 'mv_injuries', Injury::class);
    }

    /**
     * @throws DoesNotExistException
     * @throws MultipleObjectsReturnedException
     * @throws Exception
     */
    public function findById(int $id): Injury {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
        return $this->findEntity($qb);
    }

    /**
     * Find all injuries with optional pagination.
     *
     * @return Injury[]
     * @throws Exception
     */
    public function findAll(?int $limit = null, ?int $offset = null): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->orderBy('datum', 'DESC');

        if ($limit !== null) {
            $qb->setMaxResults($limit);
        }
        if ($offset !== null) {
            $qb->setFirstResult($offset);
        }

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

    /**
     * Find all injuries for a specific member.
     *
     * @return Injury[]
     * @throws Exception
     */
    public function findByMemberId(int $memberId): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->where($qb->expr()->eq('member_id', $qb->createNamedParameter($memberId, IQueryBuilder::PARAM_INT)))
            ->orderBy('datum', 'DESC');

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

    /**
     * Find all injuries for a specific camp.
     *
     * @return Injury[]
     * @throws Exception
     */
    public function findByLagerId(int $lagerId): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->where($qb->expr()->eq('lager_id', $qb->createNamedParameter($lagerId, IQueryBuilder::PARAM_INT)))
            ->orderBy('datum', 'DESC');

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

    /**
     * Find injuries within a date range.
     *
     * @return Injury[]
     * @throws Exception
     */
    public function findByDateRange(string $from, string $to): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->where($qb->expr()->gte('datum', $qb->createNamedParameter($from)))
            ->andWhere($qb->expr()->lte('datum', $qb->createNamedParameter($to)))
            ->orderBy('datum', 'DESC');

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