<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PackageController;
use App\Http\Controllers\BookingController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\Admin\DestinationController;
use App\Http\Controllers\Admin\TestimonialController;
use App\Http\Controllers\Admin\PostController;
use App\Http\Controllers\Admin\FeatureController;
use App\Http\Controllers\BannerController;
// Frontend Routes
Route::get('/robots.txt', [\App\Http\Controllers\SitemapController::class, 'robots'])->name('robots');
Route::get('/sitemap.xml', [\App\Http\Controllers\SitemapController::class, 'index'])->name('sitemap');

Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/about', [HomeController::class, 'about'])->name('about');
Route::get('/contact', [HomeController::class, 'contact'])->name('contact');
Route::post('/contact', [HomeController::class, 'contactSubmit'])->name('contact.submit');

// Package Routes
Route::get('/packages', [PackageController::class, 'index'])->name('packages.index');
Route::get('/packages/{slug}', [PackageController::class, 'show'])->name('packages.show');
Route::get('/packages/{slug}/book', [PackageController::class, 'book'])->name('packages.book');

// Destination Routes
Route::get('/destinations', [HomeController::class, 'destinations'])->name('destinations');

// Booking Routes
Route::post('/bookings', [BookingController::class, 'store'])->name('bookings.store');
Route::get('/booking/success/{reference}', [BookingController::class, 'success'])->name('booking.success');
Route::get('/booking/failed/{reference}', [BookingController::class, 'failed'])->name('booking.failed');

// Offline Payment Routes
Route::prefix('offline-payment')->name('offline-payment.')->group(function () {
    Route::get('/{reference}', [\App\Http\Controllers\OfflinePaymentController::class, 'show'])->name('show');
    Route::post('/{reference}/submit', [\App\Http\Controllers\OfflinePaymentController::class, 'submit'])->name('submit');
    Route::get('/{reference}/pending', [\App\Http\Controllers\OfflinePaymentController::class, 'pending'])->name('pending');
    Route::get('/{reference}/status', [\App\Http\Controllers\OfflinePaymentController::class, 'checkStatus'])->name('status');
});

// Payment Routes
Route::prefix('payment')->name('payment.')->group(function () {
    Route::post('/{reference}/initialize', [PaymentController::class, 'initializePayment'])->name('initialize');
    Route::get('/callback', [PaymentController::class, 'handleCallback'])->name('callback');
    Route::get('/methods', [PaymentController::class, 'paymentMethods'])->name('methods');
});

// Webhook Routes (CSRF exemption in VerifyCsrfToken middleware)
Route::post('/api/webhooks/flutterwave', [PaymentController::class, 'handleWebhook'])->name('webhook.flutterwave');

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

Route::middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
    Route::get('/profile', [UserController::class, 'profile'])->name('user.profile');
    Route::put('/profile', [UserController::class, 'updateProfile'])->name('user.profile.update');
    Route::post('/profile/password', [UserController::class, 'updatePassword'])->name('user.profile.password');
    Route::post('/profile/avatar', [UserController::class, 'updateAvatar'])->name('user.profile.avatar');
    Route::get('/bookings', [UserController::class, 'bookings'])->name('user.bookings');
    Route::get('/bookings/{reference}', [UserController::class, 'bookingDetail'])->name('user.booking.detail');
    Route::post('/bookings/{reference}/cancel', [UserController::class, 'cancelBooking'])->name('user.booking.cancel');
    Route::get('/favorites', [UserController::class, 'favorites'])->name('user.favorites');
    Route::post('/favorites/{packageId}/remove', [UserController::class, 'removeFromFavorites'])->name('user.favorites.remove');

    Route::get('/notifications', [UserController::class, 'notifications'])->name('user.notifications');
    Route::post('/notifications/{id}/read', [UserController::class, 'markNotificationAsRead'])->name('user.notifications.read');
    Route::post('/notifications/{id}/delete', [UserController::class, 'deleteNotification'])->name('user.notifications.delete');
    Route::get('/bookings/{reference}/invoice', [UserController::class, 'downloadInvoice'])->name('user.booking.invoice');
    Route::get('/bookings/{reference}/ticket', [UserController::class, 'downloadTicket'])->name('user.booking.ticket');
});

// =============================================
// OFFICIAL ADMIN ROUTES
// =============================================
// Primary admin access point: /admin/dashboard
Route::middleware(['auth', 'role:admin'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
    // Official Admin Dashboard Route
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
    
    // Package Management
    Route::get('/packages', [AdminController::class, 'packages'])->name('packages');
    Route::get('/packages/create', [AdminController::class, 'createPackage'])->name('packages.create');
    Route::post('/packages', [AdminController::class, 'storePackage'])->name('packages.store');
    Route::get('/packages/{id}/edit', [AdminController::class, 'editPackage'])->name('packages.edit');
    Route::put('/packages/{id}', [AdminController::class, 'updatePackage'])->name('packages.update');
    Route::delete('/packages/{id}', [AdminController::class, 'deletePackage'])->name('packages.delete');
    
    // Booking Management
    Route::get('/bookings', [AdminController::class, 'bookings'])->name('bookings');
    Route::get('/bookings/{reference}', [AdminController::class, 'bookingDetail'])->name('bookings.detail');
    Route::post('/bookings/{reference}/confirm', [AdminController::class, 'confirmBooking'])->name('bookings.confirm');
    Route::post('/bookings/{reference}/cancel', [AdminController::class, 'cancelBooking'])->name('bookings.cancel');
    
    // Payment Management
    Route::get('/payments', [AdminController::class, 'payments'])->name('payments');
    Route::get('/payments/{reference}', [AdminController::class, 'paymentDetail'])->name('payments.detail');
    Route::post('/payments/{reference}/confirm', [AdminController::class, 'confirmPayment'])->name('payments.confirm');
    Route::post('/payments/{reference}/reject', [AdminController::class, 'rejectPayment'])->name('payments.reject');

    // Offline Payment Management
    Route::get('/offline-payments', [AdminController::class, 'offlinePayments'])->name('offline-payments.index');
    Route::get('/offline-payments/{id}', [AdminController::class, 'offlinePaymentDetail'])->name('offline-payments.detail');
    Route::post('/offline-payments/{id}/approve', [AdminController::class, 'approveOfflinePayment'])->name('offline-payments.approve');
    Route::post('/offline-payments/{id}/reject', [AdminController::class, 'rejectOfflinePayment'])->name('offline-payments.reject');
    Route::get('/offline-payments-settings', [AdminController::class, 'offlinePaymentSettings'])->name('offline-payments.settings');
    Route::put('/offline-payments-settings', [AdminController::class, 'updateOfflinePaymentSettings'])->name('offline-payments.settings.update');
    
    // User Management
    Route::get('/users', [AdminController::class, 'users'])->name('users');
    Route::get('/users/{id}', [AdminController::class, 'userDetail'])->name('users.detail');
    
    // Banner Management
    Route::get('/banners', [BannerController::class, 'index'])->name('banners');
    Route::get('/banners/create', [BannerController::class, 'create'])->name('banners.create');
    Route::post('/banners', [BannerController::class, 'store'])->name('banners.store');
    Route::get('/banners/{id}/edit', [BannerController::class, 'edit'])->name('banners.edit');
    Route::put('/banners/{id}', [BannerController::class, 'update'])->name('banners.update');
    Route::delete('/banners/{id}', [BannerController::class, 'destroy'])->name('banners.destroy');
    Route::post('/banners/{id}/toggle', [BannerController::class, 'toggleStatus'])->name('banners.toggle');
    
    // Settings
    Route::get('/settings', [AdminController::class, 'settings'])->name('settings');
    Route::put('/settings', [AdminController::class, 'updateSettings'])->name('settings.update');

    // SEO Settings
    Route::get('/seo', [\App\Http\Controllers\Admin\SeoController::class, 'index'])->name('seo');
    Route::put('/seo', [\App\Http\Controllers\Admin\SeoController::class, 'update'])->name('seo.update');

    // Dynamic Features Management
    Route::resource('destinations', DestinationController::class);
    Route::resource('testimonials', TestimonialController::class);
    Route::resource('posts', PostController::class);
    Route::resource('features', FeatureController::class);
});
