<?php

namespace App\Classes\Modules\Exports\Services;

use App\Classes\ValueObjects\Constants\AddressType;
use App\Classes\ValueObjects\Constants\OrderType;
use App\Classes\ValueObjects\Constants\PackageType;
use App\Models\Order;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\WithTitle;

class ExportsDeliveryOrders implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize, WithTitle
{
    use Exportable;

    private $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function headings(): array
    {
        return [
            'Types_Of_Services',
            'Service_Type',
            'Sender_Name',
            'Sender_Company_Name',
            'Sender_Building_Unit',
            'Sender_Address',
            'Sender_Address_2',
            'Sender_Postal_Code',
            'Sender_Phone_Number',
            'Delivery_Name',
            'Delivery Company Name',
            'Delivery_Building_Unit',
            'Delivery_Address',
            'Delivery_Address_2',
            'Delivery_Postal_Code',
            'Delivery_Phone_Number',
            'Shipment_Type',
            'Mode',
            'Pickup_Address_Type',
            'Reference_Number',
            'Quantity',
            'Weight',
            'Length',
            'Width',
            'Height',
            'COD_Amount',
            'DO_Numbers'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        $selectedIds = array_map(function($value){
            return ['id' => $value];
        }, json_decode($this->request->input('ids')));
        
        return Order::where('type', OrderType::LAST_MILE_DELIVERY)->whereIn('id', $selectedIds);
    }

    public function title(): string
    {
        return 'bulk-shipment';
    }

    /**
     * @param Order $order
     *
     * @return array
     */
    public function map($order): array
    {
        $senderAddress = $order->addresses()->where('type', AddressType::PICK_UP)->first();
        $deliveryAddress = $order->addresses()->where('type', AddressType::DELIVERY)->first();
        $package = $order->packingLists()->first()->packages()->first();

        return [
            'Domestic Shipment',
            'ST00000020',
            $senderAddress->contacts()->first()->reference,
            $senderAddress->reference ? $senderAddress->reference : $order->companyModule->name,
            $senderAddress->street_one,
            $senderAddress->street_two.' '.$senderAddress->district->name.' '.$senderAddress->postcode.' '.$senderAddress->state->name.' '.$senderAddress->country->name,
            '',
            $senderAddress->postcode,
            $senderAddress->contacts()->first()->phone,
            $deliveryAddress->contacts()->first()->reference,
            $deliveryAddress->reference,
            $deliveryAddress->street_one,
            $deliveryAddress->street_two.' '.$deliveryAddress->district->name.' '.$deliveryAddress->postcode.' '.$deliveryAddress->state->name.' '.$deliveryAddress->country->name,
            '',
            $deliveryAddress->postcode,
            $deliveryAddress->contacts()->first()->phone,
            $package->type === PackageType::CARTON ? 'Package' : ($package->type === PackageType::PALLET ? 'Pallet' : 'Documents'),
            'Pick Up',
            'Default',
            $order->reference,
            $package->quantity,
            $package->weight * $package->quantity,
            1,
            1,
            1,
            '',
            ''
        ];
    }
}