<?php

namespace App\Domains\Invoice\Controllers;

use App\Domains\Invoice\Actions\ChangeStatus\ChangeStatusCommand;
use App\Domains\Invoice\Actions\ChangeStatus\ChangeStatusRequest;
use App\Domains\Invoice\Actions\CreateInvoice\CreateInvoiceCommand;
use App\Domains\Invoice\Actions\CreateInvoice\CreateInvoiceRequest;
use App\Domains\Invoice\Actions\UpdateInvoice\UpdateInvoiceCommand;
use App\Domains\Invoice\Actions\UpdateInvoice\UpdateInvoiceRequest;
use App\Enumerations\CostUnitType;
use App\Enumerations\InvoiceStatus;
use App\Enumerations\InvoiceType;
use App\Resources\InvoiceResource;
use App\Scopes\CommonController;
use App\ValueObjects\Amount;
use App\ValueObjects\InvoiceFile;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class EditController extends CommonController{
    public function copyInvoice(int $invoiceId) : JsonResponse{
        $invoice = $this->invoices->getAsTreasurer($invoiceId);
        if ($invoice === null) {
            return response()->json([]);
        }

        $receiptfile = null;
        if ($invoice->document_filename !== null) {
            $receiptfile = new InvoiceFile();
            $receiptfile->filename = $invoice->document_filename;
            $receiptfile->fullPath = $invoice->document_filename;
        }
        $createInvoiceRequest = new CreateInvoiceRequest(
            $invoice->costUnit()->first(),
            $invoice->contact_name,
            $invoice->type,
            $invoice->amount,
            $receiptfile,
            $invoice->donation,
            $invoice->user_id,
            $invoice->contact_email,
            $invoice->contact_phone,
            $invoice->contact_bank_owner,
            $invoice->contact_bank_iban,
            $invoice->type_other,
            $invoice->travel_direction,
            $invoice->distance,
            $invoice->passengers,
            $invoice->transportation,
            $invoice->travel_reason,
            $invoice->payment_purpose,
            $invoice->comment,
        );

        $invoiceCreationCommand = new CreateInvoiceCommand($createInvoiceRequest);
        $newInvoice = $invoiceCreationCommand->execute()->invoice;

        $invoiceDenyRequest = new ChangeStatusRequest($invoice,InvoiceStatus::INVOICE_STATUS_DENIED, 'Abrechnungskorrektur in Rechnungsnummer #' . $newInvoice->invoice_number . ' erstellt.');
        $invoiceDenyCommand = new ChangeStatusCommand($invoiceDenyRequest);
        $invoiceDenyCommand->execute();


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

        return response()->json([
            'invoice' => new InvoiceResource($newInvoice)->toArray(),
            'status' => 'success',
            'costUnits' => array_merge($runningJobs, $currentEvents),
        ]);
    }

    public function updateInvoice(int $invoiceId, Request $request) : JsonResponse {
        $invoice = $this->invoices->getAsTreasurer($invoiceId);
        if ($invoice === null) {
            return response()->json([]);
        }

        $modifyData = $request->get('invoiceData');

        $newAmount = Amount::fromString($modifyData['amount']);
        $amountLeft = Amount::fromString($invoice->amount);
        $amountLeft->subtractAmount($newAmount);

        $newCostUnit = $this->costUnits->getById($modifyData['cost_unit'],true);
        $invoiceType = InvoiceType::where('slug', $modifyData['type_internal'])->first();


        $updateInvoiceRequest = new UpdateInvoiceRequest(
            $invoice,
            $modifyData['notices'],
            $invoiceType,
            $newCostUnit,
            $newAmount
        );
        $updateInvoiceCommand = new UpdateInvoiceCommand($updateInvoiceRequest);
        $updateInvoiceCommand->execute();


        $newInvoice = null;
        if (isset($modifyData['duplicate']) && $modifyData['duplicate'] === true) {
            $receiptfile = null;
            if ($invoice->document_filename !== null) {
                $receiptfile = new InvoiceFile();
                $receiptfile->filename = $invoice->document_filename;
                $receiptfile->fullPath = $invoice->document_filename;
            }
            $createInvoiceRequest = new CreateInvoiceRequest(
                $invoice->costUnit()->first(),
                $invoice->contact_name,
                $invoice->type,
                $amountLeft->getAmount(),
                $receiptfile,
                $invoice->donation,
                $invoice->user_id,
                $invoice->contact_email,
                $invoice->contact_phone,
                $invoice->contact_bank_owner,
                $invoice->contact_bank_iban,
                $invoice->type_other,
                $invoice->travel_direction,
                $invoice->distance,
                $invoice->passengers,
                $invoice->transportation
            );

            $invoiceCreationCommand = new CreateInvoiceCommand($createInvoiceRequest);
            $newInvoice = $invoiceCreationCommand->execute()->invoice;
        }

        $useInvoice = $newInvoice ?? $invoice;
        $do_copy = $newInvoice !== null ? true : false;
        return response()->json([
            'invoice' => new InvoiceResource($useInvoice)->toArray(),
            'do_copy' => $do_copy,
        ]);
    }
}
