<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CityController;
use App\Http\Controllers\ContinentController;
use App\Http\Controllers\CountryController;
use App\Http\Controllers\LeadController;
use App\Http\Controllers\LocationLookupController;
use App\Http\Controllers\TradefairController;
use App\Http\Controllers\UserManagementController;
use App\Http\Controllers\VisibilityManagerController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\DepartmentCrudController;
use App\Http\Controllers\VisibilityModuleController;
use App\Http\Controllers\HomeController;

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

Route::middleware('auth')->group(function () {
    Route::get('/home', [HomeController::class, 'redirect'])->name('home');
    Route::get('/welcome', [HomeController::class, 'noAccess'])->name('crm.home');
});

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.submit');
});

Route::middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    Route::middleware('module:leads.dashboard')->get('/dashboard', [LeadController::class, 'dashboard'])->name('leads.dashboard');
    Route::middleware('module:leads.listing')->get('/leads/listing', [LeadController::class, 'listing'])->name('leads.listing');
    Route::middleware('module:leads.my_tasks')->get('/leads/my-tasks', [LeadController::class, 'myTasks'])->name('leads.my-tasks');
    Route::middleware('module:leads.design_tasks|leads.listing|leads.dashboard')->get('/leads/design-tasks', [LeadController::class, 'designTasks'])->name('leads.design-tasks');
    Route::middleware('module:leads.design_tasks|leads.listing|leads.dashboard')->patch('/leads/{lead}/design-task', [LeadController::class, 'updateDesignTask'])->whereNumber('lead')->name('leads.design-task.update');
    Route::middleware('module:leads.assign')->post('/leads/{lead}/assign-marketing', [LeadController::class, 'assignToMarketing'])->whereNumber('lead')->name('leads.assign-marketing');
    Route::middleware('module:leads.assign.design')->post('/leads/{lead}/assign-design', [LeadController::class, 'assignToDesign'])->whereNumber('lead')->name('leads.assign-design');

    Route::middleware('module:leads.edit|leads.edit.assigned')->group(function () {
        Route::get('/leads/{lead}/edit', [LeadController::class, 'edit'])->whereNumber('lead')->name('leads.edit');
        Route::put('/leads/{lead}', [LeadController::class, 'update'])->whereNumber('lead')->name('leads.update');
    });

    Route::middleware('module:leads.view|leads.my_tasks|leads.design_tasks')->group(function () {
        Route::get('/leads/{lead}', [LeadController::class, 'show'])->whereNumber('lead')->name('leads.show');
    });

    Route::middleware('module:leads.status')->patch('/leads/{lead}/status', [LeadController::class, 'updateStatus'])->whereNumber('lead')->name('leads.status.update');
    Route::middleware('module:leads.remarks')->post('/leads/{lead}/remarks', [LeadController::class, 'storeRemark'])->whereNumber('lead')->name('leads.remarks.store');

    Route::middleware('module:leads.create')->group(function () {
        Route::get('/leads/create', [LeadController::class, 'create'])->name('leads.create');
        Route::post('/leads', [LeadController::class, 'store'])->name('leads.store');
    });

    Route::middleware('module:leads.delete')->delete('/leads/{lead}', [LeadController::class, 'destroy'])->whereNumber('lead')->name('leads.destroy');

    Route::middleware('module:vendor_quotes.manage|leads.my_tasks')->group(function () {
        Route::post('/leads/{lead}/vendor-quotes', [LeadController::class, 'storeVendorQuote'])->whereNumber('lead')->name('vendor-quotes.store');
        Route::patch('/leads/{lead}/vendor-quotes/{vendorQuote}', [LeadController::class, 'updateVendorQuote'])->whereNumber('lead')->name('vendor-quotes.update');
    });

    Route::middleware('module:users.manage')->group(function () {
        Route::get('/users', [UserManagementController::class, 'index'])->name('users.index');
        Route::post('/users', [UserManagementController::class, 'store'])->name('users.store');
        Route::post('/users/departments/quick', [UserManagementController::class, 'storeDepartmentQuick'])->name('users.departments.quick');
        Route::get('/users/{user}/edit', [UserManagementController::class, 'edit'])->whereNumber('user')->name('users.edit');
        Route::put('/users/{user}', [UserManagementController::class, 'update'])->whereNumber('user')->name('users.update');
        Route::delete('/users/{user}', [UserManagementController::class, 'destroy'])->whereNumber('user')->name('users.destroy');
    });

    Route::middleware('module:masters.roles')->group(function () {
        Route::get('/masters/roles', [RoleController::class, 'index'])->name('roles.index');
        Route::post('/masters/roles', [RoleController::class, 'store'])->name('roles.store');
        Route::get('/masters/roles/{role}/edit', [RoleController::class, 'edit'])->name('roles.edit');
        Route::put('/masters/roles/{role}', [RoleController::class, 'update'])->name('roles.update');
        Route::delete('/masters/roles/{role}', [RoleController::class, 'destroy'])->name('roles.destroy');
    });

    Route::middleware('module:masters.departments')->group(function () {
        Route::get('/masters/departments', [DepartmentCrudController::class, 'index'])->name('departments.master.index');
        Route::post('/masters/departments', [DepartmentCrudController::class, 'store'])->name('departments.master.store');
        Route::get('/masters/departments/{department}/edit', [DepartmentCrudController::class, 'edit'])->name('departments.master.edit');
        Route::put('/masters/departments/{department}', [DepartmentCrudController::class, 'update'])->name('departments.master.update');
        Route::delete('/masters/departments/{department}', [DepartmentCrudController::class, 'destroy'])->name('departments.master.destroy');
    });

    Route::middleware('module:geography.continents')->group(function () {
        Route::get('/continents', [ContinentController::class, 'index'])->name('continents.index');
        Route::post('/continents', [ContinentController::class, 'store'])->name('continents.store');
        Route::get('/continents/{continent}/edit', [ContinentController::class, 'edit'])->name('continents.edit');
        Route::put('/continents/{continent}', [ContinentController::class, 'update'])->name('continents.update');
        Route::delete('/continents/{continent}', [ContinentController::class, 'destroy'])->name('continents.destroy');
    });

    Route::middleware('module:geography.countries')->group(function () {
        Route::get('/countries', [CountryController::class, 'index'])->name('countries.index');
        Route::post('/countries', [CountryController::class, 'store'])->name('countries.store');
        Route::get('/countries/{country}/edit', [CountryController::class, 'edit'])->name('countries.edit');
        Route::put('/countries/{country}', [CountryController::class, 'update'])->name('countries.update');
        Route::delete('/countries/{country}', [CountryController::class, 'destroy'])->name('countries.destroy');
    });

    Route::middleware('module:geography.cities')->group(function () {
        Route::get('/cities', [CityController::class, 'index'])->name('cities.index');
        Route::post('/cities', [CityController::class, 'store'])->name('cities.store');
        Route::get('/cities/{city}/edit', [CityController::class, 'edit'])->name('cities.edit');
        Route::put('/cities/{city}', [CityController::class, 'update'])->name('cities.update');
        Route::delete('/cities/{city}', [CityController::class, 'destroy'])->name('cities.destroy');
    });

    Route::middleware('module:tradefairs.manage')->group(function () {
        Route::get('/tradefairs', [TradefairController::class, 'index'])->name('tradefairs.index');
        Route::get('/tradefairs/create', [TradefairController::class, 'create'])->name('tradefairs.create');
        Route::post('/tradefairs', [TradefairController::class, 'store'])->name('tradefairs.store');
        Route::get('/tradefairs/{tradefair}/edit', [TradefairController::class, 'edit'])->name('tradefairs.edit');
        Route::put('/tradefairs/{tradefair}', [TradefairController::class, 'update'])->name('tradefairs.update');
        Route::delete('/tradefairs/{tradefair}', [TradefairController::class, 'destroy'])->name('tradefairs.destroy');
    });

    Route::middleware('module:geography.continents|tradefairs.manage|leads.create')->get('/locations/countries-by-continent', [LocationLookupController::class, 'countriesByContinent'])->name('locations.countries-by-continent');
    Route::middleware('module:geography.cities|tradefairs.manage|leads.create')->get('/locations/cities-by-country', [LocationLookupController::class, 'citiesByCountry'])->name('locations.cities-by-country');

    Route::middleware('role:admin')->prefix('admin')->group(function () {
        Route::name('visibility.')->group(function () {
            Route::get('/visibility', [VisibilityManagerController::class, 'index'])->name('index');
            Route::post('/visibility/departments', [VisibilityManagerController::class, 'storeDepartment'])->name('departments.store');
            Route::put('/visibility/departments/{department}/permissions', [VisibilityManagerController::class, 'updateDepartmentPermissions'])->name('departments.permissions');
            Route::put('/visibility/users/{user}/overrides', [VisibilityManagerController::class, 'updateUserOverrides'])->name('users.overrides');
        });

        Route::get('/masters/modules', [VisibilityModuleController::class, 'index'])->name('visibility-modules.index');
        Route::post('/masters/modules', [VisibilityModuleController::class, 'store'])->name('visibility-modules.store');
        Route::get('/masters/modules/{visibilityModule}/edit', [VisibilityModuleController::class, 'edit'])->name('visibility-modules.edit');
        Route::put('/masters/modules/{visibilityModule}', [VisibilityModuleController::class, 'update'])->name('visibility-modules.update');
        Route::delete('/masters/modules/{visibilityModule}', [VisibilityModuleController::class, 'destroy'])->name('visibility-modules.destroy');
    });
});
