<?php

use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminRulebookController;
use App\Http\Controllers\BrokerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

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

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'overview'])->name('dashboard');
    Route::get('/holdings', [DashboardController::class, 'holdings'])->name('holdings');
    Route::get('/pnl', [DashboardController::class, 'pnl'])->name('pnl');
    Route::get('/orders', [DashboardController::class, 'orders'])->name('orders');
    Route::get('/watchlist', [DashboardController::class, 'watchlist'])->name('watchlist');
    Route::get('/broker', [BrokerController::class, 'show'])->name('broker');
    Route::post('/broker/credentials', [BrokerController::class, 'storeCredentials'])->name('broker.credentials');
    Route::get('/broker/connect', [BrokerController::class, 'redirect'])->name('broker.connect');
    Route::get('/broker/callback', [BrokerController::class, 'callback'])->name('broker.callback');
    Route::get('/callback', [BrokerController::class, 'callback'])->name('flattrade.callback');
    Route::post('/broker/disconnect', [BrokerController::class, 'disconnect'])->name('broker.disconnect');
    Route::post('/broker/sync', [BrokerController::class, 'syncNow'])->name('broker.sync');
    Route::get('/agent', [DashboardController::class, 'agent'])->name('agent');
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications');
    Route::post('/notifications/{notification}/read', [NotificationController::class, 'markRead'])->name('notifications.read');

    Route::post('/bot/toggle', [DashboardController::class, 'toggleBot'])->name('bot.toggle');
    Route::post('/bot/start', [DashboardController::class, 'startBot'])->name('bot.start');
    Route::post('/bot/stop', [DashboardController::class, 'stopBot'])->name('bot.stop');
});

Route::middleware(['auth', 'verified', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::post('/sync', [AdminDashboardController::class, 'syncData'])->name('sync');
    Route::get('/rulebook', [AdminRulebookController::class, 'edit'])->name('rulebook.edit');
    Route::put('/rulebook', [AdminRulebookController::class, 'update'])->name('rulebook.update');
    Route::get('/logs', [AdminDashboardController::class, 'logs'])->name('logs');
});

Route::middleware('auth')->group(function () {
    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');
});

require __DIR__.'/auth.php';
