<?php

use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\AttachmentController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\PasswordController;
use App\Http\Controllers\AvailabilityController;
use App\Http\Controllers\BookingController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\PackageController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ProductCategoryController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\QuotationController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SpaceBlockController;
use App\Http\Controllers\SpaceController;
use App\Http\Controllers\SpaceTypeController;
use Illuminate\Support\Facades\Route;

Route::redirect('/', '/login');

Route::middleware('guest')->group(function (): void {
    Route::get('login', [LoginController::class, 'create'])->name('login');
    Route::post('login', [LoginController::class, 'store']);
});

Route::middleware(['auth', 'active'])->group(function (): void {
    Route::post('logout', [LoginController::class, 'destroy'])->name('logout');

    Route::get('dashboard', [DashboardController::class, 'index'])
        ->name('dashboard')
        ->middleware('permission:dashboard.view');

    Route::get('profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('password', [PasswordController::class, 'update'])->name('password.update');

    Route::middleware('permission:customers.view')->group(function (): void {
        Route::get('customers/{customer}/history', [CustomerController::class, 'history'])->name('customers.history');
        Route::resource('customers', CustomerController::class);
    });

    Route::middleware('permission:spaces.view')->group(function (): void {
        Route::resource('space-types', SpaceTypeController::class)->except(['show']);
        Route::post('spaces/{space}/blocks', [SpaceController::class, 'storeBlock'])->name('spaces.blocks.store');
        Route::resource('spaces', SpaceController::class);
        Route::delete('space-blocks/{space_block}', [SpaceBlockController::class, 'destroy'])->name('space-blocks.destroy');
        Route::get('space-blocks', [SpaceBlockController::class, 'index'])->name('space-blocks.index');
        Route::get('availability/events', [AvailabilityController::class, 'events'])->name('availability.events');
        Route::get('availability/check', [AvailabilityController::class, 'checkAvailability'])->name('availability.check');
        Route::get('availability/busy-dates', [AvailabilityController::class, 'busyDates'])->name('availability.busy-dates');
        Route::get('availability', [AvailabilityController::class, 'index'])->name('availability.index');
    });

    Route::post('pricing/preview', [\App\Http\Controllers\PricingController::class, 'preview'])
        ->name('pricing.preview');
    Route::post('pricing/package-lines', [\App\Http\Controllers\PricingController::class, 'packageLines'])
        ->name('pricing.package-lines');

    Route::middleware('permission:bookings.view')->group(function (): void {
        Route::post('bookings/{booking}/confirm', [BookingController::class, 'confirm'])->name('bookings.confirm');
        Route::post('bookings/{booking}/cancel', [BookingController::class, 'cancel'])->name('bookings.cancel');
        Route::post('bookings/{booking}/postpone', [BookingController::class, 'postpone'])->name('bookings.postpone');
        Route::post('bookings/{booking}/mark-in-progress', [BookingController::class, 'markInProgress'])->name('bookings.mark-in-progress');
        Route::post('bookings/{booking}/mark-completed', [BookingController::class, 'markCompleted'])->name('bookings.mark-completed');
        Route::post('bookings/{booking}/mark-no-show', [BookingController::class, 'markNoShow'])->name('bookings.mark-no-show');
        Route::post('bookings/{booking}/place-hold', [BookingController::class, 'placeHold'])->name('bookings.place-hold');
        Route::post('bookings/{booking}/reschedule', [BookingController::class, 'reschedule'])->name('bookings.reschedule');
        Route::post('bookings/{booking}/duplicate', [BookingController::class, 'duplicate'])->name('bookings.duplicate');
        Route::resource('bookings', BookingController::class);
    });

    Route::middleware('permission:products.view')->group(function (): void {
        Route::resource('product-categories', ProductCategoryController::class)->except(['show']);
        Route::resource('products', ProductController::class);
    });

    Route::middleware('permission:packages.view')->group(function (): void {
        Route::post('packages/{package}/duplicate', [PackageController::class, 'duplicate'])->name('packages.duplicate');
        Route::post('packages/{package}/estimate', [PackageController::class, 'estimate'])->name('packages.estimate');
        Route::resource('packages', PackageController::class);
    });

    Route::middleware('permission:quotations.view')->group(function (): void {
        Route::post('bookings/{booking}/quotations', [QuotationController::class, 'createFromBooking'])->name('bookings.quotations.create');
        Route::post('quotations/{quotation}/issue', [QuotationController::class, 'issue'])->name('quotations.issue');
        Route::post('quotations/{quotation}/accept', [QuotationController::class, 'accept'])->name('quotations.accept');
        Route::post('quotations/{quotation}/reject', [QuotationController::class, 'reject'])->name('quotations.reject');
        Route::post('quotations/{quotation}/expire', [QuotationController::class, 'expire'])->name('quotations.expire');
        Route::post('quotations/{quotation}/cancel', [QuotationController::class, 'cancel'])->name('quotations.cancel');
        Route::post('quotations/{quotation}/revise', [QuotationController::class, 'revise'])->name('quotations.revise');
        Route::post('quotations/{quotation}/duplicate', [QuotationController::class, 'duplicate'])->name('quotations.duplicate');
        Route::post('quotations/{quotation}/convert', [QuotationController::class, 'convert'])->name('quotations.convert');
        Route::get('quotations/{quotation}/pdf', [QuotationController::class, 'pdf'])->name('quotations.pdf');
        Route::get('quotations/{quotation}/print', [QuotationController::class, 'print'])->name('quotations.print');
        Route::resource('quotations', QuotationController::class);
    });

    Route::middleware('permission:invoices.view')->group(function (): void {
        Route::post('bookings/{booking}/invoices', [InvoiceController::class, 'createFromBooking'])->name('bookings.invoices.create');
        Route::post('quotations/{quotation}/invoices', [InvoiceController::class, 'createFromQuotation'])->name('quotations.invoices.create');
        Route::post('invoices/{invoice}/issue', [InvoiceController::class, 'issue'])->name('invoices.issue');
        Route::post('invoices/{invoice}/void', [InvoiceController::class, 'void'])->name('invoices.void');
        Route::post('invoices/{invoice}/status', [InvoiceController::class, 'updateStatus'])->name('invoices.status');
        Route::post('invoices/{invoice}/quotation', [InvoiceController::class, 'createQuotation'])->name('invoices.quotation.create');
        Route::get('invoices/{invoice}/pdf', [InvoiceController::class, 'pdf'])->name('invoices.pdf');
        Route::get('invoices/{invoice}/print', [InvoiceController::class, 'print'])->name('invoices.print');
        Route::resource('invoices', InvoiceController::class);
    });

    Route::middleware('permission:payments.view')->group(function (): void {
        Route::post('payments/{payment}/reverse', [PaymentController::class, 'reverse'])->name('payments.reverse');
        Route::post('payments/{payment}/refund', [PaymentController::class, 'refund'])->name('payments.refund');
        Route::get('payments/{payment}/pdf', [PaymentController::class, 'pdf'])->name('payments.pdf');
        Route::get('payments/{payment}/print', [PaymentController::class, 'print'])->name('payments.print');
        Route::resource('payments', PaymentController::class)->except(['destroy']);
    });

    Route::middleware('auth')->group(function (): void {
        Route::post('attachments/{type}/{id}', [AttachmentController::class, 'store'])
            ->name('attachments.store')
            ->middleware('permission:attachments.manage');
        Route::get('attachments/{attachment}/download', [AttachmentController::class, 'download'])
            ->name('attachments.download');
        Route::delete('attachments/{attachment}', [AttachmentController::class, 'destroy'])
            ->name('attachments.destroy')
            ->middleware('permission:attachments.manage');
    });

    Route::middleware('permission:reports.view')->group(function (): void {
        Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('reports/{type}', [ReportController::class, 'show'])->name('reports.show');
        Route::get('reports/{type}/export/{format}', [ReportController::class, 'export'])
            ->where('format', 'csv|pdf|print')
            ->middleware('permission:reports.export')
            ->name('reports.export');
    });

    Route::middleware('permission:notifications.view')->group(function (): void {
        Route::get('notifications', [NotificationController::class, 'index'])->name('notifications.index');
        Route::post('notifications/{notification}/read', [NotificationController::class, 'markRead'])->name('notifications.read');
        Route::post('notifications/read-all', [NotificationController::class, 'markAllRead'])->name('notifications.read-all');
    });

    Route::prefix('admin')->name('admin.')->group(function (): void {
        Route::middleware('permission:users.manage')->group(function (): void {
            Route::resource('users', UserController::class);
            Route::post('users/{user}/reset-password', [UserController::class, 'resetPassword'])
                ->name('users.reset-password');
            Route::post('users/{user}/activate', [UserController::class, 'activate'])
                ->name('users.activate');
            Route::post('users/{user}/deactivate', [UserController::class, 'deactivate'])
                ->name('users.deactivate');
        });

        Route::middleware('permission:roles.manage')->group(function (): void {
            Route::resource('roles', RoleController::class);
        });

        Route::middleware('permission:settings.manage')->group(function (): void {
            Route::get('settings', [SettingController::class, 'edit'])->name('settings.edit');
            Route::put('settings', [SettingController::class, 'update'])->name('settings.update');
        });

        Route::middleware('permission:audit.view')->group(function (): void {
            Route::get('audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index');
        });
    });
});
