<?php

/*
|--------------------------------------------------------------------------
| ProductCompat Web Routes
|--------------------------------------------------------------------------
*/

use Modules\ProductCompat\Http\Controllers\InstallController;
use Modules\ProductCompat\Http\Controllers\ProductCompatController;
use Modules\ProductCompat\Http\Controllers\ReviewController;
use Modules\ProductCompat\Http\Controllers\ChangeReportController;
use Modules\ProductCompat\Http\Controllers\ProcessController;
use Modules\ProductCompat\Http\Controllers\QueueController;
use Modules\ProductCompat\Http\Controllers\CandidateController;
use Modules\ProductCompat\Http\Controllers\SettingsController;
use Modules\ProductCompat\Http\Controllers\BatchController;

// Install / update / uninstall
Route::middleware(['web', 'auth'])->prefix('product-compat')->group(function () {
    Route::get('/install', [InstallController::class, 'index']);
    Route::get('/uninstall', [InstallController::class, 'uninstall']);
    Route::get('/update', [InstallController::class, 'update']);
});

// Admin screens
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu'],
    'prefix' => 'product-compat',
], function () {
    // Editor
    Route::get('/', [ProductCompatController::class, 'index'])->name('productcompat.index');
    Route::post('/edit', [ProductCompatController::class, 'edit'])->name('productcompat.edit');
    Route::post('/store', [ProductCompatController::class, 'store'])->name('productcompat.store');
    Route::post('/enrich', [ProductCompatController::class, 'enrich'])->name('productcompat.enrich');

    // Review / cleanup
    Route::get('/review', [ReviewController::class, 'index'])->name('productcompat.review');
    Route::post('/review/approve', [ReviewController::class, 'approve'])->name('productcompat.review.approve');
    Route::post('/review/reject', [ReviewController::class, 'reject'])->name('productcompat.review.reject');
    Route::post('/review/bulk-approve', [ReviewController::class, 'bulkApprove'])->name('productcompat.review.bulk_approve');
    Route::post('/review/check', [ReviewController::class, 'markChecked'])->name('productcompat.review.check');
    Route::post('/review/remove-custom-fields', [ReviewController::class, 'removeCustomFields'])->name('productcompat.review.remove_cf');

    // AI data candidates (review-first apply / reject)
    Route::post('/candidate/apply', [CandidateController::class, 'apply'])->name('productcompat.candidate.apply');
    Route::post('/candidate/apply-image', [CandidateController::class, 'applyImage'])->name('productcompat.candidate.apply_image');
    Route::post('/candidate/reject', [CandidateController::class, 'reject'])->name('productcompat.candidate.reject');

    // Change report
    Route::get('/report', [ChangeReportController::class, 'index'])->name('productcompat.report');
    Route::get('/report/export', [ChangeReportController::class, 'export'])->name('productcompat.report.export');

    // Control panel: run pipeline from UI with live progress
    Route::get('/panel', [ProcessController::class, 'panel'])->name('productcompat.panel');
    Route::post('/panel/start', [ProcessController::class, 'start'])->name('productcompat.panel.start');
    Route::post('/panel/chunk', [ProcessController::class, 'runChunk'])->name('productcompat.panel.chunk');
    Route::post('/panel/progress', [ProcessController::class, 'progress'])->name('productcompat.panel.progress');
    Route::post('/panel/stop', [ProcessController::class, 'stop'])->name('productcompat.panel.stop');

    // AI search queue
    Route::get('/queue', [QueueController::class, 'index'])->name('productcompat.queue');
    Route::post('/queue/add', [QueueController::class, 'add'])->name('productcompat.queue.add');
    Route::post('/queue/retry', [QueueController::class, 'retry'])->name('productcompat.queue.retry');
    Route::post('/queue/skip', [QueueController::class, 'skip'])->name('productcompat.queue.skip');
    Route::post('/queue/backfill', [QueueController::class, 'backfill'])->name('productcompat.queue.backfill');
    Route::post('/queue/all', [QueueController::class, 'queueAll'])->name('productcompat.queue.all');

    // Offline batch enrichment (generate → download → upload enriched → import)
    Route::get('/batches', [BatchController::class, 'index'])->name('productcompat.batches');
    Route::post('/batches/generate', [BatchController::class, 'generate'])->name('productcompat.batches.generate');
    Route::get('/batches/download', [BatchController::class, 'download'])->name('productcompat.batches.download');
    Route::get('/batches/download-all', [BatchController::class, 'downloadAll'])->name('productcompat.batches.download_all');
    Route::post('/batches/import', [BatchController::class, 'import'])->name('productcompat.batches.import');
    Route::post('/batches/match', [BatchController::class, 'match'])->name('productcompat.batches.match');
    Route::post('/batches/clear', [BatchController::class, 'clear'])->name('productcompat.batches.clear');

    // Settings
    Route::get('/settings', [SettingsController::class, 'edit'])->name('productcompat.settings');
    Route::post('/settings', [SettingsController::class, 'save'])->name('productcompat.settings.save');
});
