<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Transaction;
use App\Models\Wallet;
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;
use Illuminate\Http\Request;
use Carbon\Carbon;

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

    private $request;

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

    public function headings(): array
    {
        return [
            'DocNo',
            'DocDate',
            'DebtorCode',
            'DebtorName',
            'CurrencyCode',
            'ShipInfo',
            'ItemCode',
            'DetailDescription',
            'FurtherDescription',
            'Qty',
            'UnitPrice',
            'AccNo',
            'DeptNo'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        switch ($this->request->input('type', null)){
            case 'payment': $type = TransactionType::PAYMENT; break;
            case 'credit_note': $type = TransactionType::CREDIT_NOTE; break;
            case 'debit_note': $type = TransactionType::DEBIT_NOTE; break;
            default : $type = TransactionType::TOP_UP;
        }

        $start_date = $this->request->input('startDate', null);
        if ($start_date) {
            $start_date = Carbon::parse($this->request->input('startDate'))->format('Y-m-d');
        }

        $end_date = $this->request->input('endDate', null);
        if ($end_date) {
            $end_date = Carbon::parse($this->request->input('endDate'))->format('Y-m-d');
        }

        $query = Transaction::query();

        $query->where('type', $type);
        $query->where('owner_type', Wallet::class);
        $query->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);

        if($start_date && $end_date) {
            $query->whereBetween('created_at', [
                Carbon::parse($start_date)->format('Y-m-d 0:00:00'), 
                Carbon::parse($end_date)->format('Y-m-d 23:59:59')
            ]);
        }
        elseif($start_date && !$end_date) {
            $query->where('created_at', '>=',  Carbon::parse($start_date)->format('Y-m-d 0:00:00'));
        }
        elseif(!$start_date && $end_date) {
            $query->where('created_at', '<=', Carbon::parse($end_date)->format('Y-m-d 23:59:59'));
        }

        return $query;
    }

    /**
     * @param Transaction $transaction
     *
     * @return array
     */
    public function map($transaction): array
    {
        $company = $transaction->owner->owner;

        return [
            '<<New>>',
            $transaction->updated_at->format('m/d/Y H:m'),
            $company->debtor,
            '',
            'MYR',
            $transaction->bill_no,
            'W1',
            'CREDIT SALES',
            '',
            1,
            round($transaction->amount, 2),
            '500-0000',
            'WALLET'
        ];
    }
}