<?php

use App\Http\Controllers\AdPlatformController;
use App\Http\Controllers\AdsPlacementController;
use App\Http\Controllers\Auth\GoogleController;
use App\Http\Controllers\BankController;
use App\Http\Controllers\BillingController;
use App\Http\Controllers\CampaignController;
use App\Http\Controllers\CreditHistoryController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DuitkuCallbackController;
use App\Http\Controllers\GroupController;
use App\Http\Controllers\KategoriController;
use App\Http\Controllers\LandingController;
use App\Http\Controllers\MenuController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

// Route::inertia('/', 'Welcome')->name('home');

Route::get('/', function () {
    if (Auth::check()) {
        return redirect('/dashboard');
    } else {
        return redirect('/login');
    }
})->name('home');

// Login Google (Socialite) — publik.
Route::get('auth/google/redirect', [GoogleController::class, 'redirect'])->name('auth.google.redirect');
Route::get('auth/google/callback', [GoogleController::class, 'callback'])->name('auth.google.callback');

// Area publik: rotator iklan (FR-ROT-01). Tidak perlu login.
// `go` dipesan untuk endpoint klik; alias tidak boleh bernilai "go".
Route::get('landing/go/{ad}', [LandingController::class, 'go'])->name('landing.go');
Route::get('landing/{alias}/{placement?}', [LandingController::class, 'index'])
    ->where('alias', '(?!go$)[A-Za-z0-9-]+')
    ->name('landing.show');

// Callback gateway Duitku — publik, CSRF-exempt (lihat bootstrap/app.php).
Route::post('payments/duitku/callback', DuitkuCallbackController::class)->name('payments.duitku.callback');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('dashboard', DashboardController::class)->name('dashboard');

    // Campaign (iklan) — admin & member.
    // `drafts` sebelum resource agar tidak tertangkap sebagai {campaign} show.
    Route::get('campaigns/drafts', [CampaignController::class, 'drafts'])->name('campaigns.drafts');
    Route::resource('campaigns', CampaignController::class);
    Route::get('campaigns/{campaign}/approve', [CampaignController::class, 'approve'])->name('campaigns.approve');
    Route::get('campaigns/{campaign}/draft', [CampaignController::class, 'draft'])->name('campaigns.draft');
    Route::get('campaigns/{campaign}/restore', [CampaignController::class, 'restore'])->name('campaigns.restore');
    Route::get('campaigns/{campaign}/stop', [CampaignController::class, 'stop'])->name('campaigns.stop');
    Route::get('campaigns/{campaign}/start', [CampaignController::class, 'start'])->name('campaigns.start');
    Route::get('campaigns/{campaign}/verify', [CampaignController::class, 'verify'])->name('campaigns.verify');
    Route::get('campaigns-export', [CampaignController::class, 'export'])->name('campaigns.export');

    // Billing — admin kelola, member lihat.
    Route::resource('billings', BillingController::class);
    Route::get('billings/{billing}/confirm', [BillingController::class, 'confirm'])->name('billings.confirm');

    // Kembali dari halaman pembayaran Duitku. Tidak kredit — callback adalah source-of-truth.
    Route::get('payments/duitku/return', [BillingController::class, 'duitkuReturn'])->name('payments.duitku.return');

    // Riwayat credit — admin & member.
    Route::get('credits', [CreditHistoryController::class, 'index'])->name('credits.index');

    // Master data — admin/developer.
    Route::middleware('group:admin,developer')->group(function () {
        Route::get('kategoris/{kategori}/report', [KategoriController::class, 'report'])->name('kategoris.report');
        Route::resource('kategoris', KategoriController::class)->except('show');
        Route::resource('placements', AdsPlacementController::class)->except('show');
        Route::resource('banks', BankController::class)->except('show');

        // Sistem — admin/developer.
        Route::resource('users', UserController::class)->except('show');
        Route::resource('groups', GroupController::class)->except('show');
        Route::resource('menus', MenuController::class)->except('show');
        Route::resource('ad-platforms', AdPlatformController::class)->except(['show', 'create', 'edit']);
    });
});

require __DIR__.'/settings.php';
