<?php

namespace App\Domains\Invoice\Controllers;

use App\Domains\Invoice\Actions\CreateInvoice\CreateInvoiceCommand;
use App\Domains\Invoice\Actions\CreateInvoice\CreateInvoiceRequest;
use App\Enumerations\CostUnitType;
use App\Enumerations\InvoiceType;
use App\Models\CostUnit;
use App\Providers\FlashMessageProvider;
use App\Providers\InertiaProvider;
use App\Providers\UploadFileProvider;
use App\Scopes\CommonController;
use App\ValueObjects\Amount;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NewInvoiceController extends CommonController {
    public function __invoke() {
        $userData = $this->users->getCurrentUserDetails();

        $runningJobs = $this->costUnits->getCostUnitsForNewInvoice(CostUnitType::COST_UNIT_TYPE_RUNNING_JOB);
        $currentEvents = $this->costUnits->getCostUnitsForNewInvoice(CostUnitType::COST_UNIT_TYPE_EVENT);


        $inertiaProvider = new InertiaProvider('Invoice/NewInvoice', [
            'userName' => $userData['userName'],
            'userEmail' => $userData['userEmail'],
            'userTelephone' => $userData['userTelephone'],
            'userAccountOwner' => $userData['userAccountOwner'],
            'userAccountIban' => $userData['userAccountIban'],
            'runningJobs' => $runningJobs,
            'currentEvents' => $currentEvents,
        ]);
        return $inertiaProvider->render();
    }

    public function saveInvoice(Request $request, int $costUnitId, string $invoiceType) : JsonResponse {
        $costUnit = $this->costUnits->getById($costUnitId, true);
        if (null === $costUnit) {
            return response()->json([
                'status' => 'error',
                'message' => 'Beim Speichern ist ein Fehler aufgetreten. Bitte starte den Vorgang erneut.'
            ]);
        }

        $uploadedFile = null;
        if (null !== $request->file('receipt')) {
            $validation = sprintf('%1$s|%2$s|max:%3$s',
                'required',
                'mimes:pdf',
                env('MAX_INVOICE_FILE_SIZE', 16)*10
            );

            $request->validate([
                'receipt' => $validation
            ]);

            $uploadFileProvider = new UploadFileProvider($request->file('receipt'), $costUnit);
            $uploadedFile = $uploadFileProvider->saveUploadedFile();
        }

        switch ($invoiceType) {
            case InvoiceType::INVOICE_TYPE_TRAVELLING:

                if ($uploadedFile !== null) {
                    $amount = Amount::fromString($request->input('amount'))->getAmount();
                    $distance = null;
                } else {
                    $distance = Amount::fromString($request->input('amount'))->getRoundedAmount();
                    $amount = $distance  * $costUnit->distance_allowance;

                }

                $createInvoiceRequest = new CreateInvoiceRequest(
                    $costUnit,
                    $request->input('name'),
                    InvoiceType::INVOICE_TYPE_TRAVELLING,
                    $amount,
                    $uploadedFile,
                    'donation' === $request->input('decision') ? true : false,
                    $this->users->getCurrentUserDetails()['userId'],
                    $request->input('email'),
                    $request->input('telephone'),
                    $request->input('accountOwner'),
                    $request->input('accountIban'),
                    null,
                    $request->input('otherText'),
                    $distance,
                    $request->input('havePassengers'),
                    $request->input('materialTransportation'),
                    $request->input('travelReason'),
                );

                break;

            default:
                $createInvoiceRequest = new CreateInvoiceRequest(
                    $costUnit,
                    $request->input('name'),
                    $invoiceType,
                    Amount::fromString($request->input('amount'))->getAmount(),
                    $uploadedFile,
                    'donation' === $request->input('decision') ? true : false,
                    $this->users->getCurrentUserDetails()['userId'],
                    $request->input('email'),
                    $request->input('telephone'),
                    $request->input('accountOwner'),
                    $request->input('accountIban'),
                    $request->input('otherText'),
                    null,
                    null,
                    $request->input('havePassengers'),
                    $request->input('materialTransportation'),
                    null
                );

                break;
        }

        $command = new CreateInvoiceCommand($createInvoiceRequest);
        $response = $command->execute();
        if ($response->success) {
            new FlashMessageProvider(
            'Die Abrechnung wurde erfolgreich angelegt.' . PHP_EOL . PHP_EOL . 'Sollten wir Rückfragen haben, melden wir uns bei dir',
            'success'
            );

            return response()->json([
                'status' => 'success',
                'message' => 'Alright'
            ]);
        }
    }
}
