<?php

namespace App\Http\Controllers\Orders;


use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\WarehouseReferences;
use App\Models\Order;
use Illuminate\Http\Request;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;

class DownloadOrderQrPdfController
{

    public function download(Request $request){
        $order = Order::find($request->route('id'));
        $deliveryAddress = $order->addresses()->where('status', ApprovalStatus::APPROVED)->first();
        $warehouse= $order->orderRoles()->where('role_id', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee;

        $warehouseAddress = $warehouse->addresses()->first();
        $warehouseContacts = $warehouse->contacts;

        $warehousePrefix = '';
        $deliveryPrefix = '';
        $customerMarking = '';
        $remark = $warehouse->remarks()->first();

        if($warehouse->reference === WarehouseReferences::VT_GUANG_ZHOU || $warehouse->reference === WarehouseReferences::VT_YIWU) {
            $customerMarking = $order->companyModule->connections()->first()->invitee_reference.'/';
            if(strtolower($deliveryAddress->state->name) === 'sabah' || strtolower($deliveryAddress->state->name) === 'labuan') {
                $deliveryPrefix = 'SB/';
            }

            if(strtolower($deliveryAddress->state->name) === 'sarawak') {
                $deliveryPrefix = 'SRW/';
            }
        }

        if($warehouse->reference === WarehouseReferences::YD_YIWU){
            $warehousePrefix = 'YW/';
        }

        if(in_array($warehouse->reference,[WarehouseReferences::YD_GUANG_ZHOU, WarehouseReferences::YD_GUANG_ZHOU_2, WarehouseReferences::YD_FS_EAST_MALAYSIA, WarehouseReferences::YD_FS_WEST_MALAYSIA])){
            $warehousePrefix = 'YD/';

            if(strtolower($deliveryAddress->state->name) === 'sabah' || strtolower($deliveryAddress->state->name) === 'labuan' || in_array(strtolower($deliveryAddress->district->name), ['limbang', 'lawas'])) {
                $warehousePrefix = 'KK/';
            }

            if(strtolower($deliveryAddress->state->name) === 'sarawak' && !in_array(strtolower($deliveryAddress->district->name), ['limbang', 'lawas'])) {
                $warehousePrefix = 'KU/';
            }

            if(strtolower($deliveryAddress->state->name) === 'johor') {
                $warehousePrefix = 'NX/';
            }
        }

        $data = [
            'order' => $order,
            'marking' => $warehousePrefix.$deliveryPrefix.'CIEF/'.$customerMarking,
            'delivery_address' => $deliveryAddress,
            'warehouse_address' => $warehouseAddress,
            'warehouse_contacts' => $warehouseContacts,
            'remark' => $remark ? $remark->content : ''
        ];

        $pdf = LaravelMpdf::loadView('pdfs.qr', $data, [], [
            'title' => 'CIEF_ORDER_NO_'.$order->reference,
            'format' => 'A4-L',
            'orientation' => 'L',
            'margin_top' => '40px',
        ]);

        return $pdf->stream('CIEF_ORDER_NO_'.$order->reference.'.pdf');
    }

}