<?php

namespace App\Actions\Account;

use App\Actions\BaseAction;
use App\Commands\Account\CreateAccountCommand;
use App\DataObjects\CreateAccountDTO;
use Closure;
use Exception;
use LogicException;
use Spatie\LaravelData\Data;

class StoreAccount extends BaseAction
{
    public function __construct(private readonly CreateAccountCommand $createAccountCommand) {}

    public function __invoke(CreateAccountDTO|Data $payload, Closure $next)
    {
        try {
            $payload->account = $this->createAccountCommand->execute($payload->data);

            return $next($payload);

        } catch (Exception $exception) {
            throw new LogicException('Error Storing Account: '.$exception->getMessage());
        }
    }
}
