<?php
/**
 * REST API Routes
 *
 * @package ZenoChatbot
 */

use ZenoChatbot\Framework\Http\Route;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your plugin. These
| routes are loaded by the ApiServiceProvider within a group which
| is assigned the "api" middleware group.
|
*/


// Admin Routes (Protected)
Route::group(['middleware' => ['auth']], function() {

    // General Settings
    Route::get('/general-settings', 'Api\V1\GeneralSettingsController@get');
    Route::post('/general-settings', 'Api\V1\GeneralSettingsController@update');

    // Email Settings
    Route::get('/email-settings', 'Api\V1\EmailSettingsController@get');
    Route::post('/email-settings', 'Api\V1\EmailSettingsController@update');

    // Chat Settings
    Route::get('/chat-settings', 'Api\V1\ChatSettingsController@get');
    Route::post('/chat-settings', 'Api\V1\ChatSettingsController@update');

    // LLM Settings
    Route::get('/llm-settings', 'Api\V1\LLMSettingsController@get');
    Route::post('/llm-settings', 'Api\V1\LLMSettingsController@update');
    Route::post('/llm-settings/test-connection', 'Api\V1\LLMSettingsController@testConnection');

    // Data Source Settings
    Route::get('/data-source-settings', 'Api\V1\DataSourceSettingsController@get');
    Route::post('/data-source-settings', 'Api\V1\DataSourceSettingsController@save');
    Route::get('/data-source-settings/post-types', 'Api\V1\DataSourceSettingsController@getPostTypes');
    Route::post('/data-source-settings/add-external-source', 'Api\V1\DataSourceSettingsController@addExternalSource');
    Route::put('/data-source-settings/update-external-source', 'Api\V1\DataSourceSettingsController@updateExternalSource');
    Route::delete('/data-source-settings/remove-external-source', 'Api\V1\DataSourceSettingsController@removeExternalSource');

    // Media Resources
    Route::get('/data-source-settings/media-resources', 'Api\V1\DataSourceSettingsController@getMediaResources');
    Route::post('/data-source-settings/media-resources', 'Api\V1\DataSourceSettingsController@addMediaResource');
    Route::delete('/data-source-settings/media-resources', 'Api\V1\DataSourceSettingsController@removeMediaResource');

    // Internal Content Settings
    Route::get('/internal-content-settings', 'Api\V1\InternalContentSettingsController@get');
    Route::post('/internal-content-settings', 'Api\V1\InternalContentSettingsController@save');
    Route::get('/internal-content-settings/post-types', 'Api\V1\InternalContentSettingsController@getPostTypes');

    // Internal Content Index
    Route::post('/internal-content-index/build', 'Api\V1\InternalContentIndexController@buildIndex');
    Route::post('/internal-content-index/clear', 'Api\V1\InternalContentIndexController@clearIndex');
    Route::get('/internal-content-index/status', 'Api\V1\InternalContentIndexController@getIndexStatus');
    Route::get('/internal-content-index/overview', 'Api\V1\InternalContentIndexController@getOverview');
    Route::post('/sitemap-content-index/build', 'Api\V1\SitemapContentIndexController@buildSitemapIndex');
    Route::post('/sitemap-content-index/build/start', 'Api\V1\SitemapContentIndexController@startSitemapBuildJob');
    Route::post('/sitemap-content-index/build/process', 'Api\V1\SitemapContentIndexController@processSitemapBuildJob');
    Route::get('/sitemap-content-index/build/status', 'Api\V1\SitemapContentIndexController@getSitemapBuildJobStatus');
    Route::post('/sitemap-content-index/clear', 'Api\V1\SitemapContentIndexController@clearSitemapIndex');
    Route::get('/sitemap-content-index/status', 'Api\V1\SitemapContentIndexController@getSitemapIndexStatus');
    Route::post('/product-content-index/build/start', 'Api\V1\ProductContentIndexController@startProductBuildJob');
    Route::post('/product-content-index/build/process', 'Api\V1\ProductContentIndexController@processProductBuildJob');
    Route::get('/product-content-index/build/status', 'Api\V1\ProductContentIndexController@getProductBuildJobStatus');
    Route::post('/product-content-index/clear', 'Api\V1\ProductContentIndexController@clearProductIndex');
    Route::get('/product-content-index/status', 'Api\V1\ProductContentIndexController@getProductIndexStatus');
    Route::get('/knowledge-notes-index', 'Api\V1\KnowledgeNotesIndexController@getKnowledgeNotes');
    Route::post('/knowledge-notes-index/save', 'Api\V1\KnowledgeNotesIndexController@saveKnowledgeNotes');
    Route::post('/knowledge-notes-index/clear', 'Api\V1\KnowledgeNotesIndexController@clearKnowledgeNotes');
    Route::get('/knowledge-notes-index/status', 'Api\V1\KnowledgeNotesIndexController@getKnowledgeNotesStatus');

    // Configuration Management
    Route::group(['prefix' => 'config'], function() {
        Route::get('/', 'Api\V1\ConfigController@get');
        Route::post('/', 'Api\V1\ConfigController@update');
        Route::post('/test-connection', 'Api\V1\ConfigController@testConnection');
        Route::post('/reset', 'Api\V1\ConfigController@reset');
    });

    // Content Index Management
    Route::group(['prefix' => 'index'], function() {
        Route::get('/', 'Api\V1\IndexController@status');
        Route::post('/rebuild', 'Api\V1\IndexController@rebuild');
        Route::delete('/clear', 'Api\V1\IndexController@clear');
    });

    // External URLs Management
    Route::group(['prefix' => 'external-urls'], function() {
        Route::get('/', 'Api\V1\ExternalUrlController@index');
        Route::post('/', 'Api\V1\ExternalUrlController@store');
        Route::delete('/{id}', 'Api\V1\ExternalUrlController@destroy');
    });

    // Dashboard Analytics
    Route::group(['prefix' => 'dashboard'], function () {
        Route::get('/stats', 'Api\V1\DashboardController@stats');
        Route::get('/chart-data', 'Api\V1\DashboardController@chartData');
        Route::get('/token-usage-chart', 'Api\V1\DashboardController@tokenUsageChartData');
        Route::get('/recent-conversations', 'Api\V1\DashboardController@recentConversations');
    });


    Route::group(['prefix' => 'admin/conversations'], function() {
        Route::get('/', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'index']);
        Route::get('/{id}', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'show']);
        Route::patch('/{id}/status', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'updateStatus']);
        Route::post('/{id}/summary/generate', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'generateSummary']);
        Route::post('/{id}/reply', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'sendReply']);
        Route::delete('/{id}', [\ZenoChatbot\Http\Controller\Api\V1\AdminChatController::class, 'destroy']);
    });

    // Saved Replies Management
    Route::group(['prefix' => 'admin/saved-replies'], function() {
        Route::get('/', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'index']);
        Route::post('/', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'store']);
        // Semantic settings routes (must come before /{id} to avoid being matched by the generic route)
        Route::get('/semantic-settings', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'getSemanticSettings']);
        Route::post('/semantic-settings', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'updateSemanticSettings']);
        Route::post('/generate-embeddings', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'generateEmbeddings']);
        Route::post('/regenerate-embeddings', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'regenerateAllEmbeddings']);
        Route::post('/clear-embeddings', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'clearEmbeddings']);
        // Analytics route (must come before /{id} to avoid being matched by the generic route)
        Route::get('/analytics', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'getAnalytics']);
        // Toggle status must come before /{id} to avoid being matched by the generic route
        Route::patch('/{id}/toggle-status', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'toggleStatus']);
        Route::get('/{id}', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'show']);
        Route::patch('/{id}', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'update']);
        Route::delete('/{id}', [\ZenoChatbot\Http\Controller\Api\V1\SavedReplyController::class, 'destroy']);
    });

});
