<?php

namespace App\Commands\Sales;

use Carbon\Carbon;
use App\Commands\Command;
use App\DataObjects\CreateSaleDTO;
use App\Models\Sale;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CreateSaleCommand implements Command
{
    public function execute(array $data): Sale
    {
        return DB::transaction(function () use ($data, &$sale) {
            $tData = new CreateSaleDTO(
                reference_number: $data['current_series'],
                happened_on: Carbon::parse($data['happened_on']),
                branch_id: $data['branch_id'],
                user_id: Auth::user()->id,
            );

            return Sale::create($tData->toArray());
        });

    }
}
