<?php

use App\Http\Controllers\Admin\ApiLogAdminController;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\ChangelogAdminController;
use App\Http\Controllers\Admin\ClientAdminController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\LicenseAdminController;
use App\Http\Controllers\Admin\ProductAdminController;
use App\Http\Controllers\Admin\ReleaseAdminController;
use App\Http\Controllers\Admin\SetupController;
use App\Http\Controllers\HomeController;
use App\Http\Middleware\EnsureApiCenterSchema;
use App\Http\Middleware\RequireAdminSession;
use Illuminate\Support\Facades\Route;

Route::get('/', HomeController::class);

Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.store');

    Route::middleware(RequireAdminSession::class)->group(function () {
        Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
        Route::get('/setup', [SetupController::class, 'index'])->name('setup');
        Route::post('/setup/migrate', [SetupController::class, 'migrate'])->name('setup.migrate');

        Route::middleware(EnsureApiCenterSchema::class)->group(function () {
            Route::get('/', DashboardController::class)->name('dashboard');
            Route::get('/products', [ProductAdminController::class, 'index'])->name('products');
            Route::post('/products', [ProductAdminController::class, 'store'])->name('products.store');
            Route::patch('/products/{product}', [ProductAdminController::class, 'update'])->name('products.update');
            Route::delete('/products/{product}', [ProductAdminController::class, 'destroy'])->name('products.destroy');
            Route::get('/clients', [ClientAdminController::class, 'index'])->name('clients');
            Route::post('/clients', [ClientAdminController::class, 'store'])->name('clients.store');
            Route::patch('/clients/{clientAccount}', [ClientAdminController::class, 'update'])->name('clients.update');
            Route::delete('/clients/{clientAccount}', [ClientAdminController::class, 'destroy'])->name('clients.destroy');
            Route::get('/licenses', [LicenseAdminController::class, 'index'])->name('licenses');
            Route::post('/licenses', [LicenseAdminController::class, 'store'])->name('licenses.store');
            Route::patch('/licenses/{license}', [LicenseAdminController::class, 'update'])->name('licenses.update');
            Route::delete('/licenses/{license}', [LicenseAdminController::class, 'destroy'])->name('licenses.destroy');
            Route::get('/releases', [ReleaseAdminController::class, 'index'])->name('releases');
            Route::post('/releases', [ReleaseAdminController::class, 'store'])->name('releases.store');
            Route::patch('/releases/{release}', [ReleaseAdminController::class, 'update'])->name('releases.update');
            Route::delete('/releases/{release}', [ReleaseAdminController::class, 'destroy'])->name('releases.destroy');
            Route::get('/changelog', [ChangelogAdminController::class, 'index'])->name('changelog');
            Route::post('/changelog', [ChangelogAdminController::class, 'store'])->name('changelog.store');
            Route::patch('/changelog/{changelogEntry}', [ChangelogAdminController::class, 'update'])->name('changelog.update');
            Route::delete('/changelog/{changelogEntry}', [ChangelogAdminController::class, 'destroy'])->name('changelog.destroy');
            Route::get('/logs', [ApiLogAdminController::class, 'index'])->name('logs');
        });
    });
});
