<?php

namespace App\Classes\Modules\Exports\Services;

use App\Models\CompanyConnection;
use App\Models\Container;
use App\Models\Order;
use App\Models\Package;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Carbon\Carbon;

class ExportsOrderSummaryByMarking implements FromCollection, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{

    use Exportable;

    protected $customerMarking;
    protected $dateFrom;
    protected $dateTo;

    public function headings(): array
    {
        return [
            'Date',
            'Container',
            'Order Marking',
            'Packing List Reference',
            'Description',
            'Quantity',
            'L (cm)',
            'H (cm)',
            'W (cm)',
            'CBM'
        ];
    }
    
    public function setParameters($customerMarking, $dateFrom, $dateTo)
    {
        $this->customerMarking = $customerMarking;
        $this->dateFrom = $dateFrom;
        $this->dateTo = $dateTo;
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function collection()
    {
        $connection = CompanyConnection::where('invitee_reference', $this->customerMarking)->first();

        $company_id = $connection->invitee->company->id;
        $company_module_id = $connection->invitee->id;
        $packagesArray = collect();
        $orders = Order::where('company_module_id', $company_module_id);

        if (isset($this->dateFrom) && isset($this->dateTo)) {
            $from = Carbon::createFromFormat('d-m-Y', $this->dateFrom);
            $to = Carbon::createFromFormat('d-m-Y', $this->dateTo);
            $orders = $orders->whereBetween('created_at', [$from, $to]);
        }

        $orders = $orders->get();

        foreach ($orders as $order){
            $packingLists = $order->packingLists;
            foreach ($packingLists as $packingList){
                if($packingList->packingLists->first()){
                    $packingList = $packingList->packingLists->first();
                }
                $packages = $packingList->packages;
                foreach ($packages as $package){
                    $packagesArray->push($package);
                }
            }
        }

        return $packagesArray;
    }

    /**
     * @param Package $package
     *
     * @return array
     */
    public function map($package): array
    {
        $packinglist = ($package->packingList()->first()->owner->owner_type == Order::class) ? $package->packingList()->first(): $package->packingList->owner;
        $order = $packinglist->owner;

        return [
            Carbon::parse($package->created_at)->format('d-m-Y'),
            '',
            $order->reference,
            $package->packingList->reference,
            $package->description,
            $package->quantity,
            $package->length,
            $package->height,
            $package->width,
            ((($package->length / 100) * ($package->height / 100) * ($package->width / 100)) *  $package->quantity)
        ];
    }
}