<?php

namespace App\Http\Controllers\Exports;


use App\Classes\Modules\Exports\Services\ExportsCustomersOrderLatestDate;
use App\Classes\Modules\Exports\Services\ExportsCustomerTotalOrderByYear;
use App\Classes\Modules\Exports\Services\ExportsPaymentTransactions;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;
use App\Classes\Modules\Exports\Services\ExportsNullDebtors;

class ExportCustomersToExcelController
{
    public function export(ExportsCustomersOrderLatestDate $exportsCustomers, Request $request){
        $token = Auth::fromUser(User::find(1));
        $request->headers->set('Authorization', 'Bearer '.$token);
        return $exportsCustomers->download('customer-latest-order-date.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
    }

    public function nullDebtor(ExportsNullDebtors $exportsNullDebtors, Request $request){
        $response = $exportsNullDebtors->download('nullDebtor.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }

    public function paymentTransactions(Request $request){
        $exportsTransactions = new ExportsPaymentTransactions($request);
        $response = $exportsTransactions->download('payment-transactions.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }

    public function totalOrders(Request $request){
        $exportsTotalOrders = new ExportsCustomerTotalOrderByYear($request);
        $response = $exportsTotalOrders->download('total-orders-' . $request->route('year') . '.xls', Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
        ob_end_clean();
        return $response;
    }
}
