<?php

namespace App\Modules\NiubizPurchase\CashMovementTemp\Domain;

use App\Modules\Shared\Domain\QueryBuilder\Operator\OperatorBuilder;
use App\Modules\Shared\Domain\Storage\Criteria\ConcreteStorageCriteria;

class IndexCashMovementTemp extends ConcreteStorageCriteria
{
    public function __construct(
        IdCashMovementTemp $idCashMovementTemp,
        private bool $lockInShare = false
    ) {
        $op = new OperatorBuilder();
        parent::__construct(
            CashMovementTemp::class,
            operator: $op->and(
                $op->equal('`Id_CashMovementTemp`', $idCashMovementTemp->value()),
                $op->notEqual('`CashMovementTemp_Status`', 0)
            ),
            limit: 1,
            lockInShare: $this->lockInShare
        );
    }

    public function analize($data)
    {
        if (!count($data)) {
            throw new CashMovementTempNotFound();
        }
        return CashMovementTemp::fromDTO($data[0]);
    }
}
