<?php

namespace App\Domains\CostUnit\Actions\CreateCostUnit;

use App\Models\CostUnit;

class CreateCostUnitCommand {
    private CreateCostUnitRequest $request;

    public function __construct(CreateCostUnitRequest $request) {
        $this->request = $request;
    }

    public function execute() : CreateCostUnitResponse {
        $response = new CreateCostUnitResponse();
        $costUnit = CostUnit::create([
            'name' => $this->request->name,
            'tenant' => app('tenant')->slug,
            'type' => $this->request->type,
            'billing_deadline' => $this->request->billingDeadline,
            'distance_allowance' => $this->request->distanceAllowance->getAmount(),
            'mail_on_new' => $this->request->mailOnNew,
            'allow_new' => true,
            'archived' => false,
        ]);

        if (null !== $costUnit) {
            $response->costUnit = $costUnit;
            $response->success = true;
        }

        return $response;
    }
}
