<?php

use App\Classes\Jobs\FetchContainersStatusUpdateFromVTPortalJob;
use App\Classes\Jobs\FetchDeliveryListFromVTPortalJob;
use App\Classes\Jobs\FetchLoadedContainersFromVTPortalJob;
use App\Classes\Jobs\FetchPackingListFromVTPortalJob;
use App\Classes\Jobs\FetchWarehouseReceiveListFromVTPortalJob;
use App\Models\CompanyConnection;
use Illuminate\Support\Facades\Crypt;
use App\Models\Container;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

require __DIR__ . '/template.php';

Route::get('', function () {
    return view('pages.accounts.login');
})->name('login');

Route::group(['prefix' => 'swagger'], function () {
    Route::get('/', function () {
        return view('swagger.index');
    });
    Route::get('/json', function () {
        return view('swagger.json');
    });
});

Route::get('/business/claim/{hash}', function ($hash) {
    return view('pages.accounts.claim_business', ['id' => Crypt::decryptString($hash)]);
})->name('company.claim');

Route::get('/account/email/verification/{token}', function ($token) {
    return view('pages.accounts.email_verified', ['token' => $token]);
})->name('account.email.verification');

Route::get('/account/password/reset/{token}', function ($token) {
    return view('pages.accounts.reset_password', ['token' => $token]);
})->name('account.password.reset');

Route::get('/dashboard', function () {
    return view('pages.dashboard');
})->name('dashboard');

Route::get('/customers', function () {
    return view('pages.admin.customers');
})->name('customers');

Route::get('/orders', function () {
    return view('pages.orders.index');
})->name('orders');

Route::get('/order/show/{order_number}', function ($orderNumber) {
    return view('pages.orders.profile', ['id' => $orderNumber]);
})->name('order.show');

Route::get('/address', function () {
    return view('pages.addresses.index');
})->name('address');

Route::get('/warehouse-list', function () {
    return view('pages.warehouse_list');
})->name('warehouse.list');

Route::get('/containers', function () {
    return view('pages.containers');
})->name('containers');

Route::get('/container/show/{reference}', function ($reference) {
    $container = Container::where('reference', $reference)->first();
    return view('pages.templates.warehouseFlow', ['id' => $container->id]);
})->name('container.show');

Route::get('/customer/{marking}', function ($marking) {
    $connection = CompanyConnection::where('invitee_reference', $marking)->first();
    $id = $connection->invitee->company->id;
    return view('pages.customers.profile', ['id' => $id]);
})->name('customer.profile');

Route::get('/orders/refresh', function(){

    FetchWarehouseReceiveListFromVTPortalJob::withChain([
        new FetchLoadedContainersFromVTPortalJob,
        new FetchPackingListFromVTPortalJob,
        new FetchContainersStatusUpdateFromVTPortalJob,
        new FetchDeliveryListFromVTPortalJob
    ])->dispatch();

    return redirect('orders');

})->name('orders.refresh');

Route::get('/containers/refresh', function(){

//    FetchLoadedContainersFromVTPortalJob::dispatch();
    FetchContainersStatusUpdateFromVTPortalJob::dispatch();

    return redirect('orders');

})->name('containers.refresh');

Route::get('/deliveries/refresh', function(){

    FetchDeliveryListFromVTPortalJob::dispatch();

    return redirect('orders');

})->name('deliveries.refresh');

Route::get('/order/{id}/download', 'Orders\DownloadOrderQrPdfController@download')->name('order.qr.download');

Route::get('/report/customclearance', 'Reports\CustomcClearanceReportController@download')->name('report.customclearance');

Route::get('/report/customclearance/{orderid}', 'Reports\CustomcClearanceReportController@download')->name('report.customclearance');

Route::group(['prefix' => 'template', 'as' => 'template.'], function () {
    Route::get('/payments-and-billing', function () {
        return view('pages.templates.paymentsBilling');
    })->name('payments-and-billing');

    Route::get('/shipping-queue', function () {
        return view('pages.templates.shippingQueue');
    })->name('shipping-queue');
});

Route::get('/yiwu', function () {
    $orders = \App\Models\Order::whereHas('OrderRoles', function($query){
        return $query->where('role_id', \App\Classes\ValueObjects\Constants\OrderRoleTypes::ORIGIN_WAREHOUSE)->where('company_module_id', 4);
    })->whereHas('addresses', function($query){
        return $query->where('status', \App\Classes\ValueObjects\Constants\ApprovalStatus::APPROVED)->whereIn('state_id', [5, 13, 14]);
    })->get();

    foreach ($orders as $order){
        echo '<a target="_blank" href="'.route('order.show', $order->reference).'">'. $order->reference.' - '.$order->created_at.'</a><br>';
    }

});

Route::get('/guangzhou', function () {
    $orders = \App\Models\Order::whereHas('OrderRoles', function($query){
        return $query->where('role_id', \App\Classes\ValueObjects\Constants\OrderRoleTypes::ORIGIN_WAREHOUSE)->where('company_module_id', 3);
    })->whereHas('addresses', function($query){
        return $query->where('status', \App\Classes\ValueObjects\Constants\ApprovalStatus::APPROVED)->whereIn('state_id', [5, 13, 14]);
    })->get();

    foreach ($orders as $order){
        echo '<a target="_blank" href="'.route('order.show', $order->reference).'">'. $order->reference.' - '.$order->created_at.'</a><br>';
    }

});

Route::get('/debug', function (){
    $orders = \App\Models\Order::all();

    $issues = [];
    foreach ($orders as $order){
        $received = $order->parcels()->warehouseReceiveList()->sum('quantity');
        $shipping = $order->parcels()->shippingList()->sum('quantity');
        if( $received < $shipping){
            $issues[] = [
                'order' => $order->reference,
                'received' => $received,
                'shipping' => $shipping
            ];
        }
    }

    dd($issues);
});
