<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Db;

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

/**
 * @extends QBMapper<StufeHistory>
 */
class StufeHistoryMapper extends QBMapper {

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

    /**
     * @return StufeHistory[]
     * @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('changed_at', 'DESC');
        return $this->findEntities($qb);
    }

    /**
     * @return StufeHistory[]
     * @throws Exception
     */
    public function findAll(): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->orderBy('changed_at', 'DESC');
        return $this->findEntities($qb);
    }

    /**
     * @return StufeHistory[]
     * @throws Exception
     */
    public function findByStufeId(int $stufeId): array {
        $qb = $this->db->getQueryBuilder();
        $qb->select('*')
            ->from($this->getTableName())
            ->where($qb->expr()->eq('stufe_id', $qb->createNamedParameter($stufeId, IQueryBuilder::PARAM_INT)))
            ->orderBy('changed_at', 'DESC');
        return $this->findEntities($qb);
    }
}
