<?php

use App\Http\Controllers\Frontend\HomeController;
use App\Http\Controllers\Frontend\ServiceController;
use App\Http\Controllers\Frontend\ConsultationController;
use App\Http\Controllers\Frontend\PaymentController;
use App\Http\Controllers\User\DashboardController;
use Illuminate\Support\Facades\Route;

// Redirect /admin/dashboard to /admin for convenience
Route::redirect('/admin/dashboard', '/admin');
Route::post('/admin/support-conversations/{conversation}/reply-inline', [App\Http\Controllers\SupportController::class, 'adminReply'])
    ->middleware('auth')
    ->name('admin.support-conversations.reply-inline');

/*
|--------------------------------------------------------------------------
| Public Frontend Routes
|--------------------------------------------------------------------------
*/
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/services', [ServiceController::class, 'index'])->name('services.index');
Route::get('/services/{service:slug}', [ServiceController::class, 'show'])->name('services.show');
Route::get('/consultation', [ConsultationController::class, 'index'])->name('consultation');
Route::post('/consultation', [ConsultationController::class, 'store'])->name('consultation.store');
Route::get('/about', [HomeController::class, 'about'])->name('about');
Route::get('/contact', [HomeController::class, 'contact'])->name('contact');
Route::post('/contact', [HomeController::class, 'sendContact'])->name('contact.send');

// Blog System
Route::get('/blog', [App\Http\Controllers\Frontend\BlogController::class, 'index'])->name('blog.index');
Route::get('/blog/{blog:slug}', [App\Http\Controllers\Frontend\BlogController::class, 'show'])->name('blog.show');

// ─────────────────────────────────────
// CONVERSION ENGINE ENDPOINTS (PUBLIC)
// ─────────────────────────────────────
Route::post('/conversion/lead', [App\Http\Controllers\ConversionController::class, 'captureLead'])->name('conversion.lead');
Route::get('/conversion/help/search', [App\Http\Controllers\ConversionController::class, 'searchHelp'])->name('conversion.help.search');
Route::get('/conversion/help/article/{id}', [App\Http\Controllers\ConversionController::class, 'getArticle'])->name('conversion.help.article');
Route::post('/conversion/support/chat', [App\Http\Controllers\SupportController::class, 'startChat'])->name('conversion.support.chat');
Route::get('/conversion/support/messages/{id}', [App\Http\Controllers\SupportController::class, 'getMessages'])->name('conversion.support.messages');
Route::post('/conversion/support/reply/{id}', [App\Http\Controllers\SupportController::class, 'reply'])->name('conversion.support.reply');

/*
|--------------------------------------------------------------------------
| Invoicing and Business Logic
|--------------------------------------------------------------------------
*/
Route::get('/invoices/{invoice}/download', [\App\Http\Controllers\InvoiceController::class, 'download'])
    ->name('invoices.download')
    ->middleware('auth');

/*
|--------------------------------------------------------------------------
| Auth Routes
|--------------------------------------------------------------------------
*/
require __DIR__.'/auth.php';

/*
|--------------------------------------------------------------------------
| User Dashboard Routes (authenticated)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->prefix('dashboard')->name('user.')->group(function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/orders', [DashboardController::class, 'orders'])->name('orders');
    Route::get('/orders/{order}', [DashboardController::class, 'orderDetail'])->name('orders.show');
    Route::post('/orders/{order}/documents', [DashboardController::class, 'uploadDocument'])->name('orders.upload');
    Route::post('/orders/{order}/remarks', [DashboardController::class, 'addRemark'])->name('orders.remark');
    // Route::get('/checkout/success/{id}', [CheckoutController::class, 'success'])->name('checkout.success');

    // ─────────────────────────────────────
    // CONVERSION ENGINE ENDPOINTS
    // ─────────────────────────────────────
    Route::get('/invoices', [DashboardController::class, 'invoices'])->name('invoices');
    Route::get('/consultations', [DashboardController::class, 'consultations'])->name('consultations');
    Route::get('/profile', [DashboardController::class, 'profile'])->name('profile');
    Route::put('/profile', [DashboardController::class, 'updateProfile'])->name('profile.update');
    Route::post('/notifications/read-all', [DashboardController::class, 'markNotificationsRead'])->name('notifications.readAll');
});

/*
|--------------------------------------------------------------------------
| Order Placement Flow
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->group(function () {
    Route::post('/orders', [DashboardController::class, 'placeOrder'])->name('orders.place');
    
    // Payment Page
    Route::get('/payment/{order}', [PaymentController::class, 'show'])->name('payment.show');
    Route::post('/payment/{order}/manual', [PaymentController::class, 'submitManualPayment'])->name('payment.manual.submit');
});
