<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\PackingList;
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 Carbon\Carbon;

class ExportsPendingArrangementDeliveryList implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{

    use Exportable;

    public function headings(): array
    {
        return [
            'Container Ref.',
            'ETA',
            'Unstuffing',
            'Marking',
            'Order Number',
            'status',
            'Delay Days / delivery date',
            'Quantity',
            'State',
            'Postcode',
            'Address',
            'PIC',
            'Phone',
            'Remark'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        return PackingList::where('type', '=', 2)->whereHas('containers', function ($query){
            $query->where('containers.status', ApprovalStatus::COMPLETED)->where('containers.created_at', '>',  Carbon::now()->subMonths(3));
        });
    }

    /**
     * @param PackingList $packingList
     *
     * @return array
     */
    public function map($packingList): array
    {
        $container = $packingList->containers()->first();
        $ContainerTransport = $container->transports()->first();
        $order = $packingList->owner;
        $inviter = $order->companyModule ? $order->companyModule->inviters()->withPivot('invitee_reference')->first() : null;
        $marking = $inviter ? $inviter->pivot->invitee_reference : 'Unclaimed';
        $address = $order->addresses()->where('status', ApprovalStatus::APPROVED)->first();
        $contacts = $address->contacts->first() ? $address->contacts->first(): '';
        $originalPackingList = $packingList->owner instanceof PackingList ? $packingList->owner : $packingList;
        $transport = $originalPackingList->containers()->first()->transports()->first();
        $deliveryTransport = $originalPackingList->transports()->first();
        $schedule = $deliveryTransport ? $deliveryTransport->schedules()->first() : null;

        return [
            $container->reference,
            $ContainerTransport->schedules()->where('status', '=', ApprovalStatus::APPROVED)->first() ? $ContainerTransport->schedules()->where('status', '=', ApprovalStatus::APPROVED)->first()->eta : '',
            $ContainerTransport->drop_date,
            $marking,
            $order->reference,
            $schedule ?  (Carbon::parse($schedule->eta) < Carbon::now() ? 'Delivered' : 'Pending Delivery') :  'Pending Arrangement',
            $schedule ?  $schedule->eta : ($transport ? Carbon::now()->diffInDays($transport->drop_date, false) . ' Days' : ''),
            $packingList->packages()->sum('quantity'),
            $address->state->name,
            $address->postcode,
            $address->street_one.', '.$address->street_two.' '.$address->district->name.' '.$address->postcode.' '.$address->state->name.' '.$address->country->name,
            $address->remarks->first() ? $address->remarks->first()->content: '',
            $contacts ? $contacts ->reference : '',
            $contacts ? $contacts ->phone : '',
        ];
    }
}