<?php

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Http\Controllers\Admin\FormController;
use HelpGent\App\Http\Controllers\Admin\EntryController;
use HelpGent\App\Http\Controllers\Admin\ResponseController;
use HelpGent\App\Http\Controllers\Admin\TagController;
use HelpGent\App\Http\Controllers\Admin\SummaryController;
use HelpGent\App\Http\Controllers\QuizController;
use HelpGent\WpMVC\Routing\Route;

Route::group(
    'app', function () {
        Route::group(
            'form', function() {
                Route::post( 'import', [FormController::class, 'import_form'] );
                Route::group(
                    '{id}', function() {
                        Route::get( 'tags', [FormController::class, 'tags'] );
                        Route::get( 'export', [FormController::class, 'export'] );
                        Route::post( 'status', [FormController::class, 'update_status'] );
                        Route::post( 'rename', [FormController::class, 'update_title'] );
                        Route::post( 'duplicate', [FormController::class, 'duplicate'] );
                        Route::group(
                            'summary', function() {
                                Route::get( 'element', [SummaryController::class, 'element'] );
                                Route::get( '/', [SummaryController::class, 'index'] );
                            }
                        );
                        Route::delete( 'element/option', [FormController::class, 'delete_element_option'] );
                        Route::delete( 'element/answer', [FormController::class, 'delete_element'] );
                    }
                );
                Route::post( 'auto-save', [FormController::class, 'auto_save'] );
                Route::delete( '/', [FormController::class, 'delete_bulk_form'] );
                Route::post( 'status', [FormController::class, 'update_bulk_status'] );
                Route::get( 'select', [FormController::class, 'select'] );
                Route::resource( '/', FormController::class );
            }
        );
        Route::resource( 'tag', TagController::class, ['items' => ['show']] );
        Route::group(
            'entries', function () {
                Route::get( '/', [EntryController::class, 'index'] );
                Route::get( 'count', [EntryController::class, 'count'] );
                Route::get( 'export', [EntryController::class, 'export'] );
                Route::group(
                    'bulk', function() {
                        Route::post( 'read', [EntryController::class, 'bulk_update_read'] );
                        Route::delete( '/', [EntryController::class, 'bulk_delete'] );
                    }
                );
                Route::group(
                    '{id}', function() {
                        Route::delete( '/', [EntryController::class, 'delete'] );
                        Route::post( 'read', [EntryController::class, 'update_read'] );
                        Route::post( 'star', [EntryController::class, 'update_starred'] );
                    }
                );
            }
        );
        Route::group(
            'quiz', function () {
                Route::patch( 'points', [QuizController::class, 'update_points'] );
            }
        );
        Route::group(
            'response', function () {
                Route::get( '/', [ResponseController::class, 'index'] );
                Route::get( 'form', [ResponseController::class, 'form'] );
                Route::post( 'setup/tag', [ResponseController::class, 'setup_tag'] );
                Route::group(
                    '{id}', function() {
                        Route::get( 'user', [ResponseController::class, 'user'] );
                        Route::delete( '/', [ResponseController::class, 'delete'] );
                        Route::post( 'read', [ResponseController::class, 'update_read'] );
                        Route::post( 'archive', [ResponseController::class, 'update_archive_status'] );
                    }
                );
            } 
        );
    }, apply_filters( 'helpgent_app_apis_middleware', ['admin'] )
);
