<?php

use Inertia\Inertia;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\V1\AuthController;
use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AdminTradeController;
use App\Http\Controllers\Admin\AdminDepositController;
use App\Http\Controllers\Admin\AdminWithdrawalController;
use App\Http\Controllers\Admin\AdminReportController;
use App\Http\Controllers\Admin\AdminCouponController;
use App\Http\Controllers\Admin\AdminSettingsController;

// Root — redirect authenticated users to /trade
Route::get('/', function () {
    if (auth()->check()) {
        return redirect('/trade');
    }
    return Inertia::render('Auth');
});

// Auth middleware redirects unauthenticated users to /login
Route::get('/login', function () {
    if (auth()->check()) {
        return redirect('/trade');
    }
    return Inertia::render('Auth');
})->name('login');

// Web auth routes (session-based)
Route::post('/login', [AuthController::class, 'webLogin']);
Route::post('/register', [AuthController::class, 'webRegister'])->name('register');
Route::post('/logout', [AuthController::class, 'webLogout'])->middleware('auth')->name('logout');

// ── Admin panel ──────────────────────────────────────────────────────────────
Route::get('/ponir/login', [AdminAuthController::class, 'showLogin'])->name('admin.login');
Route::post('/ponir/login', [AdminAuthController::class, 'login'])->name('admin.login.post');
Route::post('/ponir/logout', [AdminAuthController::class, 'logout'])->name('admin.logout')->middleware('admin.auth');

Route::prefix('/ponir')->middleware('admin.auth')->name('admin.')->group(function () {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::get('/users', [AdminUserController::class, 'index'])->name('users');
    Route::get('/users/{id}', [AdminUserController::class, 'show'])->name('users.show');
    Route::put('/users/{id}/balance', [AdminUserController::class, 'adjustBalance'])->name('users.balance');
    Route::put('/users/{id}/status', [AdminUserController::class, 'updateStatus'])->name('users.status');
    Route::get('/trades', [AdminTradeController::class, 'index'])->name('trades');
    Route::put('/trades/{uuid}/cancel', [AdminTradeController::class, 'cancel'])->name('trades.cancel');
    Route::get('/deposits', [AdminDepositController::class, 'index'])->name('deposits');
    Route::put('/deposits/{id}/approve', [AdminDepositController::class, 'approve'])->name('deposits.approve');
    Route::put('/deposits/{id}/reject', [AdminDepositController::class, 'reject'])->name('deposits.reject');
    Route::get('/withdrawals', [AdminWithdrawalController::class, 'index'])->name('withdrawals');
    Route::put('/withdrawals/{id}/approve', [AdminWithdrawalController::class, 'approve'])->name('withdrawals.approve');
    Route::put('/withdrawals/{id}/reject', [AdminWithdrawalController::class, 'reject'])->name('withdrawals.reject');
    Route::get('/reports', [AdminReportController::class, 'index'])->name('reports');
    Route::get('/settings', [AdminSettingsController::class, 'index'])->name('settings');
    Route::put('/settings', [AdminSettingsController::class, 'update'])->name('settings.update');
    Route::put('/settings/assets', [AdminSettingsController::class, 'updateAssets'])->name('settings.assets');
    Route::get('/coupons', [AdminCouponController::class, 'index'])->name('coupons');
    Route::post('/coupons', [AdminCouponController::class, 'store'])->name('coupons.store');
    Route::put('/coupons/{id}/toggle', [AdminCouponController::class, 'toggle'])->name('coupons.toggle');
    Route::delete('/coupons/{id}', [AdminCouponController::class, 'destroy'])->name('coupons.destroy');
});

// ── Protected trading app routes ─────────────────────────────────────────────
Route::middleware('auth')->group(function () {
    Route::get('/trade', function () {
        return Inertia::render('Trade');
    });

    Route::get('/portfolio', function () {
        return Inertia::render('Trade');
    });

    Route::get('/deposit', function () {
        return Inertia::render('Trade');
    });
});
