<?php

use App\Http\Controllers\CompanyController;
use App\Http\Controllers\TruckController;
use App\Http\Controllers\DriverController;
use App\Http\Controllers\DriverDashboardController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\PackageController;
use App\Http\Controllers\PaymentMethodController;
use App\Http\Controllers\SubscriptionController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\TrialPaymentController;
use App\Http\Controllers\AdminDashboardController;
use App\Http\Controllers\Auth\OnboardController as WebOnboardController;
use Illuminate\Support\Facades\Route;

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

Route::get('/dashboard', [DashboardController::class, 'index'])->middleware(['auth', 'verified', 'check.trial.expired'])->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');

    Route::prefix('documents')->name('documents.')->group(function () {
        Route::get('/upload', [DocumentController::class, 'create'])->name('create');
        Route::post('/', [DocumentController::class, 'store'])->name('store');
    });
});

// Owner protected routes
Route::middleware(['auth', 'owner'])->prefix('owner')->name('owner.')->group(function () {
    // Company setup
    Route::get('/companies/create', [CompanyController::class, 'create'])->name('companies.create');
    Route::post('/companies', [CompanyController::class, 'store'])->name('companies.store');

    // Fleet management
    Route::resource('/trucks', TruckController::class)->names([
        'index' => 'trucks.index',
        'create' => 'trucks.create',
        'store' => 'trucks.store',
        'show' => 'trucks.show',
        'edit' => 'trucks.edit',
        'update' => 'trucks.update',
        'destroy' => 'trucks.destroy',
    ]);

    // Driver management
    Route::resource('/drivers', DriverController::class)->names([
        'index' => 'drivers.index',
        'create' => 'drivers.create',
        'store' => 'drivers.store',
        'show' => 'drivers.show',
        'edit' => 'drivers.edit',
        'update' => 'drivers.update',
        'destroy' => 'drivers.destroy',
    ]);

    // Analytics
    Route::get('/analytics', [\App\Http\Controllers\AnalyticsController::class, 'index'])->name('analytics.index');
    // Loads
    Route::get('/loads', [\App\Http\Controllers\LoadsController::class, 'index'])->name('loads.index');

    // Document upload
    Route::get('/documents/upload', [DocumentController::class, 'create'])->name('documents.create');
    Route::post('/documents', [DocumentController::class, 'store'])->name('documents.store');
});

// Subscription and Payment routes
Route::middleware('auth')->name('subscriptions.')->prefix('subscriptions')->group(function () {
    Route::get('/', [SubscriptionController::class, 'index'])->name('index');
    Route::get('/select-package', [SubscriptionController::class, 'selectPackage'])->middleware('owner')->name('select-package');
    Route::post('/', [SubscriptionController::class, 'store'])->middleware('owner')->name('store');
    Route::get('/{subscription}', [SubscriptionController::class, 'show'])->name('show');
    Route::patch('/{subscription}/cancel', [SubscriptionController::class, 'cancel'])->middleware('owner')->name('cancel');
    Route::post('/{subscription}/renew', [SubscriptionController::class, 'renew'])->middleware('owner')->name('renew');
    Route::get('/{subscription}/invoice', [SubscriptionController::class, 'invoice'])->middleware('owner')->name('invoice');
});

// Payment routes
Route::middleware('auth')->name('payments.')->prefix('payments')->group(function () {
    Route::get('/checkout/{subscription}', [PaymentController::class, 'checkout'])->middleware('owner')->name('checkout');
    Route::post('/process', [PaymentController::class, 'process'])->middleware('owner')->name('process');
    Route::get('/success/{payment}', [PaymentController::class, 'success'])->middleware('owner')->name('success');
    Route::get('/cancel/{payment}', [PaymentController::class, 'cancel'])->middleware('owner')->name('cancel');
    Route::post('/{payment}/retry', [PaymentController::class, 'retryPayment'])->middleware('owner')->name('retry');
    Route::post('/subscription/{subscription}/create-stripe', [PaymentController::class, 'createStripeSubscription'])->middleware('owner')->name('create-stripe-subscription');
    Route::post('/subscription/{subscription}/update-driver-changes', [PaymentController::class, 'updateSubscriptionDriverChanges'])->middleware('owner')->name('update-driver-changes');
    Route::post('/webhook/stripe', [PaymentController::class, 'stripeWebhook'])->withoutMiddleware('auth');
    Route::post('/webhook/paypal', [PaymentController::class, 'paypalWebhook'])->withoutMiddleware('auth');
    Route::post('/webhook/wise', [PaymentController::class, 'wiseWebhook'])->withoutMiddleware('auth');
});

// Admin routes for packages and payment methods
Route::middleware(['auth', 'admin'])->name('admin.')->prefix('admin')->group(function () {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::get('/companies', [AdminDashboardController::class, 'companies'])->name('companies.index');
    Route::get('/users', [AdminDashboardController::class, 'users'])->name('users.index');
    Route::get('/owners', [AdminDashboardController::class, 'owners'])->name('owners.index');
    Route::get('/drivers', [AdminDashboardController::class, 'drivers'])->name('drivers.index');
    Route::get('/fleet', [AdminDashboardController::class, 'fleet'])->name('fleet.index');

    // Package management
    Route::resource('/packages', PackageController::class)->names([
        'index' => 'packages.index',
        'create' => 'packages.create',
        'store' => 'packages.store',
        'show' => 'packages.show',
        'edit' => 'packages.edit',
        'update' => 'packages.update',
        'destroy' => 'packages.destroy',
    ]);

    // Payment method management
    Route::resource('/payment-methods', PaymentMethodController::class)->names([
        'index' => 'payment-methods.index',
        'create' => 'payment-methods.create',
        'store' => 'payment-methods.store',
        'show' => 'payment-methods.show',
        'edit' => 'payment-methods.edit',
        'update' => 'payment-methods.update',
        'destroy' => 'payment-methods.destroy',
    ]);
    Route::post('/payment-methods/{paymentMethod}/test', [PaymentMethodController::class, 'test'])->name('payment-methods.test');

    // Payment dashboard
    Route::get('/payments', [PaymentController::class, 'index'])->name('payments.index');
    Route::get('/payments/{payment}', [PaymentController::class, 'show'])->name('payments.show');
    Route::patch('/payments/{payment}/status', [PaymentController::class, 'updateStatus'])->name('payments.update-status');
});

// Onboarding stepper (web)
Route::middleware('auth')->group(function () {
    Route::get('/onboard', [WebOnboardController::class, 'show'])->name('onboard.show');
    Route::post('/onboard', [WebOnboardController::class, 'submit'])->name('onboard.submit');
    Route::post('/onboard/payment-intent', [WebOnboardController::class, 'createPaymentIntent'])->name('onboard.payment-intent');
});

// Trial payment completion (web)
Route::middleware('auth')->prefix('trial-payment')->name('trial-payment.')->group(function () {
    Route::get('/', [TrialPaymentController::class, 'show'])->name('show');
    Route::post('/intent', [TrialPaymentController::class, 'createPaymentIntent'])->name('intent');
    Route::post('/complete', [TrialPaymentController::class, 'complete'])->name('complete');
});

// Driver protected routes
Route::middleware(['auth', 'verified'])->prefix('driver')->name('driver.')->group(function () {
    Route::get('/dashboard', [DriverDashboardController::class, 'dashboard'])->name('dashboard');
    Route::get('/loads', [DriverDashboardController::class, 'loads'])->name('loads');
    Route::get('/profile', [DriverDashboardController::class, 'profile'])->name('profile');
    Route::patch('/profile', [DriverDashboardController::class, 'updateProfile'])->name('profile.update');
});

require __DIR__.'/auth.php';
