<?php

namespace App\Providers;

use App\Models\Invoice;
use App\Resources\InvoiceResource;

class InvoiceCsvFileProvider {

    /* @var Invoice[] */
    private array $invoices;

    public function __construct(array $invoices) {
        $this->invoices = $invoices;
    }

    public function createCsvFileContent() : string
    {
        $csvArray[] = implode(
            ',', [
            '"Rechnungsnummer"',
            '"Zahlungsgrund"',
            '"Name"',
            '"IBAN"',
            '"Kontoinhaber*in"',
            '"Betrag"',
            '"Spende"',
            '"Buchungstext"'
        ]);

        foreach ($this->invoices as $invoice) {
            $invoiceReadable = new InvoiceResource($invoice)->toArray();
            $csvLine = implode(',', [
                '"' . $invoiceReadable['invoiceNumber'] . '"',
                '"' . $invoiceReadable['invoiceType'] . '"',
                '"' . $invoiceReadable['contactName'] . '"',
                '"' . $invoiceReadable['accountIban'] . '"',
                '"' . $invoiceReadable['accountOwner'] . '"',
                '"' . $invoiceReadable['amountPlain'] . '"',
                '"' . $invoiceReadable['donation'] . '"',
                '"' . $invoiceReadable['paymentPurpose'] . '"'
            ]);

            $csvArray[] = $csvLine;
        }

       return implode(PHP_EOL, $csvArray);
    }
}
