<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\UmkmController;
use App\Http\Controllers\AdminAccountController;
use App\Http\Controllers\SupportController;
use App\Models\BudgetPlan;
use App\Models\Payment;
use App\Models\Transaction;
use App\Models\Umkm;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes khusus untuk Internal Admin Panel (Laravel Breeze)
|--------------------------------------------------------------------------
*/
Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    $umkms = Umkm::latest()->get();

    $totalModal = $umkms->sum('modal_awal');
    $totalMasuk = Transaction::where('tipe', 'masuk')->sum('jumlah');
    $totalKeluar = Transaction::where('tipe', 'keluar')->sum('jumlah');
    $totalPemasukan = Payment::where('payment_status', 'paid')->sum('amount');
    $terisiFrontend = $umkms->whereNotNull('user_id')->count();
    $inputManual = $umkms->whereNull('user_id')->count();
    $budgetPlan = BudgetPlan::where('user_id', auth()->id())->first();

    return view('dashboard', [
        'totalUmkm'       => $umkms->count(),
        'totalModal'      => $totalModal,
        'totalPemasukan'  => $totalPemasukan,
        'terisiFrontend'  => $terisiFrontend,
        'inputManual'     => $inputManual,
        'totalMasuk'      => $totalMasuk,
        'totalKeluar'     => $totalKeluar,
        'budgetPlan'      => $budgetPlan,
        'recentUmkms'     => $umkms->take(5),
    ]);
})->middleware(['auth', 'verified'])->name('dashboard');

// Semua rute di dalam grup ini wajib login via Web (Khusus internal Admin)
Route::middleware('auth')->group(function () {
    // Rute Profil Bawaan Breeze
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // Rute Monitoring CRUD UMKM untuk Admin (Melihat, Mengubah, Menghapus Data)
    Route::resource('umkm', UmkmController::class);

    // Rute Kelola Akun SaaS (Daftar akun dari registrasi / lynk.id webhook)
    Route::get('/accounts', [AdminAccountController::class, 'index'])->name('accounts.index');
    Route::get('/accounts/create', fn () => redirect()->route('accounts.index'));
    Route::delete('/accounts/{account}', [AdminAccountController::class, 'destroy'])->name('accounts.destroy');

    // Rute Pesan Dukungan (Support Ticket dari Pengguna)
    Route::get('/support', [SupportController::class, 'index'])->name('support.index');
    Route::get('/support/{contact}', [SupportController::class, 'show'])->name('support.show');
    Route::post('/support/{contact}/reply', [SupportController::class, 'reply'])->name('support.reply');
    Route::post('/support/{contact}/close', [SupportController::class, 'close'])->name('support.close');
});

require __DIR__.'/auth.php';