<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\PenggunaController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\BarangController;
use App\Http\Controllers\PaketController;
use App\Http\Controllers\SewaController;
use App\Http\Controllers\RiwayatController;
use App\Http\Controllers\StockController;
use App\Http\Controllers\PaketBarangController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\OngkirKotaController;
use App\Http\Controllers\ContactInfoController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// Route untuk halaman welcome

Route::get('/', function () {
    $pakets = \App\Models\Paket::where('status', 'aktif')->get();
    return view('welcome', compact('pakets'));
});



// Route untuk sewa yang bisa diakses tanpa login
Route::get('/sewa', [SewaController::class, 'index'])->name('sewa.index');

// Route untuk Authentication (untuk guest)
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
    Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.post');
});

// Route untuk logout
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Route untuk pembatalan pesanan
Route::middleware(['auth'])->group(function () {
    Route::get('/riwayat/cancel/{id}', [RiwayatController::class, 'showCancelForm'])->name('riwayat.cancel-form');
    Route::put('/riwayat/cancel/{id}/process', [RiwayatController::class, 'processCancel'])->name('riwayat.process-cancel');
    
    // Chat Routes
    Route::get('/chat', [ChatController::class, 'index'])->name('chat.index');
    Route::get('/chat/{user_id}', [ChatController::class, 'show'])->name('chat.show');
    Route::post('/chat/{user_id}', [ChatController::class, 'store'])->name('chat.store');
    Route::get('/chat/{user_id}/messages', [ChatController::class, 'getMessages'])->name('chat.messages');
    Route::get('/chat/unread-count', [ChatController::class, 'getUnreadCount'])->name('chat.unreadCount');
    Route::post('/chat/{user_id}/mark-as-read', [ChatController::class, 'markAsRead'])->name('chat.markAsRead');
});

// Route yang membutuhkan autentikasi
Route::middleware(['auth'])->group(function () {
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Profile
    Route::get('/profile', [ProfileController::class, 'index'])->name('profile.index');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');

    // Pengguna
    Route::get('/pengguna', [PenggunaController::class, 'index'])->name('pengguna');
    Route::get('/pengguna/tambah', [PenggunaController::class, 'create'])->name('pengguna.create');
    Route::post('/pengguna', [PenggunaController::class, 'store'])->name('pengguna.store');
    Route::delete('/pengguna/{id}', [PenggunaController::class, 'destroy'])->name('pengguna.destroy');

    // Admin
    Route::get('/admin', [AdminController::class, 'index'])->name('admin');
    Route::get('/admin/tambah', [AdminController::class, 'create'])->name('admin.create');
    Route::post('/admin', [AdminController::class, 'store'])->name('admin.store');
    Route::get('/admin/{id}/edit', [AdminController::class, 'edit'])->name('admin.edit');
    Route::put('/admin/{id}', [AdminController::class, 'update'])->name('admin.update');
    Route::delete('/admin/{id}', [AdminController::class, 'destroy'])->name('admin.destroy');

    // Barang
    Route::get('/barang', [BarangController::class, 'index'])->name('barang');
    Route::get('/barang/tambah', [BarangController::class, 'create'])->name('barang.create');
    Route::post('/barang', [BarangController::class, 'store'])->name('barang.store');
    Route::get('/barang/{id}/edit', [BarangController::class, 'edit'])->name('barang.edit');
    Route::put('/barang/{id}', [BarangController::class, 'update'])->name('barang.update');
    Route::delete('/barang/{id}', [BarangController::class, 'destroy'])->name('barang.destroy');

    // Paket (untuk semua user)
    Route::get('/paket', [PaketController::class, 'index'])->name('paket.index');
    
    // Khusus Admin
    Route::middleware(['admin'])->group(function () {
        // Paket Management
    Route::get('/paket/tambah', [PaketController::class, 'create'])->name('paket.create');
    Route::post('/paket', [PaketController::class, 'store'])->name('paket.store');
    Route::get('/paket/{id}/edit', [PaketController::class, 'edit'])->name('paket.edit');
    Route::put('/paket/{id}', [PaketController::class, 'update'])->name('paket.update');
    Route::delete('/paket/{id}', [PaketController::class, 'destroy'])->name('paket.destroy');
        Route::post('/paket/{id}/activate', [PaketController::class, 'activate'])->name('paket.activate');
        Route::post('/paket/{id}/activate-all', [PaketController::class, 'activateAll'])->name('paket.activate-all');
    });

    // Input Stock
    Route::get('/input-stock', [StockController::class, 'index'])->name('input-stock');
    Route::post('/input-stock', [StockController::class, 'store'])->name('input-stock.store');
    Route::get('/input-stock/{id}/edit', [StockController::class, 'edit'])->name('input-stock.edit');
    Route::put('/input-stock/{id}', [StockController::class, 'update'])->name('input-stock.update');
    Route::get('/input-stock/{id}/kelola-barang', [StockController::class, 'kelolaBarang'])->name('input-stock.kelola-barang');
    Route::get('/input-stock/{id}/barang', [StockController::class, 'getBarangPaket'])->name('input-stock.barang');
    Route::post('/input-stock/{id}/barang', [StockController::class, 'tambahBarang'])->name('input-stock.tambah-barang');
    Route::delete('/input-stock/{paketId}/barang/{barangId}', [StockController::class, 'hapusBarang'])->name('input-stock.hapus-barang');

    // PaketBarang
    Route::put('/paket-barang/{id}', [PaketBarangController::class, 'update'])->name('paket-barang.update');
    Route::delete('/paket-barang/{id}', [PaketBarangController::class, 'destroy'])->name('paket-barang.destroy');

    // Sewa Routes
    Route::prefix('sewa')->name('sewa.')->group(function () {
        Route::get('/create/{id}', [SewaController::class, 'create'])->name('create');
        Route::post('/', [SewaController::class, 'store'])->name('store');
        Route::get('/{id}', [SewaController::class, 'show'])->name('show');
        Route::post('/{id}/upload-bukti', [SewaController::class, 'uploadBukti'])->name('upload-bukti');
        Route::post('/{id}/upload-jaminan', [SewaController::class, 'uploadJaminan'])->name('upload-jaminan');
        Route::post('/{id}/batal', [SewaController::class, 'batal'])->name('batal');
        Route::get('/riwayat', [SewaController::class, 'riwayat'])->name('riwayat');
    });

    // Riwayat untuk customer
    Route::get('/riwayat', [RiwayatController::class, 'index'])->name('riwayat');
    Route::get('/riwayat/{id}', [RiwayatController::class, 'show'])->name('riwayat.show');
    Route::put('/riwayat/{id}/batal', [RiwayatController::class, 'cancel'])->name('riwayat.batal');
    Route::delete('/riwayat/{id}/hapus', [RiwayatController::class, 'hapus'])->name('riwayat.hapus');

    // Admin Routes
    Route::middleware(['admin'])->prefix('admin')->name('admin.')->group(function () {
        // Riwayat Routes untuk admin
        Route::get('/riwayat', [RiwayatController::class, 'adminIndex'])->name('riwayat');
        Route::get('/riwayat/{id}', [RiwayatController::class, 'show'])->name('riwayat.show');
        Route::put('/riwayat/{id}/status', [RiwayatController::class, 'updateStatus'])->name('riwayat.status');
        Route::get('/riwayat/{id}/update-payment', [RiwayatController::class, 'showUpdatePaymentForm'])->name('riwayat.show-update-payment');
        // Route::put('/riwayat/{id}/update-payment', [RiwayatController::class, 'updatePayment'])->name('riwayat.update-payment');
        // Route::post('/riwayat/update-status/{id}', [RiwayatController::class, 'updateStatus'])->name('riwayat.updateStatus');
        Route::post('/riwayat/update-status/{id}', [\App\Http\Controllers\RiwayatController::class, 'updateStatus'])->name('riwayat.updateStatus');
        Route::delete('/riwayat/hapus/{id}', [\App\Http\Controllers\RiwayatController::class, 'hapus']);
        Route::get('/riwayat', [\App\Http\Controllers\RiwayatController::class, 'index'])->name('riwayat');
        
        
        // Verifikasi Pembayaran
        Route::get('/verifikasi', [App\Http\Controllers\Admin\VerifikasiController::class, 'index'])->name('verifikasi.index');
        Route::post('/verifikasi/{id}/approve', [App\Http\Controllers\Admin\VerifikasiController::class, 'approve'])->name('verifikasi.approve');
        Route::post('/verifikasi/{id}/reject', [App\Http\Controllers\Admin\VerifikasiController::class, 'reject'])->name('verifikasi.reject');

        // Laporan Transaksi
        Route::get('/reports/transactions', [App\Http\Controllers\Admin\ReportController::class, 'transactions'])->name('reports.transactions');
    });

    // Chat Routes
    Route::get('/chat/latest-messages/{user_id}', [ChatController::class, 'getLatestMessages'])->name('chat.latestMessages');
    Route::get('/chat/{user_id}/messages', [ChatController::class, 'getNewMessages'])->name('chat.messages');
});

// Reroute dari dashboard/users ke pengguna
Route::get('/dashboard/users', function () {
    return redirect()->route('pengguna');
})->name('dashboard.users');

// Admin Routes
Route::middleware(['auth', 'role:admin'])->prefix('admin')->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('admin.dashboard');
    // ... other admin routes ...
});

// Customer Routes
Route::middleware(['auth', 'role:customer'])->prefix('customer')->group(function () {
    Route::get('/dashboard', [App\Http\Controllers\Customer\DashboardController::class, 'index'])->name('customer.dashboard');
    
    // Paket - hanya view
    Route::get('/paket', [PaketController::class, 'index'])->name('customer.paket');
    
    // Sewa
    Route::get('/sewa', [SewaController::class, 'index'])->name('customer.sewa');
    Route::get('/sewa/create/{id}', [SewaController::class, 'create'])->name('customer.sewa.create');
    Route::post('/sewa', [SewaController::class, 'store'])->name('customer.sewa.store');
    Route::get('/sewa/{id}', [SewaController::class, 'show'])->name('customer.sewa.show');
    Route::post('/sewa/{id}/upload-bukti', [SewaController::class, 'uploadBukti'])->name('customer.sewa.upload-bukti');
    
    // Riwayat
    Route::get('/riwayat', [RiwayatController::class, 'index'])->name('customer.riwayat');
    Route::get('/riwayat/{id}', [RiwayatController::class, 'show'])->name('customer.riwayat.show');
    Route::put('/riwayat/{id}/cancel', [RiwayatController::class, 'cancel'])->name('customer.riwayat.cancel');
});

// Routes untuk manajemen ongkir kota (admin only)
Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::resource('ongkir-kota', OngkirKotaController::class);
});

// Routes untuk Hubungi Kami
Route::get('/contact', [ContactInfoController::class, 'show'])->name('contact.show');
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/admin/contact', [ContactInfoController::class, 'index'])->name('contact.index');
    Route::post('/admin/contact', [ContactInfoController::class, 'store'])->name('contact.store');
});

Route::get('/paket/{id}/detail', [PaketController::class, 'detail'])->name('paket.detail');

// Route untuk data pengguna biasa
Route::get('/users', [UserController::class, 'index'])->name('users');
Route::delete('/users/{id}', [UserController::class, 'destroy'])->name('users.destroy');

Route::put('/admin/riwayat/{id}/update-status', [App\Http\Controllers\Admin\ReportController::class, 'updateStatus'])->name('admin.riwayat.update-status');
