<?php

use App\Http\Controllers\Web\AdminController;
use App\Http\Controllers\Web\AdminUserController;
use App\Http\Controllers\Web\AdminPropertyController;
use App\Http\Controllers\Web\AdminBookingController;
use App\Http\Controllers\Web\AdminPaymentController;
use App\Http\Controllers\Web\AdminRentalAgreementController;
use App\Http\Controllers\Web\AdminReviewController;
use App\Http\Controllers\Web\AdminReportController;
use App\Http\Controllers\Web\AdminNotificationController;
use App\Http\Controllers\Web\AdminHostelController;
use App\Http\Controllers\Web\AdminMessageController;
use App\Http\Controllers\Web\AdminPaymentReceiptController;
use App\Http\Controllers\Web\AdminSettingsController;
use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\WebPropertyController;
use Illuminate\Support\Facades\Route;

// Root route - shows login or dashboard based on auth status
Route::get('/', function () {
    if (auth()->check()) {
        return app(AdminController::class)->dashboard();
    }
    return app(AdminController::class)->login();
})->name('home');

Route::post('/', [AdminController::class, 'authenticate'])->name('admin.authenticate');

// API Documentation routes (public - no authentication required)
Route::get('/apidocs', function () {
    return view('apidocs');
})->name('apidocs');

Route::get('/api-spec', function () {
    $path = base_path('openapi.json');
    if (!file_exists($path)) {
        abort(404, 'API specification not found');
    }
    
    // Load and decode the OpenAPI spec
    $spec = json_decode(file_get_contents($path), true);
    
    $baseUrl = request()->getSchemeAndHttpHost();
    $spec['servers'] = [
        [
            'url' => $baseUrl . '/api/v1',
            'description' => $baseUrl
        ]
    ];
    
    return response()->json($spec);
})->name('api-spec');

// Dashboard routes (JWT authentication only)
Route::prefix('dashboard')->middleware('auth:sanctum')->group(function () {
    Route::get('/profile', [DashboardController::class, 'profile']);
    Route::get('/analytics', [DashboardController::class, 'analytics']);
    Route::get('/properties', [DashboardController::class, 'properties']);
});

// Admin routes
Route::prefix('admin')->group(function () {
    Route::get('/login', [AdminController::class, 'login'])->name('admin.login');
    Route::post('/logout', [AdminController::class, 'logout'])->name('admin.logout');
    
    // Protected admin routes - Admin only
    Route::middleware(['auth', 'admin'])->group(function () {
        Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
        
        // User Management
        Route::get('/users', [AdminUserController::class, 'index'])->name('admin.users.index');
        Route::get('/users/create', [AdminUserController::class, 'create'])->name('admin.users.create');
        Route::post('/users', [AdminUserController::class, 'store'])->name('admin.users.store');
        Route::get('/users/{id}/edit', [AdminUserController::class, 'edit'])->name('admin.users.edit');
        Route::put('/users/{id}', [AdminUserController::class, 'update'])->name('admin.users.update');
        Route::delete('/users/{id}', [AdminUserController::class, 'destroy'])->name('admin.users.destroy');
        Route::get('/users/pending-landlords', [AdminUserController::class, 'pendingLandlords'])->name('admin.users.pending-landlords');
        Route::post('/users/{id}/approve', [AdminUserController::class, 'approveLandlord'])->name('admin.users.approve');
        Route::post('/users/{id}/reject', [AdminUserController::class, 'rejectLandlord'])->name('admin.users.reject');
        
        // Property Management
        Route::get('/properties', [AdminPropertyController::class, 'index'])->name('admin.properties.index');
        Route::get('/properties/create', [AdminPropertyController::class, 'create'])->name('admin.properties.create');
        Route::post('/properties', [AdminPropertyController::class, 'store'])->name('admin.properties.store');
        Route::get('/properties/landlords', [AdminPropertyController::class, 'getLandlords'])->name('admin.properties.landlords');
        Route::get('/properties/{id}/edit', [AdminPropertyController::class, 'edit'])->name('admin.properties.edit');
        Route::put('/properties/{id}', [AdminPropertyController::class, 'update'])->name('admin.properties.update');
        Route::delete('/properties/{id}', [AdminPropertyController::class, 'destroy'])->name('admin.properties.destroy');
        Route::post('/properties/{id}/verify', [AdminPropertyController::class, 'verifyProperty'])->name('admin.properties.verify');
        Route::post('/properties/{id}/status', [AdminPropertyController::class, 'updateStatus'])->name('admin.properties.status');
        Route::get('/properties/verifications', [AdminPropertyController::class, 'verifications'])->name('admin.properties.verifications');
        Route::get('/properties/verifications/statistics', [AdminPropertyController::class, 'verificationStatistics'])->name('admin.properties.verifications.statistics');
        
        // Booking Management
        Route::get('/bookings', [AdminBookingController::class, 'index'])->name('admin.bookings.index');
        Route::get('/bookings/{id}', [AdminBookingController::class, 'show'])->name('admin.bookings.show');
        Route::put('/bookings/{id}/status', [AdminBookingController::class, 'updateStatus'])->name('admin.bookings.updateStatus');
        Route::delete('/bookings/{id}', [AdminBookingController::class, 'destroy'])->name('admin.bookings.destroy');
        
        // Payment Management
        Route::get('/payments', [AdminPaymentController::class, 'index'])->name('admin.payments.index');
        Route::get('/payments/overdue', [AdminPaymentController::class, 'overduePayments'])->name('admin.payments.overdue');
        Route::get('/payments/due-today', [AdminPaymentController::class, 'dueTodayPayments'])->name('admin.payments.due-today');
        Route::get('/payments/{id}', [AdminPaymentController::class, 'show'])->name('admin.payments.show');
        Route::put('/payments/{id}/status', [AdminPaymentController::class, 'updateStatus'])->name('admin.payments.updateStatus');
        Route::delete('/payments/{id}', [AdminPaymentController::class, 'destroy'])->name('admin.payments.destroy');
        
        // Rental Agreement Management
        Route::get('/rental-agreements', [AdminRentalAgreementController::class, 'index'])->name('admin.rental-agreements.index');
        Route::get('/rental-agreements/{id}', [AdminRentalAgreementController::class, 'show'])->name('admin.rental-agreements.show');
        Route::put('/rental-agreements/{id}/toggle-status', [AdminRentalAgreementController::class, 'toggleStatus'])->name('admin.rental-agreements.toggleStatus');
        Route::delete('/rental-agreements/{id}', [AdminRentalAgreementController::class, 'destroy'])->name('admin.rental-agreements.destroy');
        
        // Review Management
        Route::get('/reviews', [AdminReviewController::class, 'index'])->name('admin.reviews.index');
        Route::get('/reviews/{id}', [AdminReviewController::class, 'show'])->name('admin.reviews.show');
        Route::delete('/reviews/{id}', [AdminReviewController::class, 'destroy'])->name('admin.reviews.destroy');
        
        // Report Management
        Route::get('/reports', [AdminReportController::class, 'index'])->name('admin.reports.index');
        Route::get('/reports/pending', [AdminReportController::class, 'pendingReports'])->name('admin.reports.pending');
        Route::get('/reports/statistics', [AdminReportController::class, 'statistics'])->name('admin.reports.statistics');
        Route::get('/reports/{id}', [AdminReportController::class, 'show'])->name('admin.reports.show');
        Route::put('/reports/{id}/status', [AdminReportController::class, 'updateStatus'])->name('admin.reports.updateStatus');
        Route::delete('/reports/{id}', [AdminReportController::class, 'destroy'])->name('admin.reports.destroy');
        
        // Notification Management
        Route::get('/notifications', [AdminNotificationController::class, 'index'])->name('admin.notifications.index');
        Route::get('/notifications/{id}', [AdminNotificationController::class, 'show'])->name('admin.notifications.show');
        Route::delete('/notifications/{id}', [AdminNotificationController::class, 'destroy'])->name('admin.notifications.destroy');
        
        // Hostel Management
        Route::get('/hostels', [AdminHostelController::class, 'index'])->name('admin.hostels.index');
        Route::get('/hostels/{id}', [AdminHostelController::class, 'show'])->name('admin.hostels.show');
        Route::delete('/hostels/{id}', [AdminHostelController::class, 'destroy'])->name('admin.hostels.destroy');
        
        // Message Management
        Route::get('/messages', [AdminMessageController::class, 'index'])->name('admin.messages.index');
        Route::get('/messages/{id}', [AdminMessageController::class, 'show'])->name('admin.messages.show');
        Route::delete('/messages/{id}', [AdminMessageController::class, 'destroy'])->name('admin.messages.destroy');
        
        // Payment Receipt Management
        Route::get('/payment-receipts', [AdminPaymentReceiptController::class, 'index'])->name('admin.payment-receipts.index');
        Route::get('/payment-receipts/{id}', [AdminPaymentReceiptController::class, 'show'])->name('admin.payment-receipts.show');
        Route::delete('/payment-receipts/{id}', [AdminPaymentReceiptController::class, 'destroy'])->name('admin.payment-receipts.destroy');
        
        // Settings Management
        Route::get('/settings/general', [AdminSettingsController::class, 'general'])->name('admin.settings.general');
        Route::post('/settings/general', [AdminSettingsController::class, 'updateGeneral'])->name('admin.settings.updateGeneral');
        Route::get('/settings/email', [AdminSettingsController::class, 'email'])->name('admin.settings.email');
        Route::post('/settings/email', [AdminSettingsController::class, 'updateEmail'])->name('admin.settings.updateEmail');
        
        // File Upload Routes
        Route::prefix('api')->group(function () {
            Route::post('/users/upload_photo', [\App\Http\Controllers\Api\AdminFileUploadController::class, 'uploadUserPhoto'])->name('admin.upload.user.photo');
            Route::post('/properties/upload_image', [\App\Http\Controllers\Api\AdminFileUploadController::class, 'uploadPropertyImage'])->name('admin.upload.property.image');
            Route::post('/contracts/upload_document', [\App\Http\Controllers\Api\AdminFileUploadController::class, 'uploadContractDocument'])->name('admin.upload.contract.document');
        });
    });
});

// Web property routes (public)
Route::prefix('properties')->group(function () {
    Route::get('/', [WebPropertyController::class, 'index']);
    Route::get('/{id}', [WebPropertyController::class, 'show']);
});
