<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\OwnerReportsController;
use App\Http\Controllers\OwnerDashboardController;
use App\Http\Controllers\DashboardExportController;
use App\Http\Controllers\IntegrationStatusController;
use App\Http\Controllers\OwnerAccessGuideController;
use App\Http\Controllers\PortalSettingsController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

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

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'show'])->name('login');
    Route::post('/login', [LoginController::class, 'login'])->middleware('throttle:login');
});

Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

    Route::get('/dashboard', OwnerDashboardController::class)->name('owner.dashboard');

    Route::get('/laporan', [OwnerReportsController::class, 'index'])->name('owner.reports');
    Route::get('/laporan/laba-rugi', [OwnerReportsController::class, 'labaRugi'])->name('owner.reports.pl');
    Route::get('/laporan/harian', [OwnerReportsController::class, 'harian'])->name('owner.reports.daily');
    Route::get('/laporan/item-fnb', [OwnerReportsController::class, 'items'])->name('owner.reports.items');
    Route::get('/laporan/layanan', [OwnerReportsController::class, 'services'])->name('owner.reports.services');
    Route::get('/laporan/sumber', [OwnerReportsController::class, 'sources'])->name('owner.reports.sources');
    Route::get('/panduan-akses', OwnerAccessGuideController::class)->name('owner.access-guide');

    Route::get('/pengaturan', [PortalSettingsController::class, 'edit'])->name('owner.settings');
    Route::post('/pengaturan', [PortalSettingsController::class, 'update'])->name('owner.settings.update');

    Route::get('/laporan/integration-status', IntegrationStatusController::class)
        ->middleware('throttle:30,1')
        ->name('owner.reports.integration-status');

    Route::get('/laporan/export.csv', DashboardExportController::class)
        ->name('owner.reports.export');

    /* Legacy URLs (backward compatibility) */
    Route::get('/dashboard/integration-status', function (Request $request) {
        return redirect()->route('owner.reports.integration-status', $request->query());
    });
    Route::get('/dashboard/export.csv', function (Request $request) {
        return redirect()->route('owner.reports.export', $request->query());
    });
});
