<?php
/**
 * Created by PhpStorm.
 * User: Omair Saleh
 * Date: 20/9/2022
 * Time: 12:20 AM
 */

namespace App\Classes\Modules\Exports\Sheets;

use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackageType;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Models\Order;
use App\Models\PackingList;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;



class PackingListsSheet implements WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize, FromCollection, WithTitle
{

    use Exportable;

    public function headings(): array
    {
        return [
            'Arrival Date',
            'Marking',
            'Order No.',
            'Reference',
            'Quantity',
            'CBM',
            'Overweight',
            'Total CBM',
            'Container No.',
            'Delivery Date',
            'Warehouse',
            'Warehouse District',
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function collection()
    {
        $packingLists = PackingList::where('status', '=', 2)->whereIn('type', [PackingListType::WAREHOUSE_RECEIVE_LIST, PackingListType::SHIPPING_PACKING_LIST])->get();

        return $packingLists->filter(function ($packingList) {

            if($packingList->type === PackingListType::SHIPPING_PACKING_LIST){
                $warehouseArrival = PackingList::where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->where('reference', $packingList->reference)->get();
                return $warehouseArrival ? false : true;
            }

            return true;
        });
    }

    /**
     * @param $packingList
     * @return array
     */
    public function map($packingList): array
    {

        $warehouseArrivalList = $packingList;
        $shippingList = PackingList::where('type', PackingListType::SHIPPING_PACKING_LIST)->where('reference', $packingList->reference)->first();

        if($packingList->type === PackingListType::SHIPPING_PACKING_LIST) {
            $warehouseArrivalList = null;
            $shippingList = $packingList;

        }

        $arrivalTransport = null;

        if($warehouseArrivalList){
            $arrivalTransport = $warehouseArrivalList->transports()->first();
        }

        $order = null;
        if($packingList->owner instanceof Order){
            $order = $packingList->owner;
        }

        $cbm = $packingList->packages->where('type', '!=', PackageType::OVER_WEIGHT)->sum(function($package) {
            return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity);
        });

        $overWeight = $packingList->packages->where('type', PackageType::OVER_WEIGHT)->sum(function($package) {
            return ($package->width / 100) * ($package->height / 100) *($package->length / 100) * ($package->quantity);
        });

        $container = $shippingList ? $shippingList->container : null;

        $delivery = $shippingList ? $shippingList->transport : null;

        return [
            $arrivalTransport ? $arrivalTransport->drop_date : null,
            $order ? $order->reference : 'Unclaimed',
            $order ? $order->companyModule->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference : 'Unclaimed',
            $packingList->reference,
            $packingList->packages()->sum('quantity'),
            $cbm,
            $overWeight ? $overWeight : 0,
            $cbm + $overWeight,
            $container ? $container->reference : null,
            $delivery ? $delivery->drop_date : null,
            strlen($packingList->reference) > 8 ? 'YD' : 'VT',
            $order ? $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee->name : null,
        ];

    }

    /**
     * @return string
     */
    public function title(): string
    {
        return 'Packing Lists';
    }
}