<?php

use App\Http\Controllers\AreaController;
use App\Http\Controllers\ContractController;
use App\Http\Controllers\MafOrderController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProductSKUController;
use App\Http\Controllers\ResponsibleController;
use App\Http\Controllers\UserController;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('home');
});

Auth::routes(['register' => false, 'reset' => false, 'verify' => false, 'confirm' => false]);

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::middleware('auth:web')->group(function () {
    // set current year for display
    Route::get('set-year', function (Request $request) {
        if ($request->has('year')) {
            session(['year' => $request->year]);
        }

        return redirect()->route('order.index');
    })->name('set-year');

    // admin routes
    Route::middleware('role:' . Role::ADMIN)->prefix('admin')->group(function (){
        Route::prefix('users')->group(function (){
            Route::get('', [UserController::class, 'index'])->name('user.index');
            Route::get('create', [UserController::class, 'create'])->name('user.create');
            Route::get('{user}', [UserController::class, 'show'])->name('user.show');
            Route::post('', [UserController::class, 'store'])->name('user.store');
            Route::put('{user}', [UserController::class, 'update'])->name('user.update');
            Route::delete('{user}', [UserController::class, 'destroy'])->name('user.destroy');
        });
        Route::prefix('responsibles')->group(function (){
            Route::get('', [ResponsibleController::class, 'index'])->name('responsible.index');
            Route::get('{responsible}', [ResponsibleController::class, 'show'])->name('responsible.show');
            Route::post('store', [ResponsibleController::class, 'store'])->name('responsible.store');
            Route::post('update/{responsible}', [ResponsibleController::class, 'update'])->name('responsible.update');
            Route::delete('delete/{responsible}', [ResponsibleController::class, 'destroy'])->name('responsible.destroy');
        });
    });

    // profile
    Route::get('profile', [UserController::class, 'profile'])->name('user.profile');
    Route::post('profile/store', [UserController::class, 'storeProfile'])->name('profile.store');

    // catalog
    Route::get('catalog', [ProductController::class, 'index'])->name('catalog.index');
    Route::get('catalog/create', [ProductController::class, 'create'])->name('catalog.create');
    Route::get('catalog/{product}', [ProductController::class, 'show'])->name('catalog.show');
    Route::post('catalog', [ProductController::class, 'store'])->name('catalog.store');
    Route::post('catalog/{product}', [ProductController::class, 'update'])->name('catalog.update');
    Route::delete('catalog/{product}', [ProductController::class, 'delete'])->name('catalog.delete');

    Route::post('catalog-import', [ProductController::class, 'import'])->name('catalog.import');
    Route::post('catalog-export', [ProductController::class, 'export'])->name('catalog.export');

    // contracts
    Route::get('contract', [ContractController::class, 'index'])->name('contract.index');
    Route::get('contract/create', [ContractController::class, 'create'])->name('contract.create');
    Route::get('contract/{contract}', [ContractController::class, 'show'])->name('contract.show');
    Route::post('contract', [ContractController::class, 'store'])->name('contract.store');
    Route::post('contract/{contract}', [ContractController::class, 'update'])->name('contract.update');
    Route::delete('contract/{contract}', [ContractController::class, 'delete'])->name('contract.delete');

    // orders
    Route::get('order', [OrderController::class, 'index'])->name('order.index');
    Route::get('order/create', [OrderController::class, 'create'])->name('order.create');
    Route::get('order/{order}', [OrderController::class, 'show'])->name('order.show');
    Route::get('order/edit/{order}', [OrderController::class, 'edit'])->name('order.edit');
    Route::post('order/store', [OrderController::class, 'store'])->name('order.store');
    Route::post('order/{order}/update', [OrderController::class, 'store'])->name('order.update');
    Route::delete('order/{order}', [OrderController::class, 'destroy'])->name('order.destroy')->middleware('role:' . Role::ADMIN);

    Route::get('order/{order}/get-maf', [OrderController::class, 'getMafToOrder'])->name('order.get-maf');
    Route::get('order/revert-maf/{order}', [OrderController::class, 'revertMaf'])->name('order.revert-maf');
    Route::post('order/move-maf', [OrderController::class, 'moveMaf'])->name('order.move-maf');

    Route::post('order/{order}/upload-photo', [OrderController::class, 'uploadPhoto'])->name('order.upload-photo');
    Route::post('order/{order}/upload-document', [OrderController::class, 'uploadDocument'])->name('order.upload-document');
    Route::post('order/{order}/upload-statement', [OrderController::class, 'uploadStatement'])->name('order.upload-statement');

    Route::middleware('role:' . Role::ADMIN)->group(function (){
        Route::delete('order/delete-photo/{order}/{file}', [OrderController::class, 'deletePhoto'])->name('order.delete-photo');
        Route::delete('order/delete-document/{order}/{file}', [OrderController::class, 'deleteDocument'])->name('order.delete-document');
        Route::delete('order/delete-statement/{order}/{file}', [OrderController::class, 'deleteStatement'])->name('order.delete-statement');
    });

    // Склад (МАФ)
    Route::get('product_sku', [ProductSKUController::class, 'index'])->name('product_sku.index');
    Route::get('product_sku/{product_sku}', [ProductSKUController::class, 'show'])->name('product_sku.show');
    Route::post('product_sku/update/{product_sku}', [ProductSKUController::class, 'update'])->name('product_sku.update');

    // Склад заказы МАФ
    Route::get('maf_orders', [MafOrderController::class, 'index'])->name('maf_order.index');
    Route::get('maf_orders/{maf_order}', [MafOrderController::class, 'show'])->name('maf_order.show');
    Route::post('maf_orders/store', [MafOrderController::class, 'store'])->name('maf_order.store');
    Route::post('maf_orders/update/{maf_order}', [MafOrderController::class, 'update'])->name('maf_order.update');
    Route::delete('maf_orders/delete/{maf_order}', [MafOrderController::class, 'destroy'])->name('maf_order.delete');
    Route::post('maf_orders/set_in_stock/{maf_order}', [MafOrderController::class, 'setInStock'])->name('maf_order.set_in_stock');

    // ajax search products
    Route::get('product/search', [ProductController::class, 'search'])->name('product.search');

    // ajax search products
    Route::get('order-search', [OrderController::class, 'search'])->name('order.search');

    // ajax get areas by district
    Route::get('areas/{district_id?}', [AreaController::class, 'ajaxGetAreasByDistrict'])->name('area.ajax-get-areas-by-district');



});