<?php

use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\DocumentationCategoryController;
use App\Http\Controllers\Admin\DocumentationController;
use App\Http\Controllers\Admin\IntegrationController;
use App\Http\Controllers\Admin\IntegrationLogController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\ConsumableController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InstallerStatsController;
use App\Http\Controllers\InstallRequestController;
use App\Http\Controllers\RequestController;
use App\Http\Controllers\Settings\CityController;
use App\Http\Controllers\Settings\ObjectModelController;
use App\Http\Controllers\Settings\PriorityController;
use App\Http\Controllers\Settings\RequestTypeController;
use App\Http\Controllers\Settings\SystemTypeController;
use App\Http\Controllers\UserController;
use App\Models\Setting;
use App\Services\LicenseService;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::get('/', fn() => Inertia::render('Welcome'))->name('home');

Route::middleware(['auth', 'verified', 'license'])
    ->scopeBindings()
    ->group(function () {
        Route::get('/license', function () {
            $key = Setting::get('license_key');
            $status = app(LicenseService::class)->cached();

            return inertia('license/Index', [
                'license' => [
                    'valid' => $status['valid'],
                    'key' => Setting::get('license_key'),
                    'expires_at' => $status['expires_at'] ?? null,
                    'message' => $status['message'] ?? null,
                ],
            ]);
        });

        Route::post('/license', function (\Illuminate\Http\Request $request) {
            $request->validate([
                'license_key' => 'required|string',
            ]);

            Setting::set('license_key', $request->license_key);

            cache()->forget('license_status');

            return back();
        });

        Route::get('/dashboard', [DashboardController::class, 'index'])
            ->name('dashboard');

        /**
         * Install Requests
         */
        Route::prefix('requests')->name('requests.')->group(function () {

            Route::get('/', [InstallRequestController::class, 'index'])->name('index');
            Route::get('/create', [InstallRequestController::class, 'create'])->name('create');
            Route::post('/', [InstallRequestController::class, 'store'])->name('store');

            Route::get('/{installRequest}/edit', [InstallRequestController::class, 'edit'])->name('edit');
            Route::put('/{installRequest}', [InstallRequestController::class, 'update'])->name('update');
            Route::delete('/{installRequest}', [InstallRequestController::class, 'destroy'])->name('destroy');

            Route::post('/{installRequest}/take', [InstallRequestController::class, 'take'])->name('take');
            Route::put('/{installRequest}/assign', [InstallRequestController::class, 'assignInstaller'])->name('assign');

            Route::post('/{installRequest}/reject', [InstallRequestController::class, 'reject'])->name('reject');
            Route::post('/{installRequest}/complete', [InstallRequestController::class, 'complete'])->name('complete');

            Route::get('/{installRequest}/rate', [InstallRequestController::class, 'rateForm'])->name('rate.form');
            Route::post('/{installRequest}/rate', [InstallRequestController::class, 'rate'])->name('rate');

            Route::post('/{installRequest}/sync-1c', [InstallRequestController::class, 'syncWithOneC'])
                ->name('sync');

            Route::get('/export', [RequestController::class, 'export'])->name('export');

            Route::get('/{installRequest}/download-media', [RequestController::class, 'downloadMedia'])
                ->name('media.download');
        });

        /**
         * Installers / Analytics
         */
        Route::prefix('installers')->name('installers.')->group(function () {
            Route::get('/stats', [InstallerStatsController::class, 'index'])->name('stats');
            Route::get('/{user}', [InstallerStatsController::class, 'show'])->name('show');
        });

        Route::get('/analytics/overview', [InstallerStatsController::class, 'analytics'])
            ->name('analytics.overview');

        /**
         * Consumables
         */
        Route::resource('consumables', ConsumableController::class);

        Route::post('consumables/{consumable}/adjust', [ConsumableController::class, 'adjustStock'])
            ->name('consumables.adjust');

        Route::post('consumables/{installer}/refill', [ConsumableController::class, 'refill'])
            ->name('consumables.refill');

        /**
         * Users
         */
        Route::resource('users', UserController::class);

        /**
         * Chat
         */
        Route::prefix('chat')->name('chat.')->group(function () {
            Route::get('/', [ChatController::class, 'index'])->name('index');
            Route::get('/users', [ChatController::class, 'users'])->name('users');
            Route::get('/{user}', [ChatController::class, 'messages'])->name('messages');
            Route::post('/', [ChatController::class, 'store'])->name('store');

            Route::post('/typing', [ChatController::class, 'typing']);
            Route::get('/typing/{user}', [ChatController::class, 'typingStatus']);
        });

        /**
         * Integration
         */
        Route::prefix('integration')->name('integration.')->group(function () {
            Route::get('/', [IntegrationController::class, 'edit'])->name('edit');
            Route::post('/', [IntegrationController::class, 'update'])->name('update');

            Route::get('/logs', [IntegrationLogController::class, 'index'])->name('logs');
            Route::post('/logs/{log}/retry', [IntegrationLogController::class, 'retry'])->name('retry');
        });

        /**
         * Documentation
         */
        Route::resource('documentation', DocumentationController::class);
        Route::resource('documentation-categories', DocumentationCategoryController::class);
        /**
         * Log
         */
        Route::resource('/activity-logs', ActivityLogController::class);
        /**
         * Settings
         */
        Route::prefix('settings')->name('settings.')->group(function () {
            Route::resource('cities', CityController::class);
            Route::resource('objects', ObjectModelController::class);
            Route::resource('types', RequestTypeController::class);
            Route::resource('priorities', PriorityController::class);
            Route::resource('types-systems', SystemTypeController::class);
        });
});

require __DIR__ . '/settings.php';
require __DIR__ . '/auth.php';
