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

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

    use Exportable;

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

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        return PackingList::where('type', PackingListType::SHIPPING_PACKING_LIST)->whereRaw('LENGTH(reference) > 8')->where('reference', 'not like', "%YW%")->where('owner_type', Order::class)->whereDate('created_at', '>=', Carbon::parse('20-09-2022'))->whereDoesntHave('containers');
    }

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

        $address = $packingList->owner->addresses()->where('status', ApprovalStatus::APPROVED)->first();
        return [
            $packingList->reference,
            $address->postcode
        ];
    }
}
