<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Tests\Db;

use OCA\Mitgliederverwaltung\Db\StockVariant;
use OCA\Mitgliederverwaltung\Db\StockVariantMapper;
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 StockVariantMapperTest 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('orderBy')->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->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 variantRow(int $id = 1, int $stockItemId = 1, string $label = 'Größe M', int $amount = 10, string $cost = '25.00', int $minThreshold = 3): array {
        return [
            'id' => $id,
            'stock_item_id' => $stockItemId,
            'label' => $label,
            'amount' => $amount,
            'cost' => $cost,
            'min_threshold' => $minThreshold,
        ];
    }

    public function testCreate_attachToStockItem(): void {
        $this->configureResultSingleRow($this->variantRow(1, 1, 'Größe M'));
        $mapper = new StockVariantMapper($this->db);
        $variant = $mapper->findById(1);
        $this->assertSame(1, $variant->getId());
        $this->assertSame(1, $variant->getStockItemId());
        $this->assertSame('Größe M', $variant->getLabel());
    }

    public function testFindByStockItemId(): void {
        $this->configureResultRows([
            $this->variantRow(1, 1, 'Größe M'),
            $this->variantRow(2, 1, 'Größe L'),
        ]);
        $mapper = new StockVariantMapper($this->db);
        $variants = $mapper->findByStockItemId(1);
        $this->assertCount(2, $variants);
        $this->assertSame('Größe M', $variants[0]->getLabel());
        $this->assertSame('Größe L', $variants[1]->getLabel());
    }

    public function testFindByStockItemIdEmpty(): void {
        $this->configureResultRows([]);
        $mapper = new StockVariantMapper($this->db);
        $variants = $mapper->findByStockItemId(999);
        $this->assertCount(0, $variants);
    }
}
