<?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\Group;
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 ExportsWhiteFormTransactions implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $request;

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

    public function headings(): array
    {
        return [
            'Bank Name',
            'Bank Details',
            'Bank Acc No.',
            'Order amount',
            'Booking Reference',
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $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 = Group::query();

        // todo-new: confirm this
        // $query->where('type', ::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 $group
     *
     * @return array
     */
    public function map($group): array
    {
        $supplier = $group->issuerCompany;
        $supplerBank = $supplier->banks->first();

        $bank_name = $supplerBank->bank_name;
        $holder_name = $supplerBank->holder_name;
        $account_no = $supplerBank->account_no;

        $bookingReference = $group->transactions()->get()->pluck('owner.owner.marking')->toArray();

        return [
            $bank_name,
            $holder_name,
            $account_no,
            $group->amount,
            implode(",", $bookingReference)
        ];
    }
}
