<?php

namespace App\Http\Controllers\Exports;


use App\Classes\Modules\Exports\Services\ExportCurrencyVendorOrder;
use App\Classes\Modules\Exports\Services\ExportsCustomers;
use App\Classes\Modules\Exports\Services\ExportsTransactions;
use App\Classes\Modules\Exports\Services\ExportsV2Transactions;
use App\Classes\Modules\Exports\Services\ExportsBookingTransactions;
use App\Classes\Modules\Exports\Services\ExportsLeadsTransactions;
use App\Classes\Modules\Exports\Services\ExportsNullDebtors;
use App\Classes\Modules\Exports\Services\ExportsPaymentTransactions;
use App\Classes\Modules\Exports\Services\ExportsWalletTransactions;
use App\Classes\Modules\Exports\Services\ExportsInvoiceTransactions;
use App\Classes\Modules\Exports\Services\ExportsReceiptTransactions;
use App\Classes\Modules\Exports\Services\ExportsImportedReceiptMappeds;
use App\Classes\Modules\Exports\Services\ExportsWhiteFormTransactions;
use App\Classes\Modules\Exports\Services\ExportsImportedInvoiceMappeds;
use App\Classes\Modules\Exports\Services\ExportsEInvoiceDebtorSummary;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;
use App\Models\TransactionMappingLog;
use Illuminate\Support\Facades\Storage;
use App\Classes\General\AWSS3Helper;
use App\Classes\Modules\Exports\Services\ExportsAllCustomersInfoForLarkSystem;
use Illuminate\Support\Carbon;

class ExportCustomersToExcelController
{

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

    public function export(ExportsCustomers $exportsCustomers, Request $request){
        $exportFileName = 'customers.csv';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsCustomers) ]);
        }
        else{
            return $exportsCustomers->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
        }
    }
    public function exportCurrencyVendorOrder(ExportCurrencyVendorOrder $exportCurrencyVendorOrder, Request $request){
        $exportCurrencyVendorOrder = new ExportCurrencyVendorOrder($request);
        $exportFileName = 'CurrencyVendorOrder.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportCurrencyVendorOrder) ]);
        }
        else{
            $response = $exportCurrencyVendorOrder->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function transactions(ExportsTransactions $exportsTransactions, Request $request){
        $exportFileName = 'transactions.csv';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
        }
        else{
            return $exportsTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
        }
    }

    public function v2transactions(Request $request){

        $validated = $request->validate([
            'startDate' => 'nullable|date_format:d-m-Y',
            'endDate' => 'nullable|date_format:d-m-Y|after_or_equal:startDate',
        ]);

        $startDate = null;
        $endDate = null;

        if (isset($validated['startDate']) && $validated['startDate']) {
            $startDate = Carbon::createFromFormat('d-m-Y', $validated['startDate'])->startOfDay();
        } else {
            $startDate = Carbon::now()->subMonths(1)->startOfDay();
        }

        if (isset($validated['endDate']) && $validated['endDate']) {
            $endDate = Carbon::createFromFormat('d-m-Y', $validated['endDate'])->endOfDay();
        } else {
            $endDate = Carbon::now()->endOfDay();
        }

        $exportsTransactions = new ExportsV2Transactions($startDate, $endDate);
        $exportFileName = 'transactions.csv';
        $filesystemDriver = Storage::getDefaultDriver();

        if ($filesystemDriver === 's3') {
            return response(['src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions)]);
        } else {
            return $exportsTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']);
        }
    }

    public function nullDebtor(ExportsNullDebtors $exportsNullDebtors, Request $request){
        $exportFileName = 'nullDebtor.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsNullDebtors) ]);
        }
        else{
            $response = $exportsNullDebtors->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

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

    public function whiteFormTransactions(Request $request){
        $exportsTransactions = new ExportsWhiteFormTransactions($request);
        $exportFileName = 'white-form-transactions.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
        }
        else{
            $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function walletTransactions(Request $request)
    {
        $exportsTransactions = new ExportsWalletTransactions($request);
    
        $type = $request->get('type');
        switch ($type) {
            case 'payment':
                $exportFileName = 'wallet-payment-transactions.xls';
                break;
            case 'credit_note':
                $exportFileName = 'wallet-credit-note-transactions.xls';
                break;
            case 'debit_note':
                $exportFileName = 'wallet-debit-note-transactions.xls';
                break;
            default:
                $exportFileName = 'wallet-topup-transactions.xls';
        }
    
        $filesystemDriver = Storage::getDefaultDriver();
        if ($filesystemDriver === 's3') {
            return response(['src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions)]);
        } else {
            $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }
    
    public function invoiceTransactions(Request $request){
        $exportsTransactions = new ExportsInvoiceTransactions($request);
        $exportFileName = 'invoice-transactions.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
        }
        else{
            $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function receiptTransactions(Request $request){
        $exportsTransactions = new ExportsReceiptTransactions($request);
        $exportFileName = 'receipt-transactions.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]);
        }
        else{
            $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function bookingTransactions(ExportsBookingTransactions $exportsBookingTransactions, Request $request){
        $exportFileName = 'bookingTransactions.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsBookingTransactions) ]);
        }
        else{
            $response = $exportsBookingTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function leadsData(ExportsLeadsTransactions $exportsLeadsTransactions, Request $request){
        $exportFileName = 'leadsData.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsLeadsTransactions) ]);
        }
        else{
            $response = $exportsLeadsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function importedInvoiceMapped(ExportsImportedInvoiceMappeds $exportsImportedInvoiceMappeds, Request $request) {
        $exportFileName = $request->input('fileName').'.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsImportedInvoiceMappeds) ]);
        }
        else{
            $response = $exportsImportedInvoiceMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function importedReceiptMapped(ExportsImportedReceiptMappeds $exportsImportedReceiptMappeds, Request $request) {
        $exportFileName = $request->input('fileName').'.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsImportedReceiptMappeds) ]);
        }
        else{
            $response = $exportsImportedReceiptMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function exportAllCustomersInfoForLarkSystem(Request $request)
    {
        $password = $request->input('password');
        if ($password !== 'all_customers_data') {
            return response()->json(['error' => 'Invalid password'], 403);
        }

        $exportsAllCustomersInfoForLarkSystem = new ExportsAllCustomersInfoForLarkSystem($request);
        $exportFileName = 'exchange_all_customers_info_for_lark_system.xls';

        $filesystemDriver = Storage::getDefaultDriver();
        if ($filesystemDriver === 's3') {
            return response(['src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsAllCustomersInfoForLarkSystem)]);
        } else {
            $response = $exportsAllCustomersInfoForLarkSystem->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }

    public function eInvoiceDebtorSummary(ExportsEInvoiceDebtorSummary $exportsEInvoiceDebtorSummary, Request $request){
        $exportFileName = 'EINV_DEBTOR_SUMMARY.xls';
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            return response([ 'src' =>  AWSS3Helper::S3Exportable($exportFileName, $exportsEInvoiceDebtorSummary) ]);
        }
        else{
            $response = $exportsEInvoiceDebtorSummary->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
            ob_end_clean();
            return $response;
        }
    }
}
