<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\CostEstimationController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HistoryController;
use App\Http\Controllers\HostingCacheController;
use App\Http\Controllers\InspectionController;
use App\Http\Controllers\LiveSummaryController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\WorkspaceController;
use Illuminate\Support\Facades\Route;

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

Route::get('/clear-cache', [HostingCacheController::class, 'clear'])
    ->name('hosting.cache.clear');

Route::get('/locale/{locale}', function (string $locale) {
    abort_unless(in_array($locale, ['en', 'id'], true), 404);

    session(['locale' => $locale]);

    return back();
})->name('locale.switch');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.store');
    Route::get('/auth/google/redirect', [AuthController::class, 'redirectToGoogle'])->name('auth.google.redirect');
    Route::get('/auth/google/callback', [AuthController::class, 'handleGoogleCallback'])->name('auth.google.callback');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.store');
});

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');
    Route::get('/inspections', [InspectionController::class, 'index'])->name('inspections.index');
    Route::get('/inspections/scan', fn () => redirect()->route('inspections.index'));
    Route::post('/inspections/scan', [InspectionController::class, 'scan'])->name('inspections.scan');
    Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
    Route::get('/reports/{report}', [ReportController::class, 'show'])->name('reports.show');
    Route::get('/cost-estimation', [CostEstimationController::class, 'index'])->name('costs.index');
    Route::get('/history', [HistoryController::class, 'index'])->name('history.index');
    Route::get('/workspaces', [WorkspaceController::class, 'index'])->name('workspaces.index');
    Route::get('/live/summary', LiveSummaryController::class)->name('live.summary');
    Route::post('/workspaces', [WorkspaceController::class, 'store'])->name('workspaces.store');
    Route::put('/workspaces/{workspace}', [WorkspaceController::class, 'update'])->name('workspaces.update');
    Route::delete('/workspaces/{workspace}', [WorkspaceController::class, 'destroy'])->name('workspaces.destroy');
    Route::post('/collections/move', [WorkspaceController::class, 'moveCollection'])->name('collections.move');
    Route::delete('/collections/{collection}', [WorkspaceController::class, 'destroyCollection'])->name('collections.destroy');

    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
});
