<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Models\Order;
use App\Models\State;
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 ExportsWarehousePackingList implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{

    use Exportable;

    public function headings(): array
    {
        return [
            'Customer Marking',
            'Order Number'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $johor_state_id = State::where('name', 'johor')->first()->id;

        return Order::whereHas('addresses', function($address) use ($johor_state_id){
            $address->where('status', ApprovalStatus::APPROVED)->where('state_id', $johor_state_id);
        })->whereHas('packingLists', function($packingLists) {
            $packingLists->where('type', PackingListType::SHIPPING_PACKING_LIST)->whereDoesntHave('transports');
        });
    }

    /**
     * @param Order $order
     *
     * @return array
     */
    public function map($order): array
    {
        $marking = $order->companyModule->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference;

        return [
            $marking,
            $order->reference,
        ];
    }
}