<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SubscriptionController;
use Illuminate\Support\Facades\Route;

// ─────────────────────────────────────────
// PUBLIC ROUTES (no auth required)
// ─────────────────────────────────────────

// Public invoice payment page (shareable link)
Route::get('/invoice/{token}', [InvoiceController::class, 'public'])
    ->name('invoices.public');

// Auth
Route::middleware('guest')->group(function () {
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register']);

    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
});

Route::post('/logout', [AuthController::class, 'logout'])
    ->name('logout')
    ->middleware('auth');

// ─────────────────────────────────────────
// AUTHENTICATED ROUTES
// ─────────────────────────────────────────
Route::middleware(['auth'])->group(function () {

    // Onboarding (must complete before accessing app)
    Route::get('/onboarding', [AuthController::class, 'showOnboarding'])->name('onboarding');
    Route::post('/onboarding', [AuthController::class, 'completeOnboarding']);

    // All routes below require completed onboarding
    Route::middleware(['onboarding'])->group(function () {

        // ── Dashboard ──────────────────────────────
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

        // ── Invoices ───────────────────────────────
        Route::prefix('invoices')->name('invoices.')->group(function () {
            Route::get('/',                      [InvoiceController::class, 'index'])->name('index');
            Route::get('/create',                [InvoiceController::class, 'create'])->name('create');
            Route::post('/',                     [InvoiceController::class, 'store'])->name('store');
            Route::get('/{invoice}',             [InvoiceController::class, 'show'])->name('show');
            Route::get('/{invoice}/edit',        [InvoiceController::class, 'edit'])->name('edit');
            Route::put('/{invoice}',             [InvoiceController::class, 'update'])->name('update');

            // Actions
            Route::post('/{invoice}/send',       [InvoiceController::class, 'send'])->name('send');
            Route::get('/{invoice}/download',    [InvoiceController::class, 'download'])->name('download');
            Route::post('/{invoice}/mark-paid',  [InvoiceController::class, 'markPaid'])->name('mark-paid');
            Route::post('/{invoice}/duplicate',  [InvoiceController::class, 'duplicate'])->name('duplicate');
            Route::post('/{invoice}/cancel',     [InvoiceController::class, 'cancel'])->name('cancel');
            Route::delete('/{invoice}',          [InvoiceController::class, 'destroy'])->name('destroy');
        });

        // ── Clients ────────────────────────────────
        Route::prefix('clients')->name('clients.')->group(function () {
            Route::get('/',              [ClientController::class, 'index'])->name('index');
            Route::get('/create',        [ClientController::class, 'create'])->name('create');
            Route::post('/',             [ClientController::class, 'store'])->name('store');
            Route::get('/{client}',      [ClientController::class, 'show'])->name('show');
            Route::get('/{client}/edit', [ClientController::class, 'edit'])->name('edit');
            Route::put('/{client}',      [ClientController::class, 'update'])->name('update');
            Route::delete('/{client}',   [ClientController::class, 'destroy'])->name('destroy');
        });

        // ── Expenses ───────────────────────────────
        Route::prefix('expenses')->name('expenses.')->group(function () {
            Route::get('/',                [ExpenseController::class, 'index'])->name('index');
            Route::get('/create',          [ExpenseController::class, 'create'])->name('create');
            Route::post('/',               [ExpenseController::class, 'store'])->name('store');
            Route::get('/summary',         [ExpenseController::class, 'summary'])->name('summary');
            Route::get('/{expense}/edit',  [ExpenseController::class, 'edit'])->name('edit');
            Route::put('/{expense}',       [ExpenseController::class, 'update'])->name('update');
            Route::delete('/{expense}',    [ExpenseController::class, 'destroy'])->name('destroy');
        });

        // ── Reports (Business plan only) ───────────
        Route::prefix('reports')->name('reports.')->group(function () {
            Route::get('/revenue',         [ReportController::class, 'revenue'])->name('revenue');
            Route::get('/clients',         [ReportController::class, 'clients'])->name('clients');
            Route::get('/tax',             [ReportController::class, 'tax'])->name('tax');
            Route::get('/export',          [ReportController::class, 'export'])->name('export');
        });

        // ── Settings ───────────────────────────────
        Route::prefix('settings')->name('settings.')->group(function () {
            Route::get('/profile',         [SettingsController::class, 'profile'])->name('profile');
            Route::put('/profile',         [SettingsController::class, 'updateProfile']);

            Route::get('/invoice',         [SettingsController::class, 'invoice'])->name('invoice');
            Route::put('/invoice',         [SettingsController::class, 'updateInvoice']);

            Route::get('/bank',            [SettingsController::class, 'bank'])->name('bank');
            Route::put('/bank',            [SettingsController::class, 'updateBank']);

            Route::get('/notifications',   [SettingsController::class, 'notifications'])->name('notifications');
            Route::put('/notifications',   [SettingsController::class, 'updateNotifications']);

            // Route::get('/subscription',    [SettingsController::class, 'subscription'])->name('subscription');
            Route::get('/subscription', [SubscriptionController::class, 'index'])->name('settings.subscription');
        });

        // Subscription
Route::prefix('subscription')->name('subscription.')->group(function () {
    Route::get('/',          [SubscriptionController::class, 'index'])->name('index');
    Route::post('/initiate', [SubscriptionController::class, 'initiate'])->name('initiate');
    Route::post('/cancel',   [SubscriptionController::class, 'cancel'])->name('cancel');
});

    }); // end onboarding middleware

}); // end auth middleware

// ─────────────────────────────────────────
// PAYMENT ROUTES (Paystack)
// ─────────────────────────────────────────
Route::prefix('payments')->name('payments.')->group(function () {
    Route::post('/initiate',   [PaymentController::class, 'initiate'])->name('initiate');
    Route::get('/callback',    [PaymentController::class, 'callback'])->name('callback');
});

Route::get('/subscription/callback', [SubscriptionController::class, 'callback'])->name('subscription.callback');