<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\KVPKey;
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 Carbon\Carbon;
use Illuminate\Support\Facades\Log;

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

    protected $startDate;
    protected $endDate;

    public function __construct($startDate = null, $endDate = null) {
        $this->startDate = $startDate ? Carbon::parse($startDate)->startOfDay() : Carbon::now()->subMonths(1);
        $this->endDate = $endDate ? Carbon::parse($endDate)->endOfDay() : Carbon::now();
    }

    public function headings(): array
    {
        return [
            'DocNo',
            'DebtorCode',
            'DocDate',
            'Description',
            'DeptNo',
            'PaymentMethod',
            'KnockOffDocType',
            'KnockOffDocNo',
            'KnockOffAmt',
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $type = TransactionType::PAYMENT;
        $query = Transaction::query();
        $query->where('owner_type', Wallet::class);
        $query->where('type', $type);
        $query->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
        $query->whereBetween('created_at', [$this->startDate, $this->endDate]);

        return $query;
    }

    /**
     * @param Transaction $transaction
     * @return array
     */
    public function map($transaction): array
    {
        $formattedDocumentDate = Carbon::parse($transaction->created_at)->format('m/d/Y');
        $owner  = $transaction->owner;
        $company = $owner->owner;

        $knockOffDocNo = '';
        $transactionKVP = $transaction->attributesKVP()->where('key', KVPKey::AUTOCOUNT_DOCNO_SALES_DEPOSIT_BY_WALLET_OFFICIAL_RECEIPT)->first();
        if($transactionKVP){
            $knockOffDocNo = $transactionKVP->value;
        }

        return [
            '<<New>>',                              //DocNo
            $company ? $company->debtor : '',       //DebtorCode
            $formattedDocumentDate,                 //DocDate
            $transaction->bill_no,                  //Description
            'C',                                    //DeptNo
            'DEPOSIT IN TRANSIT',                   //PaymentMethod
            'RP',                                   //KnockOffDocType
            $knockOffDocNo,                         //KnockOffDocNo
            number_format($transaction->amount, 2), //KnockOffAmt
        ];
    }
}
