<?php

namespace App\Repositories;

use App\Enumerations\InvoiceStatus;
use App\Enumerations\UserRole;
use App\Models\CostUnit;
use App\Models\Invoice;
use App\Providers\AuthCheckProvider;
use App\Resources\InvoiceResource;
use App\ValueObjects\Amount;
use Illuminate\Database\Eloquent\Collection;

class InvoiceRepository {
    public function getMyInvoicesWidget() : array {
        $invoices = [
            InvoiceStatus::INVOICE_STATUS_NEW => ['slug' => InvoiceStatus::INVOICE_STATUS_NEW, 'title' => 'Neue Abrechnungen', 'count' => 0, 'amount' => Amount::fromString('0')->toString()],
            InvoiceStatus::INVOICE_STATUS_APPROVED => ['slug' => InvoiceStatus::INVOICE_STATUS_APPROVED, 'title' => 'Freigegebene Abrechnungen', 'count' => 0, 'amount' => Amount::fromString('0')->toString()],
            InvoiceStatus::INVOICE_STATUS_DENIED =>  ['slug' => InvoiceStatus::INVOICE_STATUS_DENIED, 'title' => 'Abgelehnte Abrechnungen', 'count' => 0, 'amount' => Amount::fromString('0')->toString()]
        ];


        $user = auth()->user();
        if (null === $user) {
            return $invoices;
        }

        foreach ([InvoiceStatus::INVOICE_STATUS_NEW, InvoiceStatus::INVOICE_STATUS_APPROVED, InvoiceStatus::INVOICE_STATUS_DENIED] as $status) {
            $amount = 0;
            $count = 0;
            foreach (Invoice::where(['user_id' => $user->id, 'status' => $status])->get() as $stack) {
                $count++;
                $amount += $stack->amount;
            }

            $invoices[$status]['count'] = $count;
            $invoices[$status]['amount'] = Amount::fromString($amount)->toString();
        }

        return $invoices;
    }

    public function getUnexportedInvoices() : Collection {
        return Invoice::where(['tenant' => app('tenant')->slug, 'status' => InvoiceStatus::INVOICE_STATUS_EXPORTED, 'upload_required' => true])->get();
    }

    public function getByStatus(CostUnit $costUnit, string $status, bool $forDisplay = true) : array {
        $returnData = [];
        foreach ($costUnit->invoices()->where('status', $status)->get() as $invoice) {
            if ($forDisplay) {
                $returnData[] = new InvoiceResource($invoice)->toArray();
            } else {
                $returnData[] = $invoice;
            }

        };

        return $returnData;
    }

    public function getMyInvoicesByStatus(string $status) : array {
        $returnData = [];
        foreach (Invoice::where(
            [
                'status' => $status,
                'user_id' => auth()->user()->id,
                'tenant' => app('tenant')->slug,

            ]
            )->get() as $invoice) {
            $returnData[] = new InvoiceResource($invoice)->toArray();
        };
        return $returnData;
    }

    public function getAsTreasurer(int $invoiceId) : ?Invoice {
        $invoice = Invoice::where('id', $invoiceId)->first();
        if ($invoice === null) {
            return null;
        }

        $isTreasurer = $invoice->costUnit()->first()->treasurers()->where('user_id', auth()->user()->id)->exists();
        if ($isTreasurer) {
            return $invoice;
        }

        return in_array(new AuthCheckProvider()->getUserRole(), [
            UserRole::USER_ROLE_ADMIN, UserRole::USER_ROLE_GROUP_LEADER
        ]) ? $invoice : null;

    }
}
