<?php

use App\Http\Controllers\AppearanceSettingsController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\CallbackRequestCompletionController;
use App\Http\Controllers\CallbackRequestController;
use App\Http\Controllers\ChannelIntegrationController;
use App\Http\Controllers\ChatWidgetController;
use App\Http\Controllers\ConversationAssignmentController;
use App\Http\Controllers\ConversationController;
use App\Http\Controllers\ConversationMessageController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\KnowledgeBaseController;
use App\Http\Controllers\TeamMemberController;
use App\Http\Controllers\UnknownQuestionController;
use App\Http\Controllers\UnknownQuestionResolutionController;
use App\Http\Controllers\WidgetScriptController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::get('/health', function () {
    return response()->json([
        'data' => [
            'service' => 'vejalnai-chatbot',
            'status' => 'ok',
        ],
    ]);
});

Route::get('/ready', function () {
    try {
        DB::select('select 1');
    } catch (Throwable) {
        return response()->json([
            'data' => [
                'service' => 'vejalnai-chatbot',
                'status' => 'unavailable',
                'database' => 'down',
            ],
        ], 503);
    }

    return response()->json([
        'data' => [
            'service' => 'vejalnai-chatbot',
            'status' => 'ready',
            'database' => 'ok',
        ],
    ]);
});

Route::get('/widget.js', WidgetScriptController::class)->name('widget.script');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store'])->name('login.store');
});

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');
    Route::get('/conversations', [ConversationController::class, 'index'])->name('conversations.index');
    Route::get('/conversations-sidebar', [ConversationController::class, 'sidebar'])->name('conversations.sidebar');
    Route::post('/conversations/bulk', [ConversationController::class, 'bulkAction'])->name('conversations.bulk');
    Route::get('/conversations/{conversation}', [ConversationController::class, 'show'])->name('conversations.show');
    Route::put('/conversations/{conversation}/label', [ConversationController::class, 'updateLabel'])->name('conversations.label.update');
    Route::put('/conversations/{conversation}/archive', [ConversationController::class, 'archive'])->name('conversations.archive');
    Route::put('/conversations/{conversation}/unarchive', [ConversationController::class, 'unarchive'])->name('conversations.unarchive');
    Route::delete('/conversations/{conversation}', [ConversationController::class, 'destroy'])->name('conversations.destroy');
    Route::put('/conversations/{id}/restore', [ConversationController::class, 'restore'])->name('conversations.restore');
    Route::delete('/conversations/{id}/force', [ConversationController::class, 'forceDestroy'])->name('conversations.forceDestroy');
    Route::post('/conversations/{conversation}/messages', [ConversationMessageController::class, 'store'])->name('conversations.messages.store');
    Route::get('/conversations/{conversation}/messages', [ConversationMessageController::class, 'index'])->name('conversations.messages.index');
    Route::put('/conversations/{conversation}/assignment', [ConversationAssignmentController::class, 'update'])->name('conversations.assignment.update');
    Route::get('/unknown-questions', UnknownQuestionController::class)->name('unknown-questions.index');
    Route::post('/unknown-questions/{unknownQuestion}/resolve', [UnknownQuestionResolutionController::class, 'store'])->name('unknown-questions.resolve');
    Route::get('/callback-requests', CallbackRequestController::class)->name('callback-requests.index');
    Route::post('/callback-requests/{callbackRequest}/complete', [CallbackRequestCompletionController::class, 'store'])->name('callback-requests.complete');

    Route::resource('/knowledge-base', KnowledgeBaseController::class);
    Route::put('/knowledge-base/{intent}/status', [KnowledgeBaseController::class, 'updateStatus'])->name('knowledge-base.status.update');
    Route::post('/knowledge-base/import', [KnowledgeBaseController::class, 'import'])->name('knowledge-base.import');
    Route::post('/knowledge-base/{intent}/phrases', [KnowledgeBaseController::class, 'storePhrase'])->name('knowledge-base.phrases.store');
    Route::delete('/knowledge-base/phrases/{phrase}', [KnowledgeBaseController::class, 'destroyPhrase'])->name('knowledge-base.phrases.destroy');
    Route::post('/reports/corrections/{correction}/approve', [\App\Http\Controllers\ReportedCorrectionResolutionController::class, 'approve'])->name('reports.corrections.approve');
    Route::post('/reports/corrections/{correction}/reject', [\App\Http\Controllers\ReportedCorrectionResolutionController::class, 'reject'])->name('reports.corrections.reject');
    Route::get('/teams', [TeamMemberController::class, 'index'])->name('teams.index');
    Route::post('/teams', [TeamMemberController::class, 'store'])->name('teams.store');
    Route::put('/teams/{membership}', [TeamMemberController::class, 'update'])->name('teams.update');
    Route::delete('/teams/{membership}', [TeamMemberController::class, 'destroy'])->name('teams.destroy');
    Route::get('/chat-widget', ChatWidgetController::class)->name('chat-widget.show');
    Route::post('/chat-widget/reset-key', [ChatWidgetController::class, 'resetKey'])->name('chat-widget.reset-key');
    Route::get('/settings/appearance', [AppearanceSettingsController::class, 'edit'])->name('settings.appearance.edit');
    Route::put('/settings/appearance', [AppearanceSettingsController::class, 'update'])->name('settings.appearance.update');
    Route::get('/settings/channels', [ChannelIntegrationController::class, 'index'])->name('settings.channels.index');
    Route::put('/settings/channels/{provider}', [ChannelIntegrationController::class, 'update'])->name('settings.channels.update');
    Route::delete('/settings/channels/{provider}', [ChannelIntegrationController::class, 'destroy'])->name('settings.channels.destroy');
    Route::get('/settings/ai-model', [\App\Http\Controllers\AiModelConfigController::class, 'edit'])->name('settings.ai-model.edit');
    Route::put('/settings/ai-model', [\App\Http\Controllers\AiModelConfigController::class, 'update'])->name('settings.ai-model.update');
    Route::post('/settings/logo', [\App\Http\Controllers\LogoUploadController::class, 'update'])->name('settings.logo.update');
    Route::post('/settings/avatar', [\App\Http\Controllers\UserAvatarController::class, 'update'])->name('settings.avatar.update');
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
});
