<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\ItemController;

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // Customer Management Routes (Both roles)
    Route::middleware('role:manage_customers')->group(function () {
        Route::resource('customers', CustomerController::class);
    });

    // Order Management Routes (Both roles)
    Route::middleware('role:manage_orders')->group(function () {
        Route::resource('orders', OrderController::class);
        Route::post('orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.update-status');
        Route::post('orders/{order}/payment', [OrderController::class, 'updatePayment'])->name('orders.update-payment');
        Route::get('orders/{order}/print', [OrderController::class, 'print'])->name('orders.print');
        Route::delete('orders/{order}/remove-item', [OrderController::class, 'removeItem'])->name('orders.remove-item');
    });

    // Items & Pricing Management Routes (Admin only)
    Route::middleware('role:manage_items')->group(function () {
        Route::resource('items', ItemController::class);
        Route::post('items/bulk-update-prices', [ItemController::class, 'bulkUpdatePrices'])->name('items.bulk-update-prices');
        Route::patch('items/{item}/toggle-status', [ItemController::class, 'toggleStatus'])->name('items.toggle-status');
    });

    // Reports Routes (Both roles, but different access levels)
    Route::middleware('role:view_reports')->group(function () {
        Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('reports/customers', [ReportController::class, 'customers'])->name('reports.customers');
    });

    // Advanced Reports (Admin only)
    Route::middleware('role:view_advanced_reports')->group(function () {
        Route::get('reports/items', [ReportController::class, 'items'])->name('reports.items');
    });



    // User Management Routes (Admin only)
    Route::middleware('role:manage_users')->group(function () {
        Route::resource('users', UserController::class);
        Route::patch('users/{user}/toggle-status', [UserController::class, 'toggleStatus'])->name('users.toggle-status');
    });

    // Debt Management Routes (Both roles)
    Route::middleware('role:manage_orders')->group(function () {
        Route::get('debt', [App\Http\Controllers\DebtController::class, 'index'])->name('debt.index');
        Route::get('debt/report', [App\Http\Controllers\DebtController::class, 'report'])->name('debt.report');
        Route::get('debt/customer/{customer}', [App\Http\Controllers\DebtController::class, 'customer'])->name('debt.customer');
        Route::get('debt/print/{customer}', [App\Http\Controllers\DebtController::class, 'printStatement'])->name('debt.print');
        Route::post('debt/reminder/{order}', [App\Http\Controllers\DebtController::class, 'sendReminder'])->name('debt.reminder');
        Route::post('debt/remind-all/{customer}', [App\Http\Controllers\DebtController::class, 'sendConsolidatedReminder'])->name('debt.remind_all');
        Route::delete('debt/order/{order}', [App\Http\Controllers\DebtController::class, 'deleteOrder'])->name('debt.delete-order');
    });

    // SMS Test Route (Admin only) - Remove this after testing
    Route::get('/test-sms/{order}/{format?}', function(\App\Models\Order $order, $format = '0') {
        if (!Auth::user()->isAdmin()) {
            abort(403);
        }

        // Test different phone number formats
        $phoneNumbers = [
            '0' => '07043167491',      // Nigerian format
            '1' => '+2347043167491',   // International format
        ];

        $phoneNumber = $phoneNumbers[$format] ?? '07043167491';

        $smsService = new \App\Services\SmsService();
        $result = $smsService->sendOrderStatusNotificationWithBalance(
            $phoneNumber,
            $order->customer->full_name,
            $order->order_number,
            'ready',
            $order->outstanding_balance
        );

        return response()->json([
            'order' => $order->order_number,
            'customer' => $order->customer->full_name,
            'phone_input' => $phoneNumber,
            'phone_formatted' => $smsService->formatPhoneNumber($phoneNumber),
            'balance' => $order->outstanding_balance,
            'sms_result' => $result,
            'test_info' => 'Use /test-sms/{order}/0 for 07043167491 or /test-sms/{order}/1 for +2347043167491'
        ]);
    })->name('test.sms');
});

require __DIR__.'/auth.php';
