<?php

namespace App\Domains\Invoice\Controllers;

use App\Enumerations\InvoiceStatus;
use App\Providers\InertiaProvider;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;

class ListMyInvoicesController extends CommonController {
    function __invoke(string $invoiceStatus) {
        $invoices = $this->invoices->getMyInvoicesByStatus($invoiceStatus);

        $subTabIndex = 0;
        switch ($invoiceStatus) {
            case InvoiceStatus::INVOICE_STATUS_NEW:
                $subTabIndex = 0;
                break;
            case InvoiceStatus::INVOICE_STATUS_APPROVED:
                $subTabIndex = 1;
                break;
            case InvoiceStatus::INVOICE_STATUS_DENIED:
                $subTabIndex = 2;
                break;

        }


        $inertiaProvider = new InertiaProvider('Invoice/ListMyInvoices', [
            'invoices' => $invoices,
            'endpoint' => $invoiceStatus,
            'currentStatus' => $subTabIndex,
        ]);
        return $inertiaProvider->render();
    }

    public function getMyInvoicesByStatus(string $invoiceStatus) : JsonResponse {
        $invoices = $this->invoices->getMyInvoicesByStatus($invoiceStatus);

        $title = '';
        switch ($invoiceStatus) {
            case InvoiceStatus::INVOICE_STATUS_NEW:
                $title = 'Neue Abrechnungen';
                break;
            case InvoiceStatus::INVOICE_STATUS_APPROVED:
                $title = 'Freigegebene Abrechnungen, nicht exportierte Abrechnungen';
                break;
            case InvoiceStatus::INVOICE_STATUS_DENIED:
                $title = 'Abgelehnte Abrechnungen';
                break;

        }

        return response()->json([
            'title' => $title,
            'endpoint' => $invoiceStatus,
            'invoices' => $invoices,
        ]);
    }

}
