<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function () {

    // Account Routes
    Route::group(['prefix' => 'account', 'as' => 'account.', 'namespace' => 'Account'], function () {
       Route::get('/list', 'ListUserController@list')->name('list');
       Route::post('/create', 'RegisterAccountController@register')->name('create');
       Route::post('/update/{id}', 'UpdateUserController@update')->name('update');

        Route::group(['prefix' => 'invite', 'as' => 'invite.'], function () {
            Route::post('/invite', 'InviteUserController@invite')->name('create');
            Route::get('/list/active', 'ListUserController@list')->name('list');
        });

    });

    // Companies Routes
    Route::group(['prefix' => 'company', 'as' => 'company.', 'namespace' => 'Companies'], function () {

        // List companies
        Route::get('/list/{pagination?}', 'ListCompanyController@list')->name('list');

        // search companies
        Route::get('/search', 'SearchCompanyController@search')->name('search');

        Route::post('/marking','CompanyMarkingController@find')->name('marking');

        // List single company
        Route::get('{id}/profile', 'ProfileCompanyController@show')->name('profile');

        // Create new company
        Route::post('/', 'CreateCompanyController@create')->name('create');

        // Update company
        Route::put('{id}', 'UpdateCompanyController@update')->name('update');

        // Delete company
        Route::delete('{id}', 'DeleteCompanyController@destroy')->name('delete');

        Route::get('{id}/order/{category}/list', 'CompanyOrderController@list')
            ->where('category', 'processing|shipping|receiving|complete|cancel')->name('order.list');

        // Companies Address Book Routes
        Route::group(['prefix' => 'address-book', 'as' => 'addressBook.', 'namespace' => 'AddressBook'], function () {

            // List addresses in company's address book
            Route::get('{companyId}', 'ListAddressBookController@list')->name('list');

            // Create new address in company's address book
            Route::post('{companyId}', 'CreateAddressBookController@store')->name('create');

            // Update address in company's address book
            Route::put('{companyId}/{id}', 'UpdateAddressBookController@update')->name('update');

            // sets default delivery address
            Route::put('{companyId}/{id}/default', 'DefaultAddressBookController@default')->name('default');

            // Delete address from company's address book
            Route::delete('{companyId}/{id}', 'DeleteAddressBookController@destroy')->name('delete');

        });
    });

    // Contact Routes
    Route::group(['prefix' => 'contact', 'as' => 'contact.', 'namespace' => 'Contacts'], function () {

        // List contacts
        Route::get('list/{type}/{reference}', 'ListContactController@list')->name('list');

        // Create new contact
        Route::post('{type}/{reference}', 'CreateContactController@create')->name('create');

        // Update contact
        Route::put('{type}/{reference}/{id}', 'UpdateContactController@update')->name('update');

        // Delete contact
        Route::delete('{id}', 'DeleteContactController@destroy')->name('delete');

    });

    // Warehouses Routes
    Route::group(['prefix' => 'warehouse', 'as' => 'warehouse.', 'namespace' => 'Warehouses'], function () {

        // List warehouses
        Route::get('/list/', 'ListWarehouseController@list')->name('list');

        // List single warehouses
        Route::get('{id}/profile','ProfileWarehouseController@show')->name('profile');

        // Create new warehouses
        Route::post('/','CreateWarehouseController@create')->name('create');

        // Update warehouses
        Route::put('{id}', 'UpdateWarehouseController@update')->name('update');

        // Delete warehouse
        Route::delete('{id}', 'DeleteWarehouseController@destroy')->name('delete');

        Route::get('{id}/order/{category}/list', 'WarehouseOrderController@list')
            ->where('category', 'receiving|shipping|complete')->name('order.list');

    });

    Route::group(['prefix' => 'order', 'as' => 'order.', 'namespace' => 'Orders'], function () {

        Route::get('{id}/profile', 'OrderController@show')->name('profile');

        Route::post('/{companyId}/create', 'CreateOrderController@create')->name('create');
        Route::delete('/{orderId}/delete', 'OrderController@delete')->name('delete');

        Route::post('/{orderId}/warehouse', 'UpdateWarehouseController@update')->name('warehouse.update');

        Route::post('/{orderId}/update', 'UpdateOrderStepController@update')->name('step.update');

        Route::get('/list/{category}', 'ListOrderController@list')->name('list');

        Route::post('/search','SearchOrderController@search')->name('search');

        Route::post('/bulk-receive-notes', 'BulkReceiveNotesController@create')->name('bulk.receive.update');

        Route::group(['prefix' => 'attachment', 'as' => 'attachment.', 'namespace' => 'Attachments'], function () {
            Route::post('/{orderId}/packing-list', 'UpdatePackingListController@update')->name('packingList.update');
            Route::post('/{orderId}/packages', 'UpdatePackagesController@update')->name('packages.update');
            Route::post('/{orderId}/tracking-number', 'UpdateTrackingNumberController@update')->name('tracking.update');

            Route::post('/{orderId}/shipping-arrangement', 'UpdateShippingArrangementController@update')->name('shipping.update');
            Route::post('/{orderId}/delivery-arrangement', 'UpdateDeliveryArrangementController@update')->name('delivery.update');
        });

        Route::post('/{orderId}/address','UpdateOrderAddressController@update')->name('address.order.update');

        Route::post('/{orderId}/update-receive-note', 'UpdateReceiveNoteController@update')->name('receive.update');

        Route::post('/{scheduleId}/update-order-schedule','UpdateOrderScheduleController@update')->name('schedule.update');

        Route::delete('/{scheduleId}/delete-order-schedule','DeleteOrderScheduleController@delete')->name('schedule.delete');
    });

    // Service Routes
    Route::group(['prefix' => 'service', 'as' => 'service.'], function () {
        // List countries
        Route::get('list/countries', 'Services\CountriesListController@index')->name('list.countries');
    });


    Route::group(['prefix' => 'report', 'as' => 'report.', 'namespace' => 'Dashboard'], function () {
        Route::get('/order', 'OrdersReportController@index')->name('order');
        Route::get('/cbm', 'CbmReportController@index')->name('cbm');
        Route::get('/customer', 'CustomerReportController@index')->name('customer');
        Route::get('/order/status', 'OrdersStatusReportController@index')->name('order.status');
    });

    // Freight Forwarder Routes
    Route::group(['prefix' => 'forwarder', 'as' => 'forwarder.', 'namespace' => 'FreightForwarder'], function () {

        // Update Freight Forwarder Settings
        Route::put('/{forwarderId}/update', 'UpdateFreightForwarderController@update')->name('update.freight.forwarder');
        Route::put('{companyId}/update/customer-relation', 'UpdateCustomerRelationController@update')->name('update.customer.relation');
        Route::put('{forwarderId}/update/services', 'UpdateServiceFeesController@update')->name('update.service.fees');
        Route::delete('/{serviceId}/delete/services', 'DeleteServiceFeesController@delete')->name('delete.service.fees');

    });

});