<?php

namespace App\Http\Controllers\Exports;


use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use Illuminate\Support\Facades\Storage;
use App\Classes\General\AWSS3Helper;
use App\Classes\Modules\Exports\Services\ExportsSalesInvoiceReport;
use App\Classes\Modules\Exports\Services\ExportsSalesInvoiceWithRefundReport;
use App\Classes\Modules\Exports\Services\ExportsARCreditNoteReport;
use App\Classes\Modules\Exports\Services\ExportsCompanies;
use App\Classes\Modules\Exports\Services\ExportsReceivePaymentDepositEntryReport;
use App\Classes\Modules\Exports\Services\ExportsReceivePaymentForBookingReport;
use App\Classes\Modules\Exports\Services\ExportsSalesDepositByWallet1PaymentEntry;
use App\Classes\Modules\Exports\Services\ExportsSalesDepositByWallet2RefundEntry;
use App\Classes\Modules\Exports\Services\ExportsSalesDepositByWallet3DepositEntry;
use App\Classes\Modules\Exports\Services\ExportsWalletTopUpDepositEntryReport;
use Carbon\Carbon;

class ExportController
{
    public function salesInvoices(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsSalesInvoiceReport($startDate, $endDate);
        return $this->handleExport($exporter, 'Exchange - Sales Invoice Report.xls');
    }

    public function salesInvoicesWithRefund(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsSalesInvoiceWithRefundReport($startDate, $endDate);
        return $this->handleExport($exporter, 'Exchange - Sales Invoice Report (with refund).xls');
    }

    public function companies(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsCompanies($startDate, $endDate);
        return $this->handleExport($exporter, 'Exchange - Customers Data Report.xls');
    }

    public function arCreditNote(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsARCreditNoteReport($startDate, $endDate);
        return $this->handleExport($exporter, 'Exchange - AR Credit Note Report.xls');
    }

    public function receivePaymentDepositEntry(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsReceivePaymentDepositEntryReport($startDate, $endDate);
        return $this->handleExport($exporter, '01D - Sales Deposit Received [AR DEPOSIT ENTRY].xls');
    }

    public function receivePaymentDepositForBooking(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsReceivePaymentForBookingReport($startDate, $endDate);
        return $this->handleExport($exporter, '01R - RECEIVE PAYMENT [AR RECEIVE PAYMENT].xls');
    }

    public function walletTopUpDepositEntry(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsWalletTopUpDepositEntryReport($startDate, $endDate);
        return $this->handleExport($exporter, 'WALLET TOP UP REPORT [Wallet Deposit Received].xls');
    }

    public function salesDepositByWalletPaymentEntry(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsSalesDepositByWallet1PaymentEntry($startDate, $endDate);
        return $this->handleExport($exporter, '01DRW - Sales Deposit by Wallet [AR PAYMENT ENTRY].xls');
    }

    public function salesDepositByWalletRefundEntry(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsSalesDepositByWallet2RefundEntry($startDate, $endDate);
        return $this->handleExport($exporter, '01DRF - Sales Deposit by Wallet [AR REFUND ENTRY].xls');
    }

    public function salesDepositByWalletDepositEntry(Request $request){
        [$startDate, $endDate] = $this->getValidatedDates($request);
        $exporter = new ExportsSalesDepositByWallet3DepositEntry($startDate, $endDate);
        return $this->handleExport($exporter, '01DD - Sales Deposit by Wallet [AR DEPOSIT ENTRY].xls');
    }

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

        $startDate = isset($validated['startDate']) && $validated['startDate']
            ? Carbon::createFromFormat('d-m-Y', $validated['startDate'])->startOfDay()
            : Carbon::now()->subMonth()->startOfDay();

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

        return [$startDate, $endDate];
    }

    private function handleExport($exporter, string $exportFileName)
    {
        $filesystemDriver = Storage::getDefaultDriver();

        if ($filesystemDriver === 's3') {
            return response([
                'src' => AWSS3Helper::S3Exportable($exportFileName, $exporter)
            ]);
        }

        $response = $exporter->download(
            $exportFileName,
            Excel::XLS,
            ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
        );

        ob_end_clean(); // prevent corrupt download in some environments
        return $response;
    }
}
