<?php

namespace App\Resources;

use App\Enumerations\InvoiceStatus;
use App\Enumerations\InvoiceType;
use App\Models\CostUnit;
use App\Repositories\CostUnitRepository;
use App\ValueObjects\Amount;

class CostUnitResource {
    private CostUnit $costUnit;

    public function __construct(CostUnit $costUnit) {
        $this->costUnit = $costUnit;
    }

    public function toArray($request) {
        $costUnitRepository = new CostUnitRepository();


        $totalAmount = $costUnitRepository->sumupAmounts($this->costUnit)->getAmount();
        $donatedAmount = $costUnitRepository->sumupAmounts($this->costUnit, true)->getAmount();

        $countInvoices = $costUnitRepository->countInvoices($this->costUnit);

        $countNewInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_NEW];
        $countApprovedInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_APPROVED];
        $countDonatedInvoices = $countInvoices[InvoiceStatus::INVOICE_META_STATUS_DONATED];
        $countDeniedInvoices = $countInvoices[InvoiceStatus::INVOICE_STATUS_DENIED];

        $amounts = [];
        $overAllAmount = new Amount(0, 'Euro');
        $overAllEstimatedAmount = new Amount(0, 'Euro');
        foreach (InvoiceType::orderBy('sort_order')->get() as $invoiceType) {
            $overAllAmount->addAmount($costUnitRepository->sumupByInvoiceType($this->costUnit, $invoiceType));
            $overAllEstimatedAmount->addAmount($costUnitRepository->sumupEstimatedByInvoiceType($this->costUnit, $invoiceType));
            $amounts[$invoiceType->slug]['string'] = $costUnitRepository->sumupByInvoiceType($this->costUnit, $invoiceType)->toString();
            $amounts[$invoiceType->slug]['name'] = $invoiceType->name;
            $amounts[$invoiceType->slug]['estimated'] = $costUnitRepository->sumupEstimatedByInvoiceType($this->costUnit, $invoiceType);
            $amounts[$invoiceType->slug]['estimatedString'] = $costUnitRepository->sumupEstimatedByInvoiceType($this->costUnit, $invoiceType)->toString();

        }


        $data = array_merge(
            $this->costUnit->toArray(),
            [
                'distanceAllowanceSmall' => new Amount($this->costUnit->distance_allowance, '')->toString(),
                'distanceAllowanceFull' => new Amount($this->costUnit->distance_allowance, ' Euro')->toString(),
                'totalAmount' => new Amount($totalAmount, ' Euro')->toString(),
                'donatedAmount' => new Amount($donatedAmount, ' Euro')->toString(),
                'countNewInvoices' => $countNewInvoices,
                'countApprovedInvoices' => $countApprovedInvoices,
                'countDonatedInvoices' => $countDonatedInvoices,
                'countDeniedInvoices' => $countDeniedInvoices,
                'treasurers' => $this->costUnit->treasurers()->get()->map(fn($user) => new UserResource($user))->toArray(),
                'amounts' => $amounts,
                'overAllAmount' => ['text' => $overAllAmount->toString(), 'value' => $overAllAmount],
                'overAllEstimatedAmount' => ['text' => $overAllEstimatedAmount->toString(), 'value' => $overAllEstimatedAmount],
            ]);


        return $data;
    }
}

