<?php

namespace App\Http\Controllers\Exports;

use App\Classes\Modules\Exports\Services\ExportsCustomersWalletTransactionHistory;
use App\Models\User;
use App\Models\Wallet;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;

class ExportCustomersWalletTransactionToExcelController
{

    /**
     * ExportCustomersWalletTransactionToExcelController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $token = Auth::fromUser(User::find(1));
        $request->headers->set('Authorization', 'Bearer ' . $token);
    }

    public function export(Request $request)
    {
        $exportsTransactions = new ExportsCustomersWalletTransactionHistory($request);
        $wallet = Wallet::find($request->route('wallet_id'));
        $company_marking = $wallet->owner->connections->first()->invitee_reference;

        $filename = $company_marking . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.xls';
        $response = $exportsTransactions->download($filename, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }
}
