<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use App\Models\Company;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ExportsCustomersWalletTransactionHistory implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $request;
    private $runningBalance = 0;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function headings(): array
    {
        return [
            'Date',
            'Description',
            'Incoming',
            'Outgoing',
            'Balance',
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $company = Company::where('reference', $this->request->route('marking'))->first();
        $transactions = $company->wallets()->first()->transactions()->whereIn('transactions.status', [2, 3])->orderBy('id');
        // dd($transactions->get()->toArray());

        return $transactions;
    }

    /**
     * @param Transaction $transaction
     *
     * @return array
     */
    public function map($transaction): array
    {
        // dd($transaction);

        $decimals = $this->request->route('is_precise') == 'true' ? 5 : 2;

        $description = '';
        switch ((int) $transaction->type) {
            case 5:
                $description = (float) $transaction->amount . ' Credit Top up';
                break;
            case 9:
                $description = 'Credit Voucher for ' . $transaction->payment_reference;
                break;
            case 1:
                $booking = Transaction::where('payment_reference', $transaction->bill_no)->first()->owner;

                if (!$booking) {
                    $description = 'Payment for unknown booking, please contact tech support.';
                    break;
                }

                $marking = $booking->marking;
                $description = 'Payment For booking refs' . $marking;
                break;
            case 11:
                $description = 'Debit Voucher for ' . $transaction->payment_reference;
                break;
        }

        $incoming = $outgoing = '';

        if (in_array($transaction->type, [TransactionType::TOP_UP, TransactionType::CREDIT_NOTE])) {
            $incoming =  number_format($transaction->amount, $decimals, '.', ',');
            $this->runningBalance += $transaction->amount;
        }

        if (in_array($transaction->type, [TransactionType::PAYMENT, TransactionType::DEBIT_NOTE])) {
            $outgoing =  number_format($transaction->amount, $decimals, '.', ',');
            $this->runningBalance -= $transaction->amount;
        }

        return [
            Carbon::parse($transaction->created_at)->format('d-m-Y h:i:s A'),
            $description,
            $incoming,
            $outgoing,
            number_format($this->runningBalance, $decimals, '.', ',')
        ];
    }
}
