<?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 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 ExportsPaymentTransactions 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',
            'Qty',
            'UnitPrice',
            'AccNo',
            'DeptNo'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $start_date = $this->request->input('start_date', null);
        if ($start_date) {
            $start_date = Carbon::parse($this->request->input('start_date'))->format('Y-m-d');
        }

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

        $query = Transaction::query();

        $query->where('type', TransactionType::PAYMENT)->where('payment_method', '!=', PaymentMethodType::WALLET);
        $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 Company $transaction
     *
     * @return array
     */
    public function map($transaction): array
    {
        $booking = $transaction->owner()->first();
        $company = $booking->company()->first();

        return [
            '<<New>>',
            $transaction->updated_at->format('d/m/Y'),
            $company->debtor,
            '',
            'MYR',
            $booking->marking,
            '',
            'PLEASE REFER TO THE ATTACHED APPENDIX REF ' . $booking->marking,
            1,
            $transaction->amount,
            '500-0000',
            'CIEF'
        ];
    }
}