<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\MessageController;
use App\Http\Controllers\PayoutRequestController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReferralController;
use App\Http\Controllers\SubscriptionController;
use App\Http\Controllers\SubscriptionJoinController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\WalletController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\AuditLogController;


/*
|--------------------------------------------------------------------------
| 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('/', function () {
    if (Auth::check()) {
        return redirect()->route('subscriptions.index');
    }

    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('/subscriptions', [SubscriptionController::class, 'index'])->name('subscriptions.index');

Route::middleware('auth')->group(function () {
    Route::get('/subscriptions/create', [SubscriptionController::class, 'create'])->name('subscriptions.create');
    Route::post('/subscriptions', [SubscriptionController::class, 'store'])->name('subscriptions.store');
    Route::get('/subscriptions/{subscription}/join', [SubscriptionJoinController::class, 'create'])->name('subscriptions.join.create');
    Route::post('/subscriptions/{subscription}/join', [SubscriptionJoinController::class, 'store'])->name('subscriptions.join.store');
    Route::post('/subscription-members/{member}/cancel', [SubscriptionJoinController::class, 'cancel'])->name('subscriptions.members.cancel');
    Route::get('/subscription-members/{member}/edit', [DashboardController::class, 'editSubscriptionMember'])->name('dashboard.subscription-members.edit');
    Route::put('/subscription-members/{member}', [DashboardController::class, 'updateSubscriptionMember'])->name('dashboard.subscription-members.update');
    Route::patch('/subscription-members/{member}/status', [DashboardController::class, 'updateSubscriptionMemberStatus'])->name('dashboard.subscription-members.update-status');
    Route::delete('/subscription-members/{member}', [DashboardController::class, 'destroySubscriptionMember'])->name('dashboard.subscription-members.destroy');

    Route::get('/my-subscriptions', [DashboardController::class, 'mySubscriptions'])->name('dashboard.my-subscriptions');
    Route::get('/my-shared-subscriptions', [DashboardController::class, 'mySharedSubscriptions'])->name('dashboard.my-shared-subscriptions');
    Route::get('/wallet', [WalletController::class, 'index'])->name('wallet.index');
    Route::get('/payout-requests', [PayoutRequestController::class, 'index'])->name('payout-requests.index');
    Route::post('/payout-requests', [PayoutRequestController::class, 'store'])->name('payout-requests.store');
    Route::get('/messages', [MessageController::class, 'index'])->name('messages.index');
    Route::post('/messages', [MessageController::class, 'store'])->name('messages.store');
    Route::get('/trust-score', [DashboardController::class, 'trustScore'])->name('trust-score.index');
    Route::get('/referrals', [ReferralController::class, 'index'])->name('referrals.index');
});
Route::get('/subscriptions/{subscription}', [SubscriptionController::class, 'show'])->name('subscriptions.show');

// Change Password
Route::middleware('auth')->group(function () {
    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::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::get('users/{userId}/delete', [App\Http\Controllers\UserController::class, 'destroy']);


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

    
});

Route::prefix('admin')
    ->as('admin.')
    ->middleware(['auth'])
    ->group(function () {
        Route::resource('categories', App\Http\Controllers\Admin\CategoryController::class);
        Route::resource('services', App\Http\Controllers\Admin\ServiceController::class);
        Route::resource('service-plans', App\Http\Controllers\Admin\ServicePlanController::class);
        Route::resource('shared-subscriptions', App\Http\Controllers\Admin\SharedSubscriptionController::class);
        Route::resource('users', App\Http\Controllers\Admin\UserController::class);
        Route::resource('wallet-transactions', App\Http\Controllers\Admin\WalletTransactionController::class)->only(['index']);
        Route::resource('payout-requests', App\Http\Controllers\Admin\PayoutRequestController::class);
        Route::resource('trust-scores', App\Http\Controllers\Admin\TrustScoreController::class)->except(['update']);
        Route::resource('referrals', App\Http\Controllers\Admin\ReferralController::class);
    });

   

require __DIR__.'/auth.php';
