<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminAuthController;

use App\Models\Game;

Route::get('/', function () {
    $games = Game::where('is_active', true)->get();
    $banners = \App\Models\Banner::where('is_active', true)->orderBy('order_num', 'asc')->get();
    $homepage = \App\Models\Setting::where('group', 'homepage')->pluck('value', 'key');
    
    // Ambil artikel yg di-featured admin untuk ditampilkan di banner
    $featured_articles = \App\Models\Article::where('is_published', true)->where('is_featured', true)->get();
    
    // Always Generate Simple Math CAPTCHA every load
    $n1 = rand(1, 9);
    $n2 = rand(1, 9);
    session(['captcha_result' => $n1 + $n2]);
    $captcha_question = "$n1 + $n2 = ?";

    return view('welcome', compact('games', 'banners', 'homepage', 'captcha_question', 'featured_articles'));
});

// Halaman Kumpulan Semua Artikel
Route::get('/articles', function () {
    $articles = \App\Models\Article::where('is_published', true)->latest()->get();
    return view('articles', compact('articles'));
});

// Halaman Detail 1 Artikel
Route::get('/article/{id}', function ($id) {
    $article = \App\Models\Article::findOrFail($id);
    return view('article_detail', compact('article'));
});

// =====================================================
// ADMIN AUTH ROUTES (tidak perlu login untuk akses ini)
// =====================================================
Route::get('/admin/login', [AdminAuthController::class, 'showLogin'])->name('admin.login');
Route::post('/admin/login', [AdminAuthController::class, 'processLogin'])->name('admin.login.post');
Route::post('/admin/logout', [AdminAuthController::class, 'logout'])->name('admin.logout');

// =====================================================
// ADMIN PROTECTED ROUTES (wajib login admin)
// =====================================================
Route::middleware(\App\Http\Middleware\AdminAuthenticated::class)->group(function () {
    Route::get('/admin', function () {
        return view('admin');
    })->name('admin.dashboard');
});

// API ROUTES THAT NEED SESSIONS (Moved here from api.php)
Route::prefix('api/topup')->group(function () {
    Route::get('/transactions', [App\Http\Controllers\TransactionController::class, 'index']);
    Route::get('/feed', [App\Http\Controllers\TransactionController::class, 'getRecentFeed']); // New optimized feed
    Route::get('/transactions/new', [App\Http\Controllers\TransactionController::class, 'getNewTransactions']);
    Route::post('/validate-nickname', [App\Http\Controllers\TransactionController::class, 'validateNickname']);
    Route::post('/checkout', [App\Http\Controllers\TransactionController::class, 'checkout']);
    Route::post('/verify-captcha', [App\Http\Controllers\TransactionController::class, 'verifyCaptcha']);
    Route::get('/{orderId}', [App\Http\Controllers\TransactionController::class, 'show']);
    Route::get('/find-by-wa/{wa}', [App\Http\Controllers\TransactionController::class, 'findByWa']);
    Route::post('/pay', [App\Http\Controllers\TransactionController::class, 'checkout']);
    Route::delete('/{id}', [App\Http\Controllers\TransactionController::class, 'destroy']); // Delete route
});

Route::get('/invoice/{order_id}', [App\Http\Controllers\TransactionController::class, 'viewInvoice']);

Route::prefix('api/promos')->group(function () {
    Route::get('/', [App\Http\Controllers\PromoController::class, 'index']);
    Route::post('/', [App\Http\Controllers\PromoController::class, 'store']);
    Route::post('/check', [App\Http\Controllers\PromoController::class, 'check']);
    Route::post('/{id}', [App\Http\Controllers\PromoController::class, 'update']);
    Route::delete('/{id}', [App\Http\Controllers\PromoController::class, 'destroy']);
});

Route::prefix('api/articles')->group(function () {
    Route::get('/', [App\Http\Controllers\ArticleController::class, 'index']);
    Route::get('/published', [App\Http\Controllers\ArticleController::class, 'getPublished']);
    Route::get('/{id}', [App\Http\Controllers\ArticleController::class, 'show']);
    Route::post('/', [App\Http\Controllers\ArticleController::class, 'store']);
    Route::post('/{id}', [App\Http\Controllers\ArticleController::class, 'update']);
    Route::delete('/{id}', [App\Http\Controllers\ArticleController::class, 'destroy']);
});

Route::prefix('api/banners')->group(function () {
    Route::get('/', [App\Http\Controllers\BannerController::class, 'index']);
    Route::post('/', [App\Http\Controllers\BannerController::class, 'store']);
    Route::post('/{id}', [App\Http\Controllers\BannerController::class, 'update']);
    Route::delete('/{id}', [App\Http\Controllers\BannerController::class, 'destroy']);
});
