<?php

use Illuminate\Support\Facades\Route;
use Maestro\Core\Http\Controllers\AuthController;
use Maestro\Core\Http\Controllers\RoleController;
use Maestro\Core\Http\Controllers\UserController;
use Maestro\Core\Http\Controllers\DataTableController;
use Maestro\Core\Http\Controllers\DataTableExportController;
use Maestro\Core\Http\Controllers\NavigationController;
use Maestro\Core\Http\Controllers\TableStateController;
use Maestro\Core\Http\Controllers\OrganizationSettingController;
use Maestro\Core\Http\Controllers\ActivityLogController;

// Public
Route::post('auth/login', [AuthController::class, 'login'])
    ->middleware('throttle:10,1');

// Protected
Route::middleware(['auth:sanctum', 'fingerprint', 'absolute_timeout', 'active_user'])
    ->group(function () {

        // Auth
        Route::post('auth/logout', [AuthController::class, 'logout']);
        Route::get('auth/me', [AuthController::class, 'me']);

        // Navigation
        Route::get('navigation', [NavigationController::class, 'index']);

        // Table state
        Route::get('table-states/{tableKey}', [TableStateController::class, 'show']);
        Route::put('table-states/{tableKey}', [TableStateController::class, 'update']);
        Route::delete('table-states/{tableKey}', [TableStateController::class, 'destroy']);

        // DataTables
        Route::get('datatables/{resourceKey}/columns', [DataTableController::class, 'columns']);
        Route::post('datatables/{resourceKey}', [DataTableController::class, 'fetch']);
        Route::post('datatables/{resourceKey}/export', [DataTableController::class, 'export']);
        Route::get('datatables/exports', [DataTableExportController::class, 'index']);
        Route::post('datatables/exports/clear-ready', [DataTableExportController::class, 'clearReady']);
        Route::get('datatables/exports/{id}', [DataTableExportController::class, 'show'])
            ->name('datatables.exports.show');
        Route::get('datatables/exports/{id}/download', [DataTableExportController::class, 'download'])
            ->name('datatables.exports.download');

        // Users
        Route::apiResource('users', UserController::class)
            ->middleware('permission:users.view');

        // Roles & permissions
        Route::get('roles', [RoleController::class, 'index'])
            ->middleware('permission:roles.view');
        Route::post('roles', [RoleController::class, 'store'])
            ->middleware('permission:roles.create');
        Route::put('roles/{role}', [RoleController::class, 'update'])
            ->middleware('permission:roles.edit');
        Route::delete('roles/{role}', [RoleController::class, 'destroy'])
            ->middleware('permission:roles.delete');
        Route::get('permissions', [RoleController::class, 'permissions'])
            ->middleware('permission:roles.view');

        // Settings
        Route::get('settings/organization', [OrganizationSettingController::class, 'show'])
            ->middleware('permission:settings.organization.view');
        Route::put('settings/organization', [OrganizationSettingController::class, 'update'])
            ->middleware('permission:settings.organization.edit');

        // Activity log
        Route::get('settings/activity-log', [ActivityLogController::class, 'index'])
            ->middleware('permission:activitylog.view');
    });
