<?php

use App\Http\Controllers\Admin\AuthController as AdminAuthController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\OrderController as AdminOrderController;
use App\Http\Controllers\Admin\PlanController;
use App\Http\Controllers\Admin\PlatformOrderController;
use App\Http\Controllers\Admin\PlatformSettingController;
use App\Http\Controllers\Admin\ProductCategoryController as AdminProductCategoryController;
use App\Http\Controllers\Admin\ProductController as AdminProductController;
use App\Http\Controllers\Admin\MerchantController as AdminMerchantController;
use App\Http\Controllers\Admin\SiteSubscriptionController;
use App\Http\Controllers\Admin\SupportTicketController;
use App\Http\Controllers\Front\H5Controller;
use App\Http\Controllers\Front\PlatformController as FrontPlatformController;
use App\Http\Controllers\MerchantAdmin\AuthController as MerchantAdminAuthController;
use App\Http\Controllers\MerchantAdmin\DashboardController as MerchantDashboardController;
use App\Http\Controllers\MerchantAdmin\OrderController as MerchantOrderController;
use App\Http\Controllers\MerchantAdmin\ProductCategoryController as MerchantProductCategoryController;
use App\Http\Controllers\MerchantAdmin\ProductController as MerchantProductController;
use App\Http\Controllers\MerchantAdmin\UserController as MerchantUserController;
use App\Http\Controllers\Front\PcController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\SiteAdmin\AuthController as SiteAdminAuthController;
use App\Http\Controllers\SiteAdmin\DashboardController as SiteDashboardController;
use App\Http\Controllers\SiteAdmin\MerchantController as SiteMerchantController;
use App\Http\Controllers\SiteAdmin\OrderController as SiteOrderController;
use App\Http\Controllers\SiteAdmin\ProductController as SiteProductController;
use App\Http\Controllers\Wechat\MiniProgramController;
use App\Http\Controllers\Wechat\MpController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Route;

Route::get('/', [HomeController::class, 'index']);
Route::get('/pc', [PcController::class, 'index']);
Route::get('/h5', [H5Controller::class, 'index']);
Route::get('/wechat/mp', [MpController::class, 'index']);
Route::get('/wechat/mini', [MiniProgramController::class, 'index']);

// 对外平台（官网/开通入口）- 前期先承接介绍与套餐展示（A：站点开通型优先）
Route::prefix('platform')->group(function () {
    Route::get('/', [FrontPlatformController::class, 'index']);
    Route::get('/plans', [FrontPlatformController::class, 'plans']);

    // A：站点开通型前期先用“线索/开通意向”承接（后续再接自助开通/下单）
    Route::post('/leads', [\App\Http\Controllers\Front\PlatformLeadController::class, 'store']);
});

Route::get('/health', function () {
    $dbOk = false;
    $dbMsg = '未检测';
    try {
        $row = DB::selectOne('select database() as db_name, now() as now_time');
        $dbOk = true;
        $dbMsg = '数据库连接正常 / ' . $row->db_name . ' / ' . $row->now_time;
    } catch (Throwable $e) {
        $dbMsg = $e->getMessage();
    }

    $redisOk = false;
    $redisMsg = '未检测';
    try {
        $pong = Redis::ping();
        $redisOk = true;
        $redisMsg = 'Redis PING => ' . var_export($pong, true);
    } catch (Throwable $e) {
        $redisMsg = $e->getMessage();
    }

    return response()->view('welcome_status', [
        'phpVersion' => PHP_VERSION,
        'dbOk' => $dbOk,
        'dbMsg' => $dbMsg,
        'redisOk' => $redisOk,
        'redisMsg' => $redisMsg,
    ]);
});

Route::prefix('admin')->group(function () {
    Route::get('/login', [AdminAuthController::class, 'showLogin']);
    Route::post('/login', [AdminAuthController::class, 'login']);
    Route::post('/logout', [AdminAuthController::class, 'logout']);

    Route::middleware('admin.auth')->group(function () {
        Route::get('/', [DashboardController::class, 'index']);

        Route::get('/merchants', [AdminMerchantController::class, 'index']);
        Route::post('/merchants', [AdminMerchantController::class, 'store']);

        Route::get('/products', [AdminProductController::class, 'index']);
        Route::get('/products/import-histories', [AdminProductController::class, 'importHistories']);
        Route::get('/products/import-histories/export', [AdminProductController::class, 'exportImportHistories']);
        Route::get('/products/export', [AdminProductController::class, 'export']);
        Route::get('/products/import-template', [AdminProductController::class, 'downloadImportTemplate']);
        Route::post('/products/import', [AdminProductController::class, 'import']);
        Route::get('/products/import-failures/{file}', [AdminProductController::class, 'downloadImportFailures']);
        Route::post('/products/batch', [AdminProductController::class, 'batchUpdate']);
        Route::post('/products', [AdminProductController::class, 'store']);
        Route::post('/products/{id}', [AdminProductController::class, 'update']);
        Route::post('/products/{id}/delete', [AdminProductController::class, 'destroy']);

        Route::get('/product-categories', [AdminProductCategoryController::class, 'index']);
        Route::post('/product-categories', [AdminProductCategoryController::class, 'store']);
        Route::post('/product-categories/{id}', [AdminProductCategoryController::class, 'update']);
        Route::post('/product-categories/{id}/delete', [AdminProductCategoryController::class, 'destroy']);

        Route::get('/orders', [AdminOrderController::class, 'index']);
        Route::get('/orders/export', [AdminOrderController::class, 'export']);
        Route::get('/orders/{id}', [AdminOrderController::class, 'show']);
        Route::post('/orders/{id}/status', [AdminOrderController::class, 'updateStatus']);

        Route::get('/platform-orders', [PlatformOrderController::class, 'index']);
        Route::get('/platform-orders/export', [PlatformOrderController::class, 'export']);

        // 批次详情（可复盘）：BAS/BMPA run_id → last_result + 一键治理入口
        Route::get('/platform-batches/show', [\App\Http\Controllers\Admin\PlatformBatchController::class, 'show']);
        Route::get('/platform-orders/create', [PlatformOrderController::class, 'create']);
        Route::post('/platform-orders', [PlatformOrderController::class, 'store']);
        Route::post('/platform-orders/batch-activate-subscriptions', [PlatformOrderController::class, 'batchActivateSubscriptions']);
        Route::post('/platform-orders/batch-mark-paid-and-activate', [PlatformOrderController::class, 'batchMarkPaidAndActivate']);
        Route::post('/platform-orders/batch-mark-activated', [PlatformOrderController::class, 'batchMarkActivated']);
        Route::post('/platform-orders/clear-sync-errors', [PlatformOrderController::class, 'clearSyncErrors']);
        Route::post('/platform-orders/clear-bmpa-errors', [PlatformOrderController::class, 'clearBmpaErrors']);
        Route::get('/platform-orders/{order}', [PlatformOrderController::class, 'show']);
        Route::get('/platform-orders/{order}/export-ledger', [PlatformOrderController::class, 'exportLedger']);
        Route::post('/platform-orders/{order}/activate-subscription', [PlatformOrderController::class, 'activateSubscription']);
        Route::post('/platform-orders/{order}/mark-paid-and-activate', [PlatformOrderController::class, 'markPaidAndActivate']);
        Route::post('/platform-orders/{order}/add-payment-receipt', [PlatformOrderController::class, 'addPaymentReceipt']);
        Route::post('/platform-orders/{order}/add-refund-receipt', [PlatformOrderController::class, 'addRefundReceipt']);
        Route::post('/platform-orders/{order}/mark-refunded', [PlatformOrderController::class, 'markRefunded']);
        Route::post('/platform-orders/{order}/mark-partially-refunded', [PlatformOrderController::class, 'markPartiallyRefunded']);
        Route::post('/platform-orders/{order}/mark-paid-status', [PlatformOrderController::class, 'markPaidStatus']);
        Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']);
        // 治理动作：续费缺订阅时，手工绑定订阅（补齐闭环）
        Route::post('/platform-orders/{order}/attach-subscription', [PlatformOrderController::class, 'attachSubscription']);
        Route::post('/platform-orders/{order}/clear-sync-error', [PlatformOrderController::class, 'clearSyncError']);
        Route::post('/platform-orders/{order}/clear-bmpa-error', [PlatformOrderController::class, 'clearBmpaError']);

        Route::get('/site-subscriptions', [SiteSubscriptionController::class, 'index']);
        Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);
        Route::get('/site-subscriptions/{subscription}', [SiteSubscriptionController::class, 'show']);
        Route::post('/site-subscriptions/{subscription}/set-status', [SiteSubscriptionController::class, 'setStatus']);
        Route::post('/site-subscriptions/batch-mark-expired', [SiteSubscriptionController::class, 'batchMarkExpired']);

        Route::get('/plans', [PlanController::class, 'index']);
        Route::get('/plans/export', [PlanController::class, 'export']);
        Route::get('/plans/create', [PlanController::class, 'create']);
        Route::post('/plans', [PlanController::class, 'store']);
        // 注意：必须放在 /plans/{plan} 之前，避免被参数路由吞掉导致 404
        Route::post('/plans/seed-defaults', [PlanController::class, 'seedDefaults']);
        Route::get('/plans/{plan}/edit', [PlanController::class, 'edit']);
        Route::post('/plans/{plan}', [PlanController::class, 'update']);
        Route::post('/plans/{plan}/set-status', [PlanController::class, 'setStatus']);
        Route::post('/plans/{plan}/set-published', [PlanController::class, 'setPublished']);

        Route::get('/settings/system', [PlatformSettingController::class, 'system']);
        Route::post('/settings/system/{id}', [PlatformSettingController::class, 'updateSystem']);
        Route::get('/settings/channels', [PlatformSettingController::class, 'channels']);
        Route::post('/settings/channels/{id}', [PlatformSettingController::class, 'updateChannel']);
        Route::post('/settings/payments/{id}', [PlatformSettingController::class, 'updatePayment']);

        Route::get('/platform-leads', [\App\Http\Controllers\Admin\PlatformLeadController::class, 'index']);
        Route::post('/platform-leads/{lead}/set-status', [\App\Http\Controllers\Admin\PlatformLeadController::class, 'setStatus']);

        // 客服中心（工单）- 先做骨架与扩展点埋口
        Route::get('/support-tickets', [SupportTicketController::class, 'index']);
    });
});

Route::prefix('merchant-admin')->group(function () {
    Route::get('/login', [MerchantAdminAuthController::class, 'showLogin']);
    Route::post('/login', [MerchantAdminAuthController::class, 'login']);
    Route::post('/logout', [MerchantAdminAuthController::class, 'logout']);

    Route::middleware('merchant.admin.auth')->group(function () {
        Route::get('/', [MerchantDashboardController::class, 'index']);

        Route::get('/products', [MerchantProductController::class, 'index']);
        Route::get('/products/import-histories', [MerchantProductController::class, 'importHistories']);
        Route::get('/products/import-histories/export', [MerchantProductController::class, 'exportImportHistories']);
        Route::get('/products/export', [MerchantProductController::class, 'export']);
        Route::get('/products/import-template', [MerchantProductController::class, 'downloadImportTemplate']);
        Route::post('/products/import', [MerchantProductController::class, 'import']);
        Route::get('/products/import-failures/{file}', [MerchantProductController::class, 'downloadImportFailures']);
        Route::post('/products/batch', [MerchantProductController::class, 'batchUpdate']);
        Route::post('/products', [MerchantProductController::class, 'store']);
        Route::post('/products/{id}', [MerchantProductController::class, 'update']);
        Route::post('/products/{id}/delete', [MerchantProductController::class, 'destroy']);

        Route::get('/product-categories', [MerchantProductCategoryController::class, 'index']);
        Route::post('/product-categories', [MerchantProductCategoryController::class, 'store']);
        Route::post('/product-categories/{id}', [MerchantProductCategoryController::class, 'update']);
        Route::post('/product-categories/{id}/delete', [MerchantProductCategoryController::class, 'destroy']);

        Route::get('/users', [MerchantUserController::class, 'index']);

        Route::get('/orders', [MerchantOrderController::class, 'index']);
        Route::get('/orders/export', [MerchantOrderController::class, 'export']);
        Route::get('/orders/{id}', [MerchantOrderController::class, 'show']);
        Route::post('/orders/{id}/status', [MerchantOrderController::class, 'updateStatus']);
    });
});

Route::prefix('site-admin')->group(function () {
    Route::get('/login', [SiteAdminAuthController::class, 'showLogin']);
    Route::post('/login', [SiteAdminAuthController::class, 'login']);
    Route::post('/logout', [SiteAdminAuthController::class, 'logout']);

    Route::middleware('site.admin.auth')->group(function () {
        Route::get('/', [SiteDashboardController::class, 'index']);
        Route::get('/merchants', [SiteMerchantController::class, 'index']);
        Route::get('/merchants/export', [SiteMerchantController::class, 'export']);
        Route::get('/products', [SiteProductController::class, 'index']);
        Route::get('/products/export', [SiteProductController::class, 'export']);
        Route::get('/orders', [SiteOrderController::class, 'index']);
        Route::get('/orders/export', [SiteOrderController::class, 'export']);
    });
});
