<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\UserRequestController;
use App\Http\Controllers\Admin\AdminMarkListController;
use App\Http\Controllers\Admin\AdminOrderController;
use App\Http\Controllers\Admin\ShipmentController;
use App\Http\Controllers\Admin\AdminInvoiceController;
use App\Http\Controllers\Admin\AdminCustomerController;
use App\Http\Controllers\Admin\AdminAccountController;
use App\Http\Controllers\Admin\AdminReportController;

// ---------------------------
// Public Front Page
// ---------------------------
Route::get('/', function () {
    return view('welcome');
});

// ---------------------------
// ADMIN LOGIN ROUTES
// ---------------------------
Route::prefix('admin')->group(function () {

    // MUST have route name "login" for session redirect
    Route::get('/login', [AdminAuthController::class, 'showLoginForm'])
        ->name('admin.login');

    Route::post('/login', [AdminAuthController::class, 'login'])
        ->name('admin.login.submit');

    Route::post('/logout', [AdminAuthController::class, 'logout'])
        ->name('admin.logout');
});


// ==========================================
// PROTECTED ADMIN ROUTES (session protected)
// ==========================================
Route::prefix('admin')
    ->middleware('auth:admin')
    ->group(function () {

        // Dashboard
        Route::get('/dashboard', [AdminOrderController::class, 'index'])
            ->name('admin.dashboard');

       Route::get('/reports', [AdminReportController::class, 'index'])->name('admin.reports');

        Route::get('/chat-support', fn() => view('admin.chat_support'))
            ->name('admin.chat_support');

        Route::get('/staff', fn() => view('admin.staff'))
            ->name('admin.staff');

        Route::get('/account', fn() => view('admin.account'))
            ->name('admin.account');

        Route::get('/profile', fn() => view('admin.profile'))
            ->name('admin.profile');


        // ---------------------------
        // USER REQUESTS
        // ---------------------------
        Route::get('/requests', [UserRequestController::class, 'index'])
            ->name('admin.requests');

        Route::get('/requests/approve/{id}', [UserRequestController::class, 'approve'])
            ->name('admin.requests.approve');

        Route::get('/requests/reject/{id}', [UserRequestController::class, 'reject'])
            ->name('admin.requests.reject');


        // ---------------------------
        // MARK LIST
        // ---------------------------
        Route::get('/mark-list', [AdminMarkListController::class, 'index'])
            ->name('admin.marklist.index');

        Route::get('/mark-list/status/{id}', [AdminMarkListController::class, 'toggleStatus'])
            ->name('admin.marklist.toggle');


        // ---------------------------
        // ORDERS
        // ---------------------------
        Route::get('/orders', [AdminOrderController::class, 'orderShow'])
            ->name('admin.orders');


        Route::get('/orders/list', [AdminOrderController::class, 'index'])
            ->name('admin.orders.index');

        Route::get('/orders/{id}', [AdminOrderController::class, 'show'])
            ->name('admin.orders.show');

        Route::post('/orders/temp/add', [AdminOrderController::class, 'addTempItem'])
            ->name('admin.orders.temp.add');

        Route::post('/orders/temp/delete', [AdminOrderController::class, 'deleteTempItem'])
            ->name('admin.orders.temp.delete');

        Route::post('/orders/temp/reset', [AdminOrderController::class, 'resetTemp'])
            ->name('admin.orders.temp.reset');

        Route::post('/orders/finish', [AdminOrderController::class, 'finishOrder'])
            ->name('admin.orders.finish');

        Route::get('/orders/view/{id}', [AdminOrderController::class, 'popup'])
            ->name('admin.orders.popup');


        // ---------------------------
        // SHIPMENTS
        // ---------------------------
        Route::get('/shipments', [ShipmentController::class, 'index'])
            ->name('admin.shipments');

        Route::post('/shipments/store', [ShipmentController::class, 'store'])
            ->name('admin.shipments.store');

        Route::post('/shipments/update-status', [ShipmentController::class, 'updateStatus'])
            ->name('admin.shipments.updateStatus');

        Route::get('/shipments/{id}', [ShipmentController::class, 'show'])
            ->name('admin.shipments.show');


        // ---------------------------
        // INVOICES
        // ---------------------------
        Route::get('/invoices', [AdminInvoiceController::class, 'index'])
            ->name('admin.invoices.index');

        Route::get('/invoices/{id}/popup', [AdminInvoiceController::class, 'popup'])
            ->name('admin.invoices.popup');

        Route::get('/invoices/{id}/edit', [AdminInvoiceController::class, 'edit'])
            ->name('admin.invoices.edit');

        Route::post('/invoices/{id}/update', [AdminInvoiceController::class, 'update'])
            ->name('admin.invoices.update');

        Route::post('/invoices/{invoice}/installments', [AdminInvoiceController::class, 'storeInstallment'])
            ->name('admin.invoice.installment.store');

        Route::post('/invoices/{id}/installment', [AdminInvoiceController::class, 'storeInstallment'])
        ->name('admin.invoice.installment.store');

        Route::delete('/installment/{id}', [AdminInvoiceController::class, 'deleteInstallment'])
        ->name('admin.invoice.installment.delete');




            //Add New Invoice
        Route::get('/admin/invoices/create', [InvoiceController::class, 'create'])->name('admin.invoices.create');
    


        // ---------------------------
        // CUSTOMERS
        // ---------------------------
        Route::get('/customers', [AdminCustomerController::class, 'index'])
            ->name('admin.customers.index');

        Route::get('/customers/add', [AdminCustomerController::class, 'create'])
            ->name('admin.customers.add');

        Route::post('/customers/store', [AdminCustomerController::class, 'store'])
            ->name('admin.customers.store');

        Route::get('/customers/{id}/view', [AdminCustomerController::class, 'view'])
            ->name('admin.customers.view');

        Route::post('/customers/{id}/status', [AdminCustomerController::class, 'toggleStatus'])
            ->name('admin.customers.status');
    });


// ==========================================
// ADMIN ACCOUNT (AJAX) ROUTES
// ==========================================
Route::prefix('admin/account')
    ->middleware('auth:admin')
    ->name('admin.account.')
    ->group(function () {

        Route::get('/dashboard', [AdminAccountController::class, 'getDashboardData'])
            ->name('dashboard');

        Route::get('/available-orders', [AdminAccountController::class, 'getAvailableOrders'])
            ->name('orders.available');

        Route::post('/create-order', [AdminAccountController::class, 'accountCreateOrder'])
            ->name('create');

        Route::post('/toggle-payment', [AdminAccountController::class, 'togglePayment'])
            ->name('toggle');

        Route::post('/installment/create', [AdminAccountController::class, 'addInstallment'])
            ->name('installment.create');

        Route::post('/installment/update-status', [AdminAccountController::class, 'updateInstallmentStatus'])
            ->name('installment.update');

        Route::get('/entry/{entry_no}', [AdminAccountController::class, 'getEntryDetails'])
            ->name('entry.details');
    });



