<?php

use Livewire\Livewire;
use Illuminate\Http\Request;
use UniSharp\LaravelFilemanager\Lfm;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\LogController;
use App\Http\Controllers\Site\HomeController;
use App\Http\Controllers\Admin\FormController;
use App\Http\Controllers\Admin\PageController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\PopupController;
use App\Http\Controllers\Site\MemberController;
use App\Http\Controllers\Admin\BannerController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\LanguageController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\TaxonomyController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\PermissionController;
use App\Http\Controllers\Admin\CertificateController;
use App\Http\Controllers\Admin\FileManagerController;
use App\Http\Controllers\Admin\SeoSettingsController;
use App\Http\Requests\CustomEmailVerificationRequest;
use App\Http\Controllers\Site\AuthenticationController;
use App\Http\Controllers\Admin\NavigationLinkController;
use App\Http\Controllers\Admin\NavigationMenuController;
use App\Http\Controllers\Site\Ajax\CitySelectController;
use App\Http\Controllers\Site\Ajax\TaxonomyFilterController;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use App\Http\Controllers\Site\PageController as SitePageController;
use App\Http\Controllers\Site\ProductController as SiteProductController;


//Password Reset
Route::post('/password/email', [AuthenticationController::class, 'sendResetLinkEmail'])->name('password.email');
Route::get('/password/reset', [AuthenticationController::class, 'showResetForm'])->name('password.reset');
Route::post('/password/reset', [AuthenticationController::class, 'resetPassword'])->name('password.update');

//Livewire
Livewire::setUpdateRoute(function ($handle) {
    return Route::post('/tigon-cms/public/livewire/update', $handle);
});

//Auth Routes
Route::get('/tgn-admin/login', [LoginController::class, 'index'])->name('login.index')->middleware('guest');
Route::post('/tgn-admin/login', [LoginController::class, 'login'])->name('login');
Route::get('/tgn-admin/logout', [LoginController::class, 'logout'])->name('logout');

Route::group(['prefix' => 'laravel-filemanager', 'middleware' => ['web', 'auth']], function () {
    Lfm::routes();
});

//Dashboard Routes Auth Middleware
Route::middleware(['auth'])->prefix('tgn-admin')->group(function () {
    Route::get('/import', [ProductController::class, 'import'])->name('admin.import');
    Route::get('/', [DashboardController::class, 'index'])->name('admin.dashboard.index');

    Route::middleware(['role:super-admin|admin'])->group(function () {
        //Permissions Routes
        Route::get('/permissions', [PermissionController::class, 'index'])->name('admin.permissions.index');
        Route::post('/permissions', [PermissionController::class, 'update'])->name('admin.permissions.update');
        Route::post('/role-store', [PermissionController::class, 'roleStore'])->name('admin.permissions.role.store');
        Route::get('/role-destroy/{id}', [PermissionController::class, 'roleDestroy'])->name('admin.permissions.role.destroy');
    });

    //User Routes
    Route::get('/users', [UserController::class, 'index'])->name('admin.users.index')->middleware('can:user.read');
    Route::get('/user-create', [UserController::class, 'create'])->name('admin.users.create')->middleware('can:user.create');
    Route::get('/user-edit/{user}', [UserController::class, 'edit'])->name('admin.users.edit')->middleware('can:user.update');

    //Banner Routes
    Route::get('/banners', [BannerController::class, 'index'])->name('admin.banner.index')->middleware('can:banner.read');
    Route::get('/banner-create', [BannerController::class, 'create'])->name('admin.banner.create')->middleware('can:banner.create');
    Route::get('/banner-edit/{id}', [BannerController::class, 'edit'])->name('admin.banner.edit')->middleware('can:banner.update');

    

    //Page Routes
    Route::get('/pages/{page?}', [PageController::class, 'index'])->name('admin.pages.index')->middleware('can:page.read');
    Route::get('/page-create/{page?}', [PageController::class, 'create'])->name('admin.pages.create')->middleware('can:page.create');
    Route::get('/page-edit/{id}', [PageController::class, 'edit'])->name('admin.pages.edit')->middleware('can:page.update');
    Route::post('/ckeditor-upload', [PageController::class, 'ckeditorUpload'])->name('admin.pages.ckeditor-upload');

    //Certificate Routes
    Route::get('/certificates', [CertificateController::class, 'index'])->name('admin.certificates.index');
    Route::get('/certificate-create', [CertificateController::class, 'create'])->name('admin.certificates.create');

    //Navigation Menu Routes
    Route::get('/navigation/menu/create', [NavigationMenuController::class, 'create'])->name('admin.navigation.menu.create')->middleware('can:navigation.create');
    Route::get('/navigation/menu/edit/{id}', [NavigationMenuController::class, 'edit'])->name('admin.navigation.menu.edit')->middleware('can:navigation.update');
    Route::get('/navigation/menu/{id?}', [NavigationMenuController::class, 'index'])->name('admin.navigation.menu.index')->middleware('can:navigation.read');

    //Navigation Link Routes
    Route::get('/navigation/link/{id}', [NavigationLinkController::class, 'index'])->name('admin.navigation.link.index')->middleware('can:link.read');
    Route::get('/navigation/link/{id}/create', [NavigationLinkController::class, 'create'])->name('admin.navigation.link.create')->middleware('can:link.create');
    Route::get('/navigation/link/{id}/edit/{link}', [NavigationLinkController::class, 'edit'])->name('admin.navigation.link.edit')->middleware('can:link.update');

    //Form Routes
    Route::get('/forms', [FormController::class, 'index'])->name('admin.forms.index');
    Route::get('/form-create', [FormController::class, 'create'])->name('admin.forms.create');
    Route::get('/form-edit/{form}', [FormController::class, 'edit'])->name('admin.forms.edit');

    //Popup Routes
    Route::get('popups', [PopupController::class, 'index'])->name('admin.popup.index')->middleware('can:popup.read');
    Route::get('popup-create', [PopupController::class, 'create'])->name('admin.popup.create')->middleware('can:popup.create');
    Route::get('popup-edit/{id}', [PopupController::class, 'edit'])->name('admin.popup.edit')->middleware('can:popup.update');

    //Taxonomy Routes
    Route::get('/taxonomy/{taxonomy?}', [TaxonomyController::class, 'index'])->name('admin.taxonomy.index')->middleware('can:taxonomy.read');
    Route::get('/taxonomy-create/{taxonomy?}', [TaxonomyController::class, 'create'])->name('admin.taxonomy.create')->middleware('can:taxonomy.create');
    Route::get('/taxonomy-edit/{id}', [TaxonomyController::class, 'edit'])->name('admin.taxonomy.edit')->middleware('can:taxonomy.update');

    //Languages Routes
    Route::get('/languages', [LanguageController::class, 'index'])->name('admin.languages.index')->middleware('can:language.read');
    Route::get('/language-variables/{language}', [LanguageController::class, 'variables'])->name('admin.languages.variables')->middleware('can:language.read');

    //FileManager Routes
    Route::get('/filemanager', [FileManagerController::class, 'index'])->name('admin.filemanager.index')->middleware('can:filemanager.read');

    //Settings Routes
    Route::get('/settings', [SettingsController::class, 'index'])->name('admin.settings.index')->middleware('can:settings.read');
    Route::get('/settings/seo', [SeoSettingsController::class, 'index'])->name('admin.settings.seo')->middleware('can:settings.read');
    Route::get('/settings/logs', [LogController::class, 'index'])->name('admin.settings.logs')->middleware('can:settings.read');
});

//Site Routes
Route::post('/login', [AuthenticationController::class, 'login'])->name('site.login');
Route::post('/register', [AuthenticationController::class, 'register'])->name('site.register');

Route::middleware(['md', 'md.verified'])->group(function () {
    Route::get('/member', [MemberController::class, 'index'])->name('site.member');
    
});

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', ]
    ], function(){

        Route::post('/contact_form_send',[HomeController::class,'contact_form_send'])->name('contact_form_send');
        Route::post('/career_form_send',[HomeController::class,'career_form_send'])->name('career_form_send');
        Route::get('/', [HomeController::class, 'index'])->name('index');
        Route::get('/{slug}', [SitePageController::class, 'page'])->name('page');

});
