<?php
namespace Tests\Unit\Domain\Order;

use PHPUnit\Framework\TestCase;
use Domain\Order\OrderRepository;

class OrderRepositoryTest extends TestCase
{
    public function testOrderStatusesReturnsMappedArray(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->method('select')
            ->willReturnCallback(function ($table, $columns, $where) {
                if ($table === 'pp_shop_statuses') {
                    return [
                        ['id' => 0, 'status' => 'Nowe'],
                        ['id' => 4, 'status' => 'W realizacji'],
                    ];
                }

                return [];
            });

        $repository = new OrderRepository($mockDb);
        $statuses = $repository->orderStatuses();

        $this->assertIsArray($statuses);
        $this->assertSame('Nowe', $statuses[0]);
        $this->assertSame('W realizacji', $statuses[4]);
    }

    public function testNextAndPrevOrderIdReturnNullForInvalidInput(): void
    {
        $mockDb = $this->createMock(\medoo::class);
        $mockDb->expects($this->never())->method('get');

        $repository = new OrderRepository($mockDb);

        $this->assertNull($repository->nextOrderId(0));
        $this->assertNull($repository->prevOrderId(0));
    }

    public function testListForAdminReturnsItemsAndTotal(): void
    {
        $mockDb = $this->createMock(\medoo::class);

        $resultSetCount = new class {
            public function fetchAll(): array
            {
                return [[3]];
            }
        };

        $resultSetData = new class {
            public function fetchAll(): array
            {
                return [
                    [
                        'id' => 11,
                        'number' => '2026/02/001',
                        'date_order' => '2026-02-15 10:00:00',
                        'client' => 'Jan Kowalski',
                        'order_email' => 'jan@example.com',
                        'address' => 'Testowa 1, 00-000 Warszawa',
                        'status' => 0,
                        'client_phone' => '111222333',
                        'transport' => 'Kurier',
                        'payment_method' => 'Przelew',
                        'summary' => '123.45',
                        'paid' => 1,
                        'total_orders' => 2,
                    ],
                ];
            }
        };

        $callIndex = 0;
        $mockDb->method('query')
            ->willReturnCallback(function () use (&$callIndex, $resultSetCount, $resultSetData) {
                $callIndex++;
                return $callIndex === 1 ? $resultSetCount : $resultSetData;
            });

        $repository = new OrderRepository($mockDb);
        $result = $repository->listForAdmin([], 'date_order', 'DESC', 1, 15);

        $this->assertSame(3, $result['total']);
        $this->assertCount(1, $result['items']);
        $this->assertSame(11, $result['items'][0]['id']);
        $this->assertSame('2026/02/001', $result['items'][0]['number']);
        $this->assertSame(2, $result['items'][0]['total_orders']);
        $this->assertSame(1, $result['items'][0]['paid']);
    }
}
