<?php

namespace App\Actions\Transactions;

use App\DataObjects\CreateTransactionDTO;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Pipeline;

class CreateRecordTransactionsAction
{
    public function __invoke(Model $record, array $transactions): void
    {
        foreach ($transactions as $transaction) {
            $tData = [
                'branch_id' => $record->branch_id,
                'happened_on' => $record->happened_on,
                ...$transaction,
            ];

            $payload = new CreateTransactionDTO(data: $tData, transactionable: $record);

            Pipeline::send(passable: $payload)->through(
                [
                    CreateTransactionAction::class,
                ]
            )->thenReturn();
        }
    }
}

