<?php

use App\Http\Controllers\Admin\AdditionalActivitiesController;
use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\CacheController;
use App\Http\Controllers\Admin\FormController;
use App\Http\Controllers\Admin\FormSubmissionController;
use App\Http\Controllers\Admin\FranchiseeController as FranchiseeController;
use App\Http\Controllers\Admin\FranchiseeGroupController;
use App\Http\Controllers\Admin\ImpersonationController;
use App\Http\Controllers\Admin\MediaController;
use App\Http\Controllers\Admin\NotificationController;
use App\Http\Controllers\Admin\PageController as PageController;
use App\Http\Controllers\Admin\PageTemplateController as PageTemplateController;
use App\Http\Controllers\Admin\PaymentController as AdminPaymentController;
use App\Http\Controllers\Admin\ProgramController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\TeacherController;
use App\Http\Controllers\Admin\TestimonialController;
use App\Http\Controllers\Admin\TranslateController;
use App\Http\Controllers\Admin\TranslateTemplateController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\TrashController;
use App\Http\Controllers\Admin\WhatsappMessageController;
use App\Http\Controllers\Admin\WhatsappTemplateController;
use App\Http\Controllers\Admin\FranchiseeCampaignController;
use App\Http\Controllers\Api\V1\ConnectPaymentController as ApiConnectPaymentController;
use App\Http\Controllers\Auth\AuthController as AdminAuthController;
use App\Http\Controllers\Auth\MicrosoftLoginController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::prefix('auth')->group(function () {
    Route::post('/login', [AdminAuthController::class, 'login']);
    Route::post('/register', [AdminAuthController::class, 'register']);

    Route::middleware(['auth:sanctum', 'verify.spa'])->group(function () {
        Route::post('/logout', [AdminAuthController::class, 'logout']);
        Route::get('/user', [AdminAuthController::class, 'user']);
        Route::post('/update-password', [AdminAuthController::class, 'updatePassword'])->name('updatePassword');
    });

    Route::get('microsoft/redirect', [MicrosoftLoginController::class, 'redirect']);
    Route::get('microsoft/callback', [MicrosoftLoginController::class, 'callback'])->name('microsoft.callback');

});
// ✅ Laravel Admin API routes
Route::middleware(['auth:sanctum', 'verify.spa'])->group(function () {
    Route::prefix('admin')->group(function () {

        Route::post('/update-setting', [SettingController::class, 'saveSettings']); // save a settings
        Route::get('/settings', [SettingController::class, 'getSettings']); // save a settings

        // Franchisee UTM campaign languages (from Zoho) — drives translation tabs.
        Route::get('/franchisee-campaigns/languages', [FranchiseeCampaignController::class, 'languages']);
        Route::get('/franchisee-campaigns/debug', [FranchiseeCampaignController::class, 'debug']);
        Route::post('/refresh-stripe-connect', [ApiConnectPaymentController::class, 'refreshStripeConnectStatus']);
        Route::post('/register-wallet-domain', [ApiConnectPaymentController::class, 'registerWalletDomain']);
        Route::post('/stripe-dashboard-link', [ApiConnectPaymentController::class, 'stripeDashboardLink']);

        // =============================================
        //  cache Routes
        // =============================================
        // // clear cache for Franchise admin
        Route::post('/franchise/clear-cache', [CacheController::class, 'clearFranchise'])
            ->name('franchise.clear-cache');
        // clear cache for all franchisees
        Route::post('/clear-cache', [CacheController::class, 'clearAll'])
            ->name('admin.clear-cache');

        // =============================================
        //  franchisees Routes
        // =============================================
        //Route::group(['prefix' => 'franchisees', 'middleware' => ['auth:sanctum'],], function () {
        Route::prefix('franchisees')->group(function () {
            Route::get('/all', [FranchiseeController::class, 'index'])->name('franchisees.index');
            Route::post('/add', [FranchiseeController::class, 'store'])->name('franchisees.store');
            Route::get('{franchiseeId}', [FranchiseeController::class, 'show'])->name('franchisees.show');
            Route::put('{franchiseeId}', [FranchiseeController::class, 'update'])->name('franchisees.update');
            Route::delete('{franchiseeId}', [FranchiseeController::class, 'destroy'])->name('franchisees.destroy');

            Route::delete('permanent-delete/{franchiseeId}', [FranchiseeController::class, 'permanentDelete'])->name('franchisees.permanentDelete');
            // Assign groups to a specific franchisee during edit
            Route::post('{franchiseeId}/assign-groups', [FranchiseeGroupController::class, 'assignGroupsToFranchisee'])->name('franchisees.assign-groups');
        });

        // =============================================
        //  franchisee groups Routes
        // =============================================
        Route::prefix('franchisee-groups')->group(function () {
            Route::get('/all', [FranchiseeGroupController::class, 'index'])->name('franchisee-groups.index');
            Route::post('/add', [FranchiseeGroupController::class, 'store'])->name('franchisee-groups.store');
            Route::get('/{groupId}', [FranchiseeGroupController::class, 'show'])->name('franchisee-groups.show');
            Route::put('/{groupId}', [FranchiseeGroupController::class, 'update'])->name('franchisee-groups.update');
            Route::delete('/{groupId}', [FranchiseeGroupController::class, 'destroy'])->name('franchisee-groups.destroy');
            // Bulk add franchisees to groups
            Route::post('/add-franchisees', [FranchiseeGroupController::class, 'addFranchisees'])->name('franchisee-groups.add-franchisees');
        });

        // ==========================================================================
        // 📝 Grouped routes for managing CMS pages (Add, List, Fetch by ID)
        // ==========================================================================
        Route::prefix('pages')->group(function () {
            Route::post('/add', [PageController::class, 'addPage']); // Add a new page
            Route::get('/all', [PageController::class, 'getAllPages']); // Retrieve all pages
            Route::get('/{id}/update-preview', [PageController::class, 'updatePreview']);
            Route::get('/{id}', [PageController::class, 'getPage']); // Retrieve a single page by ID
            Route::post('update/{id}', [PageController::class, 'updatePage']); // update a single page by ID
            Route::post('delete/{id}', [PageController::class, 'deletePageById']); // delete a single page by ID
            Route::delete('{id}/translation/{languageCode}', [PageController::class, 'removeTranslation']); // remove a page translation by language


        });

        Route::prefix('transaction')->group(function () {
            Route::get('/pending-approvals', [PageController::class, 'getPendingAprovalFranchisee']); // Retrieve all Franchisee pending appovals
            Route::get('/pending-pages/{id}', [PageController::class, 'getPendingFranchissePages']); // Retrieve all pending appovals pages
            Route::post('/status/{id}', [PageController::class, 'updateTranslationStatus']); // update a single page by ID
        });


        Route::prefix('pagetemplates')->group(function () {
            Route::post('add', [PageTemplateController::class, 'addTemplate']);// Add a new template
            Route::get('/all', [PageTemplateController::class, 'getPageallTemplate']); // Retrieve all pages template
            Route::get('/{id}', [PageTemplateController::class, 'getTemplate']);// Add a new template
            Route::post('/{id}', [PageTemplateController::class, 'updateTemplate']);// Add a new template
            Route::post('delete/{id}', [PageTemplateController::class, 'deleteTemplateById']);
        });
        // =============================================
        //  Users Routes
        // =============================================
        Route::prefix('users')->group(function () {
            Route::get('/all', [UserController::class, 'index'])->name('users.index');
            Route::post('/add', [UserController::class, 'store'])->name('users.store');
            Route::get('/{userId}', [UserController::class, 'show'])->name('users.show');
            Route::put('/{userId}', [UserController::class, 'update'])->name('users.update');
            Route::delete('/{userId}', [UserController::class, 'destroy'])->name('users.destroy');
        });

        // =============================================
        //  programs Routes
        // =============================================
        Route::prefix('programs')->group(function () {
            Route::get('/all', [ProgramController::class, 'index']);
            Route::post('/add', [ProgramController::class, 'store']);
            Route::get('/{id}', [ProgramController::class, 'show']);
            Route::get('/{id}/update-preview', [ProgramController::class, 'updatePreview']);
            Route::get('/{id}/reset-preview', [ProgramController::class, 'resetPreview']);
            Route::put('/{id}', [ProgramController::class, 'update']);
            Route::delete('/{id}', [ProgramController::class, 'destroy']);
            Route::post('/{id}/override', [ProgramController::class, 'update']);
            Route::post('/{id}/adopt-global', [ProgramController::class, 'adoptGlobal']);
            Route::post('/{id}/reset-global', [ProgramController::class, 'resetToGlobal']);
            Route::post('/{id}/update-latest-global', [ProgramController::class, 'updateToLatestGlobal']);
            Route::post('/reorder', [ProgramController::class, 'reorder']);
        });

        // =============================================
        //  Form Routes
        // =============================================
        Route::prefix('forms')->group(function () {
            Route::get('/all', [FormController::class, 'index']);
            Route::post('/add', [FormController::class, 'store']);
            Route::get('/{id}', [FormController::class, 'show']);
            Route::get('/{id}/update-preview', [FormController::class, 'updatePreview']);
            Route::put('/{id}', [FormController::class, 'update']);
            Route::post('/{id}', [FormController::class, 'update']);
            Route::delete('/{id}', [FormController::class, 'destroy']);
            Route::post('/reorder', [FormController::class, 'reorder']);
            Route::post('/{id}/adopt-global', [FormController::class, 'adoptGlobal']);
            Route::post('/{id}/reset-global', [FormController::class, 'resetToGlobal']);
            Route::post('/{id}/update-latest-global', [FormController::class, 'updateToLatestGlobal']);

            //Get franchisee Form submited data for super admin
            Route::get('/franchisee-forms', [FormSubmissionController::class, 'getFranchiseeForms']);

            //Get Submited Data By Franchisee Id
            Route::get('/franchisee-forms-entries/{id}', [FormSubmissionController::class, 'getFranchiseeFormsById']);
        });


        // =============================================
        //  Testimonial Routes
        // =============================================
        Route::prefix('testimonials')->group(function () {
            Route::get('/all', [TestimonialController::class, 'index']);
            Route::post('/add', [TestimonialController::class, 'store']);
            Route::post('/reorder', [TestimonialController::class, 'reorder']);
            Route::get('/{id}/update-preview', [TestimonialController::class, 'updatePreview']);
            Route::get('/{id}/reset-preview', [TestimonialController::class, 'resetPreview']);
            Route::post('/{id}/adopt-global', [TestimonialController::class, 'adoptGlobal']);
            Route::post('/{id}/reset-global', [TestimonialController::class, 'resetGlobal']);
            Route::post('/{id}/update-latest-global', [TestimonialController::class, 'updateLatestGlobal']);
            Route::put('/{id}', [TestimonialController::class, 'update']);
            Route::post('/{id}', [TestimonialController::class, 'update']);
            Route::get('/{id}', [TestimonialController::class, 'show']);
            Route::delete('/{id}', [TestimonialController::class, 'destroy']);
        });

        // =============================================
        //  Teachers Routes
        // =============================================
        Route::prefix('teachers')->group(function () {
            Route::get('/all', [TeacherController::class, 'getAllTeachers']);
            Route::post('/add', [TeacherController::class, 'saveTeachers']);
            Route::post('/reorder', [TeacherController::class, 'reorder']);

            Route::post('/{id}', [TeacherController::class, 'updateTeachers']);
            Route::get('/{id}', [TeacherController::class, 'getTeachersById']);
            Route::delete('/{id}', [TeacherController::class, 'deleteTeachers']);
        });

        // =============================================
        //  Additional Activities
        // =============================================
        Route::prefix('additional-activities')->group(function () {
            Route::get('/all', [AdditionalActivitiesController::class, 'getAll']);
            Route::post('/add', [AdditionalActivitiesController::class, 'saveActivities']);
            Route::post('/reorder', [AdditionalActivitiesController::class, 'reorder']);
            Route::get('/{id}/update-preview', [AdditionalActivitiesController::class, 'updatePreview']);
            Route::get('/{id}/reset-preview', [AdditionalActivitiesController::class, 'resetPreview']);
            Route::post('/{id}/adopt-global', [AdditionalActivitiesController::class, 'adoptGlobal']);
            Route::post('/{id}/reset-global', [AdditionalActivitiesController::class, 'resetToGlobal']);
            Route::post('/{id}/update-latest-global', [AdditionalActivitiesController::class, 'updateToLatestGlobal']);

            Route::post('/{id}', [AdditionalActivitiesController::class, 'updateActivities']);
            Route::get('/{id}', [AdditionalActivitiesController::class, 'getActivitiesById']);
            Route::delete('/{id}', [AdditionalActivitiesController::class, 'deleteActivities']);
        });

        // =============================================
        //  Notifications Routes
        // =============================================
        Route::prefix('notifications')->group(function () {
            Route::get('/all', [NotificationController::class, 'index']);
            Route::get('/count', [NotificationController::class, 'count']);
            Route::post('/{id}/read', [NotificationController::class, 'markAsRead']);
            Route::post('/read-all', [NotificationController::class, 'markAllAsRead']);
            Route::delete('/{id}', [NotificationController::class, 'destroy']);
            Route::delete('/', [NotificationController::class, 'clearAll']);
        });

        // =============================================
        //  Media Routes
        // =============================================
        Route::prefix('media')->group(function () {
            Route::post('/', [MediaController::class, 'uploadMediaFiles']);

            Route::delete('/{id}', [MediaController::class, 'deleteMedia']);
            Route::get('/', [MediaController::class, 'getMediaImages']);

        });

        // =============================================
        //  Payments Routes
        // =============================================
        Route::prefix('payments')->group(function () {
            Route::get('/all', [AdminPaymentController::class, 'index'])->name('payments.index');
        });

        // =============================================
        //  Trash Routes
        // =============================================
        Route::prefix('trash')->group(function () {

            Route::get('list', [TrashController::class, 'trashList']);
            Route::post('restore/{id}', [TrashController::class, 'restore']);
            Route::post('hard-delete/{id}', [TrashController::class, 'forceDelete']);

            // Route::post('{type}/bulk-restore', [TrashController::class, 'bulkRestore']);
            // Route::delete('{type}/bulk-force', [TrashController::class, 'bulkForceDelete']);

        });
        // =============================================
        //  Translate Routes
        // =============================================
        Route::post('/translate', [TranslateController::class, 'translate']);

        // =============================================
        //  impersonating Routes
        // =============================================
        Route::post('/impersonate/start/{franchiseeId}', [ImpersonationController::class, 'start'])
            ->name('impersonate.start');

        // status to show banner if impersonating
        Route::post('/impersonate/stop', [ImpersonationController::class, 'stop'])
            ->name('impersonation.stop');

        // stop impersonation
        Route::get('/impersonate/status', [ImpersonationController::class, 'status'])
            ->name('impersonate.status');

        // =============================================
        //  impersonating Routes
        // =============================================
        Route::get('/audit-logs', [AuditLogController::class, 'index']);
        Route::get('/audit-logs/{id}', [AuditLogController::class, 'show']);

        // =============================================
        //  Translate templates
        // =============================================
        Route::prefix('translate-templates')->group(function () {
            Route::get('/', [TranslateTemplateController::class, 'index']);
            Route::post('/', [TranslateTemplateController::class, 'store']);
            Route::post('/check-page-translation', [TranslateTemplateController::class, 'checkPageTranslation']);
            Route::get('/occupied-language-codes', [TranslateTemplateController::class, 'occupiedLanguageCodes']);
            Route::get('/readiness/{language_code}', [TranslateTemplateController::class, 'checkLanguageReadiness']);
            Route::get('/published/{language_code}', [TranslateTemplateController::class, 'getPublishedTemplateByLanguage']);
            Route::post('/published/{language_code}/adopt', [TranslateTemplateController::class, 'adoptPublishedTemplate']);

            Route::get('/{id}', [TranslateTemplateController::class, 'show']);
            Route::put('/{id}', [TranslateTemplateController::class, 'update']);
            Route::delete('/{id}', [TranslateTemplateController::class, 'destroy']);
            Route::patch('/{id}/status', [TranslateTemplateController::class, 'updateStatus']);
        });

        // =============================================
        //  WhatsApp welcome templates
        // =============================================
        Route::prefix('whatsapp-templates')->group(function () {
            Route::get('/', [WhatsappTemplateController::class, 'index']);
            Route::get('/check-name', [WhatsappTemplateController::class, 'checkName']);
            // Abusive-language / profanity check on the body before submitting to Meta.
            Route::post('/check-message', [WhatsappTemplateController::class, 'checkMessage']);
            // AI-translate the template content into a campaign language.
            Route::post('/translate-content', [WhatsappTemplateController::class, 'translateContent']);
            Route::post('/', [WhatsappTemplateController::class, 'store']);
            Route::get('/{id}', [WhatsappTemplateController::class, 'show']);
            Route::put('/{id}', [WhatsappTemplateController::class, 'update']);
            Route::delete('/{id}', [WhatsappTemplateController::class, 'destroy']);
            Route::patch('/{id}/default', [WhatsappTemplateController::class, 'setDefault']);
        });

        // =============================================
        //  WhatsApp message flow (super admin creates + orders; franchisee translates)
        // =============================================
        Route::prefix('whatsapp-messages')->group(function () {
            Route::get('/', [WhatsappMessageController::class, 'index']);
            Route::post('/', [WhatsappMessageController::class, 'store']);
            Route::post('/reorder', [WhatsappMessageController::class, 'reorder']);
            Route::post('/adopt-global', [WhatsappMessageController::class, 'adoptGlobal']);
            // Franchisee translation: AI translate goes through the shared POST admin/translate
            // endpoint (type=whatsapp_messages); these handle manual edits + fetching per language.
            Route::post('/translations', [WhatsappMessageController::class, 'saveTranslations']);
            Route::get('/translations/{language_code}', [WhatsappMessageController::class, 'translations']);
            // Question-type support (literal segments — keep BEFORE the /{id} wildcard).
            Route::get('/age-options', [WhatsappMessageController::class, 'ageOptions']);
            Route::get('/program-options', [WhatsappMessageController::class, 'programOptions']);
            Route::get('/programs-for-age', [WhatsappMessageController::class, 'programsForAge']);
            Route::get('/media-for-program', [WhatsappMessageController::class, 'mediaForProgram']);
            Route::get('/{id}', [WhatsappMessageController::class, 'show']);
            Route::put('/{id}', [WhatsappMessageController::class, 'update']);
            Route::delete('/{id}', [WhatsappMessageController::class, 'destroy']);
            Route::post('/{id}/translate', [WhatsappMessageController::class, 'saveTranslation']);
        });
    });

    Route::prefix('api/franchise')->group(function () {
        Route::post('forms/{id}/accept-global-update', [FormController::class, 'updateToLatestGlobal']);
        Route::post('forms/{id}/reset-to-global', [FormController::class, 'resetToGlobal']);
        Route::post('pages/{page}/accept-global-update', [PageController::class, 'acceptGlobalUpdate']);
        Route::post('pages/{page}/reset-to-global', [PageController::class, 'resetFranchisePageToGlobal']);
    });
});


// ✅ Catch-all: Vue frontend routes (e.g., /dashboard, /users)
Route::get('/{any}', function () {
    return view('application'); // Blade file serving your Vue app
})->where('any', '^(?!admin).*'); // Excludes /admin/*
