<?php

namespace App\Tasks;

use App\Domains\Invoice\Actions\UploadInvoice\UploadInvoiceCommand;
use App\Domains\Invoice\Actions\UploadInvoice\UploadInvoiceRequest;
use App\Repositories\InvoiceRepository;
use Psr\Log\LoggerInterface;

class UploadInvoices implements CronTask {
    public function handle(): void
    {
        if (!app('tenant')->upload_exports) {
            return;
        }

        $invoiceRepository = new InvoiceRepository();
        foreach ($invoiceRepository->getUnexportedInvoices() as $invoice) {
            app('taskLogger')->info("Uploading invoice {$invoice->invoice_number}");
            $request = new UploadInvoiceRequest($invoice);
            $command = new UploadInvoiceCommand($request);
            if ($command->execute()->success) {
                app('taskLogger')->info('Upload successful');
            } else {
                app('taskLogger')->error('Upload failed');
            }

            app('taskLogger')->info('------------------------------------');
        };
    }
}
