<?php

use Illuminate\Support\Facades\Route;


use App\Modules\ThreeCX\Controllers\ThreeCXController;
use App\Modules\ThreeCX\Controllers\ThreeCXAuthController;
use App\Modules\ThreeCX\Controllers\CallsControllerREST;

// Group for authenticated routes
Route::middleware(['web', 'auth'])->prefix('threecx')->group(function () {
    Route::post('/call/start', [ThreeCXController::class, 'startCall'])->name('threecx.call.start');
    Route::post('/call/stop', [ThreeCXController::class, 'stopCall'])->name('threecx.call.stop');
    Route::get('/call/status', [ThreeCXController::class, 'getCallStatus'])->name('threecx.call.status');

    Route::get('/logs/task', [ThreeCXController::class, 'getTaskCallLogs'])->name('threecx.logs.task');
    Route::get('/logs/user', [ThreeCXController::class, 'getUserCallLogs'])->name('threecx.logs.user');
    Route::get('/task/{id}', [ThreeCXController::class, 'getTaskData'])->name('threecx.task.data');
});

// Admin-only routes
Route::middleware(['web', 'auth', 'admin'])->prefix('admin/threecx')->group(function () {
    Route::post('/sync-cdr', [ThreeCXController::class, 'syncCDR'])->name('threecx.admin.sync_cdr');
    Route::get('/test-connection', [ThreeCXController::class, 'testConnection'])->name('threecx.admin.test_connection');
    Route::post('/test-call', [ThreeCXController::class, 'adminTestCall'])->name('threecx.admin.test_call');
    Route::post('/hangup-call', [ThreeCXController::class, 'adminHangupCall'])->name('threecx.admin.hangup_call');
});

// API routes
Route::middleware(['web', 'auth'])->prefix('api/threecx')->group(function () {
    Route::post('/call/start', [ThreeCXController::class, 'startCall'])->name('api.threecx.call.start');
    Route::post('/call/stop', [ThreeCXController::class, 'stopCall'])->name('api.threecx.call.stop');
    Route::get('/call/status', [ThreeCXController::class, 'getCallStatus'])->name('api.threecx.call.status');
    Route::get('/logs/task', [ThreeCXController::class, 'getTaskCallLogs'])->name('api.threecx.logs.task');
    Route::get('/logs/user', [ThreeCXController::class, 'getUserCallLogs'])->name('api.threecx.logs.user');
    Route::get('/task/{id}', [ThreeCXController::class, 'getTaskData'])->name('api.threecx.task.data');

    Route::get('/calls/history', [CallsControllerREST::class, 'history'])->name('api.threecx.calls.history');
    Route::post('/calls', [CallsControllerREST::class, 'store'])->name('api.threecx.calls.store');
    Route::put('/calls/{id}', [CallsControllerREST::class, 'update'])->name('api.threecx.calls.update');
    Route::post('/call/history', [ThreeCXController::class, 'logCallHistory'])->name('api.threecx.call.history');
});

// API v1 backward compatibility
Route::middleware(['web', 'auth'])->prefix('api/v1/threecx')->group(function () {
    Route::post('/call/start', [ThreeCXController::class, 'startCall'])->name('api.v1.threecx.call.start');
    Route::get('/call/status/{participantId}', [ThreeCXController::class, 'getCallStatus'])->name('api.v1.threecx.call.status');
    Route::post('/call/history', [ThreeCXController::class, 'logCallHistory'])->name('api.v1.threecx.call.history');
    Route::post('/call/sync-status', [ThreeCXController::class, 'syncCallStatus'])->name('api.v1.threecx.call.sync-status');
    Route::post('/logs/finalize/{taskId}', [ThreeCXController::class, 'finalizeTaskCallLogs'])->name('api.v1.threecx.logs.finalize');
});
