<?php

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Http\Controllers\AgentController;
use HelpGent\App\Http\Controllers\Admin\FormController;
use HelpGent\App\Http\Controllers\AnswerController;
use HelpGent\App\Http\Controllers\AttachmentController;
use HelpGent\App\Http\Controllers\FormBubbleController;
use HelpGent\App\Http\Controllers\FontController;
use HelpGent\App\Http\Controllers\MessageController;
use HelpGent\App\Http\Controllers\ChatMessageController;
use HelpGent\App\Http\Controllers\Guest\ChatMessageController as GuestChatMessageController;
use HelpGent\App\Http\Controllers\ForwardController;
use HelpGent\App\Http\Controllers\QuizController;
use HelpGent\App\Http\Controllers\ReCaptchaController;
use HelpGent\App\Http\Controllers\ResponseController;
use HelpGent\App\Http\Controllers\UserController;
use HelpGent\App\Http\Controllers\SiteVerificationController;
use HelpGent\WpMVC\Routing\Route;

include __DIR__ . '/admin.php';
include __DIR__ . '/app.php';
include __DIR__ . '/attachment.php';
include __DIR__ . '/attachment-transfer-queues.php';

Route::post( 'site-token/verify', [ SiteVerificationController::class, 'verify_token' ] );

Route::group(
    '/', function() {
        Route::get( 'fonts', [FontController::class, 'index'] );
        Route::post( 'form/media', [FormController::class, 'insert_media'] );
        Route::group(
            'response', function() {
                Route::post( 'generate-token', [ ResponseController::class, 'generate_token' ] );
                Route::post( 'register-user', [ UserController::class, 'register' ] );
                Route::get( '/', [ResponseController::class, 'index'], ['user'] );

                Route::group(
                    '{id}', function() {
                        Route::get( 'form-info', [ResponseController::class, 'get_form_info'] );
                        Route::get( 'agent', [ResponseController::class, 'get_agent'] );
                    }
                );

                Route::group(
                    'answer', function() {
                        Route::post( '/', [ ResponseController::class, 'store' ] );
                        Route::post( 'feedback', [ QuizController::class, 'feedback' ] );
                        Route::get( 'result', [ QuizController::class, 'result' ] );
                        Route::delete( 'attachment/{id}', [ AttachmentController::class, 'delete_answer' ] );
                        Route::post( 'attachment', [ AttachmentController::class, 'answer_store' ] );
                        Route::post( 'attachment/create-upload-id', [ AttachmentController::class, 'create_answer_upload_id' ] );
                        Route::post( '/single', [ ResponseController::class, 'store_single' ] );
                    }
                );
            }
        );

        Route::get( 'form-bubbles/{page_id}', [FormBubbleController::class, 'index' ] );
        Route::group(
            '/', function() {
                Route::group(
                    'message', function() {
                        Route::group(
                            'forward', function() {
                                Route::get( 'users', [ForwardController::class, 'users'] );
                                Route::get( 'responses', [ForwardController::class, 'responses'] );
                                Route::post( '/', [ForwardController::class, 'to_response'] );
                            }
                        );
                        Route::get( 'attachment', [MessageController::class, 'attachment'] );
                        Route::post( '{id}/read', [MessageController::class, 'read'] );
                        Route::post( 'bulk-read', [MessageController::class, 'bulk_read'] );
                        Route::get( 'answer', [AnswerController::class, 'index'] );
                        Route::resource( '/', MessageController::class, ['items' => ['show']] );
                    }
                );
            }, ['user']
        );

        Route::group(
            '/', function() {
                Route::group(
                    'chat-message', function() {
                        Route::get( 'unread-count', [ ChatMessageController::class, 'user_chat_unread_count' ] );
                        Route::post( 'bulk-read', [ ChatMessageController::class, 'bulk_read' ] );
                        Route::post( '{id}/restore', [ ChatMessageController::class, 'restore' ] );
                        Route::get( 'attachment', [ ChatMessageController::class, 'attachment' ] );
                        Route::resource( '/', ChatMessageController::class );
                    }
                );
            }, ['user']
        );

        Route::group(
            '/guest', function() {
                Route::group(
                    'chat-message', function() {
                        Route::get( '/', [ GuestChatMessageController::class, 'index' ] );
                        Route::post( '/', [ GuestChatMessageController::class, 'store' ] );
                        Route::get( '/unread-count', [ GuestChatMessageController::class, 'user_chat_unread_count' ] );
                    }
                );
            }, ['cors']
        );

        Route::post( 'message/forward/to-user', [ForwardController::class, 'to_user'], ['admin'] );

        Route::group(
            'user', function() {
                Route::get( 'agents', [ AgentController::class, 'index' ] );
                Route::post( 'login', [UserController::class, 'login_user'] );
                Route::post( 'token-renew', [UserController::class, 'token_renew'] );
            }
        );

        Route::post( 'recaptcha/siteverify', [ReCaptchaController::class, 'site_verify'] );
        Route::get( 'user', [UserController::class, 'get'], ['user'] );
        Route::group(
            'user', function() {
                Route::post( '/', [UserController::class, 'update'] );
                Route::post( '/logout', [UserController::class, 'logout'] );
                Route::delete( '/', [UserController::class, 'delete'] );
                Route::post( 'email', [UserController::class, 'update_email'] );
            }, ['user']
        );
    }, ['cors']
);
