<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Models\Booking;
use App\Models\Company;
use App\Models\Transaction;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

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

    private $request;

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

    public function headings(): array
    {
        return [
            'TransID',
            'Company ID',
            'Marking',
            'Type Of User',
            'Group Control / Experiment',
            'Payment Date',
            'Completed Purchase OrderDate',
            'DayTo Closed',
            'IsCompleted Purchase Order (1 or 0)',
            'Final Payment',
            'Discount Amount',
            'PaymentType',
            'RowUpdateDate'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {   
        return Booking::query();
    }

    /**
     * @param $booking
     * @return array
     */
    public function map($booking): array
    {
        $companyType = [];
        $companyType[companyType::COMPANY_BUSINESS] = 'COMPANY_BUSINESS';
        $companyType[companyType::PERSONAL_BUSINESS] = 'PERSONAL_BUSINESS';

        $payments = $booking->transactions()->where('type', 1)->whereIn('status', [2, 3]);
        
        $paymentmethondArray = PaymentMethodType::PAYMENT_METHODS_ID;
        $paymentmethondArray[0] = 'Hybrid';
        $paymentMethod = '';
        foreach ($payments->get() as $payment) {
            if ( $paymentMethod == '' ) { 
                $paymentMethod = $payment->payment_method;
            } else if ( $payment->payment_method != $paymentMethod ) {
                $paymentMethod = 0; // set it to Hybrid
            }
        }
        $paymentMethod === '' ? '' : $paymentMethod = $paymentmethondArray[$paymentMethod];

        $firstPayment = $payments->first();

        $CompletedPurchaseOrders = $booking->transactions()->where('type', 7)->whereIn('status', [1, 2]);

        $FirstCompletedPurchaseOrder = $CompletedPurchaseOrders->first();

        $lastPayment = $booking->transactions()->where('type', 1)->whereIn('status', [2, 3])->orderBy('id', 'desc')->first();

        return [
            $booking->id,
            $booking->company_id,
            $booking->company->reference,
            '',
//            $companyType[$booking->company->type],
            '',
            $firstPayment == null ? '' : $firstPayment->created_at,
            $FirstCompletedPurchaseOrder == null ? '' : $FirstCompletedPurchaseOrder->created_at,
            '',
            $CompletedPurchaseOrders->count() > 0 ? '1' : '0',
            $lastPayment == null ? '' : $lastPayment->amount,
            '',
            $paymentMethod,
            '',
        ];
    }
}