<?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',
            'ShipInfo',
            'AccNo',
            'DetailDescription',
            'FurtherDescription',
            'ProjNo',
            'DeptNo',
            'Qty',
            'UnitPrice'
        ];
    }

    /**
     * @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 = Transaction::query();

        $query->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::COMPLETED]);

        if($start_date && $end_date) {
            $query->whereBetween('updated_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->whereHas('transactions', function($transaction) use ($start_date) {
                $transaction->where('type', TransactionType::PAYMENT)->where('updated_at', '>=',  Carbon::parse($start_date)->format('Y-m-d 0:00:00'));
            });

        }
        elseif(!$start_date && $end_date) {
            $query->whereHas('transactions', function($transaction) use ($end_date) {
                $transaction->where('type', TransactionType::PAYMENT)->where('updated_at', '>=',  Carbon::parse($end_date)->format('Y-m-d 0:00:00'));
            });
        }

        return $query;
    }

    public function map($transaction): array
    {
        $container = $transaction->owner->containers()->first();
        $order = $transaction->owner->owner;
        $company = $order->companyModule->company;
        $shippingTransactionDetails = $transaction->transactionDetails()->where('reference', 'SHIPPING_FEE')->first();

        $furtherDescription = '';

        foreach ($transaction->transactionDetails as $item){

            if($item->reference === 'SHIPPING_FEE')
                $furtherDescription .= str_replace('X1 Freight Service Charge<br>', '', $item->name)."\n";
            elseif($item->reference === 'OVER_WEIGHT_CHARGES')
                $furtherDescription .= $item->name.' '.$item->quantity.' CBM'."\n";
            elseif($item->reference === 'MIN_CBM_CHARGES')
                $furtherDescription .= $item->name.' '.$item->quantity.' CBM'."\n";
            else
                $furtherDescription .= $item->name.' '.$item->quantity.' X '.$item->price."\n";
        }

        return [
            '<<New>>',
            $transaction->created_at->format('m/d/Y H:m'),
            $company->debtor,
            $order->reference,
            '500-0000',
            'X1 Freight Service Charge',
            $furtherDescription,
            $container->reference,
            'CIEF',
            $shippingTransactionDetails->quantity,
            $shippingTransactionDetails->price
        ];
        return [
            '<<New>>',
            $transaction->updated_at->format('m/d/Y H:m'),
            $company->debtor,
            $container->reference,
            '500-0000',
            'X1 Freight Service Charge',
            'FurtherDescription',
            $container->reference,
            'M3',
            'CIEF',
            $shippingTransactionDetails->quantity,
            $shippingTransactionDetails->price,
        ];
    }
}
