<?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 Templates (must be before /{id} routes)
    Route::get('/templates', [BlockRegistryController::class, 'blockTemplatesIndex'])
        ->name('admin.blocks.templates.index');
    Route::get('/templates/create', [BlockRegistryController::class, 'blockTemplateCreate'])
        ->name('admin.blocks.templates.create');
    Route::post('/templates', [BlockRegistryController::class, 'blockTemplateStore'])
        ->name('admin.blocks.templates.store');
    Route::get('/templates/{id}/edit', [BlockRegistryController::class, 'blockTemplateEdit'])
        ->name('admin.blocks.templates.edit');
    Route::put('/templates/{id}', [BlockRegistryController::class, 'blockTemplateUpdate'])
        ->name('admin.blocks.templates.update');
    Route::delete('/templates/{id}', [BlockRegistryController::class, 'blockTemplateDestroy'])
        ->name('admin.blocks.templates.destroy');

    // Block Sync (must be before /{id} routes)
    Route::get('/sync', [BlockRegistryController::class, 'syncIndex'])
        ->name('admin.blocks.sync');
    Route::post('/sync/export/{slug}', [BlockRegistryController::class, 'syncExport'])
        ->name('admin.blocks.sync.export');
    Route::post('/sync/export-all', [BlockRegistryController::class, 'syncExportAll'])
        ->name('admin.blocks.sync.exportAll');
    Route::post('/sync/import/{slug}', [BlockRegistryController::class, 'syncImport'])
        ->name('admin.blocks.sync.import');
    Route::post('/sync/import-all', [BlockRegistryController::class, 'syncImportAll'])
        ->name('admin.blocks.sync.importAll');
    Route::get('/sync/diff/{slug}', [BlockRegistryController::class, 'syncDiff'])
        ->name('admin.blocks.sync.diff');

    // 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 data endpoints
    Route::get('/ajax/products', [BlockRegistryController::class, 'ajaxGetProducts'])
        ->name('admin.blocks.ajax.products');
    Route::get('/ajax/link-search', [BlockRegistryController::class, 'ajaxSearchInternalLinks'])
        ->name('admin.blocks.ajax.linkSearch');
    Route::get('/ajax/page-search', [BlockRegistryController::class, 'ajaxSearchPages'])
        ->name('admin.blocks.ajax.pageSearch');
    Route::get('/ajax/listing-containers', [BlockRegistryController::class, 'ajaxGetListingContainers'])
        ->name('admin.blocks.ajax.listingContainers');

    // AJAX endpoints for block templates on entity pages
    Route::get('/ajax/entity/available-templates', [BlockRegistryController::class, 'ajaxGetAvailableTemplates'])
        ->name('admin.blocks.ajax.entity.availableTemplates');
    Route::post('/ajax/entity/apply-template', [BlockRegistryController::class, 'ajaxApplyTemplate'])
        ->name('admin.blocks.ajax.entity.applyTemplate');

    // 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');
});
