<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Tests\Db;

use OCA\Mitgliederverwaltung\Db\GeneralMaterial;
use OCA\Mitgliederverwaltung\Db\GeneralMaterialMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\IResult;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class GeneralMaterialMapperTest extends TestCase {

    private IDBConnection&MockObject $db;
    private IQueryBuilder&MockObject $qb;
    private IExpressionBuilder&MockObject $expr;
    private IResult&MockObject $result;

    protected function setUp(): void {
        parent::setUp();

        $this->db = $this->createMock(IDBConnection::class);
        $this->qb = $this->createMock(IQueryBuilder::class);
        $this->expr = $this->createMock(IExpressionBuilder::class);
        $this->result = $this->createMock(IResult::class);

        $this->qb->method('expr')->willReturn($this->expr);
        $this->qb->method('select')->willReturnSelf();
        $this->qb->method('from')->willReturnSelf();
        $this->qb->method('where')->willReturnSelf();
        $this->qb->method('andWhere')->willReturnSelf();
        $this->qb->method('orderBy')->willReturnSelf();
        $this->qb->method('gte')->willReturn('col >= :param');
        $this->qb->method('lte')->willReturn('col <= :param');
        $this->qb->method('isNotNull')->willReturn('col IS NOT NULL');
        $this->qb->method('insert')->willReturnSelf();
        $this->qb->method('delete')->willReturnSelf();
        $this->qb->method('values')->willReturnSelf();
        $this->qb->method('createNamedParameter')->willReturn(':param');
        $this->qb->method('createFunction')->willReturnCallback(fn($call) => $call);
        $this->qb->method('getSQL')->willReturn('SELECT * FROM test');

        $this->expr->method('eq')->willReturn('col = :param');
        $this->expr->method('isNull')->willReturn('col IS NULL');
        $this->expr->method('neq')->willReturn('col != :param');
        $this->expr->method('orX')->willReturn('or_expr');

        $this->db->method('getQueryBuilder')->willReturn($this->qb);
    }

    private function configureResultRows(array $rows): void {
        $fetchCalls = array_merge($rows, [false]);
        $this->result->method('fetch')
            ->willReturnOnConsecutiveCalls(...$fetchCalls);
        $this->result->method('closeCursor')->willReturn(true);
        $this->qb->method('executeQuery')->willReturn($this->result);
    }

    private function configureResultSingleRow(array $row): void {
        $this->result->method('fetch')
            ->willReturnOnConsecutiveCalls($row, false);
        $this->result->method('closeCursor')->willReturn(true);
        $this->qb->method('executeQuery')->willReturn($this->result);
    }

    private function configureResultEmpty(): void {
        $this->result->method('fetch')->willReturn(false);
        $this->result->method('closeCursor')->willReturn(true);
        $this->qb->method('executeQuery')->willReturn($this->result);
    }

    private function itemRow(int $id = 1, string $name = 'Zelt A', ?int $condition = 4, ?string $notes = 'Sehr gut'): array {
        return [
            'id' => $id,
            'name' => $name,
            'condition' => $condition,
            'notes' => $notes,
            'created_at' => '2026-04-21 00:00:00',
            'updated_at' => '2026-04-21 00:00:00',
        ];
    }

    public function testCreateAndFind(): void {
        $this->configureResultSingleRow($this->itemRow(42, 'Zelt A', 4, 'Sehr gut'));
        $mapper = new GeneralMaterialMapper($this->db);
        $item = $mapper->findById(42);
        $this->assertSame(42, $item->getId());
        $this->assertSame('Zelt A', $item->getName());
        $this->assertSame(4, $item->getCondition());
    }

    public function testFindAll(): void {
        $this->configureResultRows([
            $this->itemRow(1, 'Zelt A', 4),
            $this->itemRow(2, 'Zelt B', 3),
        ]);
        $mapper = new GeneralMaterialMapper($this->db);
        $items = $mapper->findAll();
        $this->assertCount(2, $items);
        $this->assertSame('Zelt A', $items[0]->getName());
    }

    public function testFindByConditionRange(): void {
        $this->configureResultRows([$this->itemRow(1, 'Zelt A', 1), $this->itemRow(2, 'Zelt B', 2)]);
        $mapper = new GeneralMaterialMapper($this->db);
        $items = $mapper->findByConditionRange(0, 2);
        $this->assertCount(2, $items);
        $this->assertLessThanOrEqual(2, $items[0]->getCondition());
    }

    public function testFindByConditionRangeEmpty(): void {
        $this->configureResultRows([]);
        $mapper = new GeneralMaterialMapper($this->db);
        $items = $mapper->findByConditionRange(4, 5);
        $this->assertCount(0, $items);
    }

    public function testUpdateCondition(): void {
        $this->configureResultSingleRow($this->itemRow(1, 'Zelt A', 3, 'Gut'));
        $mapper = new GeneralMaterialMapper($this->db);
        $item = $mapper->findById(1);
        $this->assertSame(3, $item->getCondition());
        $this->assertSame('Gut', $item->getNotes());
    }
}
