<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\PublicController;
use App\Http\Controllers\AdminController;
use App\Models\Category;
use Illuminate\Support\Facades\Route;

Route::get('/', [PublicController::class, 'home'])->name('home');
Route::get('/platform/{platform}', [PublicController::class, 'platform'])->name('platform.show');
Route::get('/category/{category:slug}', [PublicController::class, 'category'])->name('category.show');
Route::get('/download/{id}', [PublicController::class, 'download'])->name('download');

// Mobile clients: same version + download URLs as the storefront (see PublicController::download).
Route::get('/api/app-update', [PublicController::class, 'appUpdate'])->name('api.app_update');

Route::get('/admin/login', [AuthController::class, 'showLogin'])->name('admin.login');
Route::post('/admin/login', [AuthController::class, 'login'])->name('admin.login.submit');
Route::post('/admin/logout', [AuthController::class, 'logout'])->name('admin.logout');

Route::middleware(['admin'])->prefix('admin')->group(function () {
    Route::get('/', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('/change-password', [AuthController::class, 'showChangePassword'])->name('admin.change_password');
    Route::post('/change-password', [AuthController::class, 'changePassword'])->name('admin.change_password.submit');
    Route::get('/applications', [AdminController::class, 'applications'])->name('admin.applications.index');
    Route::get('/applications/create', [AdminController::class, 'createApplication'])->name('admin.applications.create');
    // Outside /applications/{id} so "upload-chunk" is never treated as an application ID.
    Route::post('/upload-chunk', [AdminController::class, 'uploadChunk'])->name('admin.applications.uploadChunk');
    Route::post('/applications', [AdminController::class, 'storeApplication'])->name('admin.applications.store');
    Route::get('/applications/{id}/edit', [AdminController::class, 'editApplication'])->name('admin.applications.edit')->whereNumber('id');
    Route::post('/applications/{id}', [AdminController::class, 'updateApplication'])->name('admin.applications.update')->whereNumber('id');
    Route::get('/applications/{id}/delete', function () {
        return redirect()->route('admin.applications.index');
    })->whereNumber('id');
    Route::post('/applications/{id}/delete', [AdminController::class, 'destroyApplication'])->name('admin.applications.destroy')->whereNumber('id');

    Route::get('/categories', [AdminController::class, 'categories'])->name('admin.categories.index');
    Route::post('/categories', [AdminController::class, 'storeCategory'])->name('admin.categories.store');
    Route::post('/categories/{category}/delete', [AdminController::class, 'destroyCategory'])->name('admin.categories.destroy');
});
