<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Group;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Http\Request;

class ExportCurrencyVendorOrder implements FromView, ShouldAutoSize
{
    use Exportable;

    private $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    
    public function view(): View
    {
        $id = $this->request->route('id');

        $group = Group::findOrFail($id);

        $supplier = $group->issuerCompany; 

        $transferFeeTransactions = $group->transactions()->with([
            'transactions' => function ($transaction) {
                return $transaction->where('type', TransactionType::TRANSFER_FEE);
            }
        ])->get()->pluck('transactions')->flatten();

        return view('pages.pdfs.currency_vendor_order_inner', [
            'transactions' => $group->transactions, 
            'transferFeeTransactions' => $transferFeeTransactions, 
            'supplier' => $supplier
        ]);
    }
}
