<?php

namespace App\Resources;

use App\Enumerations\InvoiceStatus;
use App\Enumerations\InvoiceType;
use App\Models\Invoice;
use App\Models\User;
use App\ValueObjects\Amount;

class InvoiceResource {
    private Invoice $invoice;

    function __construct(Invoice $invoice) {
        $this->invoice = $invoice;
    }

    public function toArray() {
        $returnData = [];
        $returnData['invoiceType'] = $this->invoice->invoiceType()->name;
        if ($this->invoice->invoiceType()->slug === InvoiceType::INVOICE_TYPE_OTHER) {
            $returnData['invoiceType'] .= ' (' . $this->invoice->type_other . ')';
        }
        $approvedBy = '--';
        $approvedAt = '--';

        if ($this->invoice->approved_at !== null) {
            $approvedBy = User::findOrFail($this->invoice->approved_by)->getOfficialName();
            $approvedAt = \DateTime::createFromFormat('Y-m-d H:i:s', $this->invoice->approved_at)->format('d.m.Y H:i');
        }

        $returnData['invoiceTypeShort'] = $this->invoice->invoiceType()->name;
        $returnData['costUnitName']  = $this->invoice->costUnit()->first()->name;
        $returnData['invoiceNumber'] = $this->invoice->invoice_number;
        $returnData['contactName'] = $this->invoice->contact_name;
        $returnData['contactEmail'] = $this->invoice->contact_email ?? '--';
        $returnData['contactPhone'] = $this->invoice->contact_phone ?? '--';
        $returnData['amount'] = Amount::fromString($this->invoice->amount, ' Euro')->toString();
        $returnData['id'] = $this->invoice->id;
        $returnData['donation'] = $this->invoice->donation;
        $returnData['externalPayment'] = null !== $this->invoice->payment_purpose;
        $returnData['paymentPurpose'] = $this->invoice->payment_purpose ?? 'Auslagenerstattung Rechnungsnummer ' . $returnData['invoiceNumber'];
        $returnData['accountOwner'] = $this->invoice->contact_bank_owner ?? '--';
        $returnData['accountIban'] = $this->invoice->contact_bank_iban ?? '--';
        $returnData['status'] = $this->invoice->status;
        $returnData['documentFilename'] = $this->invoice->document_filename;
        $returnData['readableStatus'] = $this->getReadableStatus();
        $returnData['comment'] = $this->invoice->comment ?? '--';
        $returnData['changes'] = $this->invoice->changes ?? '--';
        $returnData['deniedReason'] = $this->invoice->denied_reason ?? '--';
        $returnData['travelDirection'] = $this->invoice->travel_direction ?? '--';
        $returnData['travelReason'] = $this->invoice->travel_reason ?? '--';
        $returnData['distance'] = $this->invoice->distance ?? '--';
        $returnData['distanceAllowance'] = new Amount($this->invoice->costUnit()->first()->distance_allowance, '')->toString();
        $returnData['passengers'] = $this->invoice->passengers ? 'Ja' : 'Nein';
        $returnData['transportation'] = $this->invoice->transportation ? 'Ja' : 'Nein';
        $returnData['travelRoute'] = $this->invoice->travel_direction;
        $returnData['costUnitId'] = $this->invoice->cost_unit_id;
        $returnData['amountPlain'] = new Amount($this->invoice->amount, '')->toString();
        $returnData['internalType'] = $this->invoice->type;
        $returnData['donationText'] = $this->invoice->donation ? 'Ja' : 'Nein';
        $returnData['createdAt'] = \DateTime::createFromFormat('Y-m-d H:i:s', $this->invoice->created_at)->format('d.m.Y H:i');
        $returnData['approvedAt'] = $approvedAt;
        $returnData['approvedBy'] = $approvedBy;

        if ($this->invoice->status === InvoiceStatus::INVOICE_STATUS_APPROVED) {
            $returnData['approvedAt'] = \DateTime::createFromFormat('Y-m-d H:i:s', $this->invoice->approved_at)->format('d.m.Y H:i');
            $returnData['approvedBy'] = $this->invoice->approvedBy()->first()->username ?? '--';
        }

        $returnData['externalPayment'] = $returnData['externalPayment'] == '' ? 0 : 1;

        return $returnData;
    }

    private function getReadableStatus() : string {
        switch ($this->invoice->status) {
            case InvoiceStatus::INVOICE_STATUS_NEW:
                return 'Unbearbeitet';
            case InvoiceStatus::INVOICE_STATUS_APPROVED:
                return 'Nicht exportiert';
            case InvoiceStatus::INVOICE_STATUS_DENIED:
                return 'Abgelehnt';
            case InvoiceStatus::INVOICE_STATUS_EXPORTED:
                return 'Exportiert';
        }
        return $this->invoice->status;
    }

}
