<?php

use Illuminate\Support\Facades\Route;
use Mtc\Modules\BlockRegistry\Http\Controllers\BlockRegistryController;

Route::prefix('admin/blocks')->middleware('auth')->group(function () {
    // Global Blocks (must be before /{id} routes)
    Route::get('/global', [BlockRegistryController::class, 'globalIndex'])
        ->name('admin.blocks.global.index');
    Route::get('/global/create', [BlockRegistryController::class, 'globalCreate'])
        ->name('admin.blocks.global.create');
    Route::post('/global', [BlockRegistryController::class, 'globalStore'])
        ->name('admin.blocks.global.store');
    Route::delete('/global/{id}', [BlockRegistryController::class, 'globalDestroy'])
        ->name('admin.blocks.global.destroy');

    // Block CRUD
    Route::get('/', [BlockRegistryController::class, 'index'])
        ->name('admin.blocks.index');
    Route::get('/create', [BlockRegistryController::class, 'create'])
        ->name('admin.blocks.create');
    Route::post('/', [BlockRegistryController::class, 'store'])
        ->name('admin.blocks.store');
    Route::get('/{id}/preview', [BlockRegistryController::class, 'previewBlockDefinition'])
        ->name('admin.blocks.preview');
    Route::get('/{id}/edit', [BlockRegistryController::class, 'edit'])
        ->name('admin.blocks.edit');
    Route::put('/{id}', [BlockRegistryController::class, 'update'])
        ->name('admin.blocks.update');
    Route::delete('/{id}', [BlockRegistryController::class, 'destroy'])
        ->name('admin.blocks.destroy');
    Route::post('/{id}/duplicate', [BlockRegistryController::class, 'duplicate'])
        ->name('admin.blocks.duplicate');

    // Entity Block Data editing
    Route::get('/data/{id}', [BlockRegistryController::class, 'editData'])
        ->name('admin.blocks.data.edit');
    Route::post('/data/{id}', [BlockRegistryController::class, 'updateData'])
        ->name('admin.blocks.data.update');
    Route::delete('/data/{id}', [BlockRegistryController::class, 'destroyData'])
        ->name('admin.blocks.data.destroy');
    Route::get('/data/{id}/preview', [BlockRegistryController::class, 'previewData'])
        ->name('admin.blocks.data.preview');

    // Category management
    Route::get('/categories', [BlockRegistryController::class, 'categories'])
        ->name('admin.blocks.categories');
    Route::post('/categories/{id?}', [BlockRegistryController::class, 'categoryStore'])
        ->name('admin.blocks.categories.store');
    Route::delete('/categories/{id}', [BlockRegistryController::class, 'categoryDestroy'])
        ->name('admin.blocks.categories.destroy');

    // AJAX endpoints (shared across all entity types)
    Route::post('/ajax/reorder', [BlockRegistryController::class, 'ajaxReorderBlocks'])
        ->name('admin.blocks.ajax.reorder');
    Route::post('/ajax/toggle-status', [BlockRegistryController::class, 'ajaxToggleStatus'])
        ->name('admin.blocks.ajax.toggleStatus');
    Route::post('/ajax/toggle-active', [BlockRegistryController::class, 'ajaxToggleActive'])
        ->name('admin.blocks.ajax.toggleActive');

    // AJAX endpoints for entity block management (pages, products, categories)
    Route::get('/ajax/entity/available', [BlockRegistryController::class, 'ajaxGetAvailableBlocksForEntity'])
        ->name('admin.blocks.ajax.entity.available');
    Route::post('/ajax/entity/add', [BlockRegistryController::class, 'ajaxAddBlockToEntity'])
        ->name('admin.blocks.ajax.entity.add');
    Route::post('/ajax/entity/add-global', [BlockRegistryController::class, 'ajaxAddGlobalBlockToEntity'])
        ->name('admin.blocks.ajax.entity.addGlobal');
    Route::get('/ajax/entity/blocks-html', [BlockRegistryController::class, 'ajaxEntityBlocksHtmlForEntity'])
        ->name('admin.blocks.ajax.entity.blocksHtml');

    // Preview for any entity type
    Route::get('/preview/{entityType}/{entityId}', [BlockRegistryController::class, 'previewEntityBlocks'])
        ->name('admin.blocks.preview.entity');
});
