<?php

use App\Http\Controllers\{ProfileController,UserController,EmailInquiryController,PhoneInquiryController};
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ManufacturerController;    
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\BrandController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProductImageController;
use App\Http\Controllers\SpecificationController;
use App\Http\Controllers\StockController;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\EnquiryController;
use App\Http\Controllers\AuditLogController;
use Spatie\Permission\Models\Role;
use App\Http\Controllers\CattleCategoryController;
use App\Http\Controllers\ExpenseCategoryController;
use App\Http\Controllers\AnimalController;
 use App\Http\Controllers\ExpenseController;
 use App\Http\Controllers\ContactController;
 use App\Http\Controllers\ArtisanCommandController;



/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/login', function () {
    // Agar user login hai, dashboard pe bhejo
    if (Auth::check()) {
        return redirect()->route('dashboard'); // dashboard route ka name
    }
    
    // Agar login nahi hai, login page dikhao
    return view('auth.login');
})->name('index');




Route::middleware('auth')->group(function () {
   Route::get('/profile', [UserController::class, 'profile'])->name('profile');

   
    Route::get('/dashboard', [UserController::class, 'dashboard'])->name('dashboard');

    
    // Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
    
});


Route::get('/', [UserController::class, 'home'])->name('home');
Route::get('/home', [UserController::class, 'home'])->name('home');

Route::get('/about', [UserController::class, 'about'])->name('about');
Route::get('/services', [UserController::class, 'services'])->name('services');
Route::get('/products', [UserController::class, 'products'])->name('products');

Route::get('/gallery', [UserController::class, 'gallery'])->name('gallery');
Route::get('/features', [UserController::class, 'features'])->name('features');
Route::get('/team', [UserController::class, 'team'])->name('team');
Route::get('/testimonial', [UserController::class, 'testimonial'])->name('testimonial');

Route::get('/contact', [UserController::class, 'contact'])->name('contact');
// Optional 404 Template Page
Route::get('/404', [UserController::class, 'error404'])->name('error404');

$roles = \Spatie\Permission\Models\Role::pluck('name')->toArray(); 


$roleMiddleware = implode('|', $roles);
Route::middleware(['auth', 'role:' . $roleMiddleware])->group(function () {
       // Change Password
    Route::get('/change-password', [UserController::class, 'change_password'])->name('change_password');
    Route::post('/change-password/update', [UserController::class, 'change_password_update'])->name('change_password_update');
    // Route::get('/dashboard', [UserController::class, 'dashboard'])->name('index');
    Route::resource('permissions',  App\Http\Controllers\PermissionController::class);
    Route::get('permissions/{permissionId}/delete', [App\Http\Controllers\PermissionController::class, 'destroy']);

    Route::resource('roles', App\Http\Controllers\RoleController::class);
    Route::get('roles/{roleId}/delete', [App\Http\Controllers\RoleController::class, 'destroy']);
    Route::get('roles/{roleId}/give-permissions', [App\Http\Controllers\RoleController::class, 'addPermissionToRole']);
    Route::put('roles/{roleId}/give-permissions', [App\Http\Controllers\RoleController::class, 'givePermissionToRole']);

    Route::resource('users', App\Http\Controllers\UserController::class);
   Route::delete('users/{userId}/delete', [UserController::class, 'destroy'])
    ->name('users.destroy');


    Route::Resource('audit-logs', AuditLogController::class);
    // Route::get('/dashboard', [AuditLogController::class, 'index'])->name('dashboard');

  

        Route::resource('cattle-categories', CattleCategoryController::class);

        Route::resource('animals', AnimalController::class);
        Route::delete('animal-images/{image}', [AnimalController::class, 'deleteImage'])
            ->name('animal-images.destroy');
        Route::resource('expense-categories', ExpenseCategoryController::class);
       

        Route::resource('expenses', ExpenseController::class);

        Route::get('/contact', [ContactController::class, 'index'])->name('contact');
        Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');
        Route::get('/contact-messages', [ContactController::class, 'adminIndex'])
                ->name('contact-messages.index');

            Route::get('/contact-messages/{contactMessage}/edit', [ContactController::class, 'edit'])
                ->name('contact-messages.edit');

            Route::get('/contact-messages/{contactMessage}', [ContactController::class, 'show'])
                ->name('contact-messages.show');

            Route::put('/contact-messages/{contactMessage}', [ContactController::class, 'update'])
                ->name('contact-messages.update');

            Route::delete('/contact-messages/{contactMessage}', [ContactController::class, 'destroy'])
                ->name('contact-messages.destroy');

                Route::get('/artisan-commands', [ArtisanCommandController::class, 'index'])
                ->name('artisan.commands');

            Route::post('/artisan-commands/run', [ArtisanCommandController::class, 'run'])
                ->name('artisan.commands.run');
    
});

   

require __DIR__.'/auth.php';
