<?php

namespace App\Mail\InvoiceMails;

use App\Enumerations\InvoiceStatus;
use App\Enumerations\InvoiceType;
use App\Models\CostUnit;
use App\Models\Invoice;
use App\Resources\CostUnitResource;
use App\Resources\InvoiceResource;
use App\ValueObjects\Amount;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

class InvoiceMailsApprovedInvoiceMail extends Mailable {
    public function __construct(
        private Invoice $invoice,
        private CostUnit $costUnit,
    )
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Deine Abrechnung wurde zur Auszahlung freigegeben',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $costUnit = new CostUnitResource($this->costUnit)->toArray(new Request());
        $invoice = new InvoiceResource($this->invoice)->toArray(new Request());

        $invoiceStatus = match($invoice['status']) {
            InvoiceStatus::INVOICE_STATUS_APPROVED => 'Zur Auszahlung freigegeben',
            InvoiceStatus::INVOICE_STATUS_DENIED => 'Nicht auszahlbar und zurückgewiesen',
            InvoiceStatus::INVOICE_STATUS_NEW => 'Erneut zur Prüfung geöffnet',
            default => 'Unbekannt',
        };

        $invoiceType = match($invoice['internalType']) {
            InvoiceType::INVOICE_TYPE_TRAVELLING => sprintf( 'Reisekosten [%1$s]', $invoice['travelReason']),
            default => $invoice['invoiceType'],
        };

        return new Content(
            view: 'emails.invoices.changed_invoice_state',
            with: [
                'costUnitContactMail' => $this->costUnit->tenant()->first()->email_finance,
                'invoiceStatus' => $invoiceStatus,
                'costUnit' => $costUnit,
                'invoice' => $invoice,
                'invoiceType' => $invoiceType,
            ],
        );
    }


    /**
     * Get the attachments for the message.
     *
     * @return array<int, Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}
