<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\BkashTokenizedController;
use App\Http\Controllers\PaymentLinkController;
use App\Http\Controllers\LicenseController;
use App\Http\Controllers\InstallController;

// Root URL Redirection
Route::get('/', function () {
    if (!file_exists(storage_path('installed'))) {
        return redirect()->route('install.index');
    }
    return redirect('/admin');
});

// Web Installer Wizard
Route::prefix('install')->name('install.')->group(function () {
    Route::get('/', [InstallController::class, 'index'])->name('index');
    Route::get('/database', [InstallController::class, 'database'])->name('database');
    Route::post('/database', [InstallController::class, 'saveDatabase'])->name('database.submit');
    Route::get('/admin', [InstallController::class, 'admin'])->name('admin');
    Route::post('/admin', [InstallController::class, 'saveAdmin'])->name('admin.submit');
    Route::get('/complete', [InstallController::class, 'complete'])->name('complete');
});

// Public Payment Checkout
Route::get('/pay/{transaction}', [PaymentController::class, 'show'])->name('checkout.show');
Route::post('/pay/{transaction}/gateway', [PaymentController::class, 'setGateway'])->name('checkout.gateway');
Route::post('/pay/{transaction}/reset-gateway', [PaymentController::class, 'resetGateway'])->name('checkout.reset_gateway');
Route::post('/pay/{transaction}/submit', [PaymentController::class, 'submit'])->name('checkout.submit');

// bKash Tokenized/API Routes
Route::get('/pay/{transaction}/bkash/initiate', [BkashTokenizedController::class, 'initiate'])->name('bkash.initiate');
Route::get('/pay/{transaction}/bkash/callback', [BkashTokenizedController::class, 'callback'])->name('bkash.callback');

// Payment Links
Route::get('/link/{slug}', [PaymentLinkController::class, 'show'])->name('payment.link');
Route::get('/p/{slug}', [PaymentLinkController::class, 'show']);

// Maintenance & Optimization Helpers
Route::get('/clear-cache', function() {
    Artisan::call('optimize:clear');
    return 'Cache cleared successfully!';
});

Route::get('/storage-link', function() {
    Artisan::call('storage:link');
    return 'Storage linked successfully!';
});

// Manual License Activation Route
Route::get('/license/activate', [LicenseController::class, 'showActivateForm'])->name('license.activate');
Route::post('/license/activate', [LicenseController::class, 'activate'])->name('license.activate.submit');