<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\CompaniesController;
use App\Http\Controllers\PricingPlansController;
use App\Http\Controllers\AppsController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\InboxController;
use App\Http\Controllers\InboxApiController;
use App\Http\Controllers\UserDashboardController;
use App\Http\Controllers\WebhookController;

// Redirect root to login
Route::get('/', fn() => redirect()->route('login'));

// WhatsApp Webhook (public — no auth, Meta calls this)
Route::get('/webhook/whatsapp',  [WebhookController::class, 'verify']);
Route::post('/webhook/whatsapp', [WebhookController::class, 'handle'])->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);

// Auth routes
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterController::class, 'register']);

// ── User dashboard (role = user) ───────────────────────────────────────────
Route::middleware(['auth', 'role.user'])->group(function () {
    Route::get('/home', [UserDashboardController::class, 'index'])->name('user.dashboard');
    Route::get('/whatsapp-setup', [UserDashboardController::class, 'whatsappSetup'])->name('user.whatsapp-setup');
    Route::post('/whatsapp-setup', [UserDashboardController::class, 'saveWhatsappSetup'])->name('user.whatsapp-setup.save');
    Route::get('/api-docs', [UserDashboardController::class, 'apiDocs'])->name('user.api-docs');

    // Inbox
    Route::get('/inbox', [InboxController::class, 'index'])->name('user.inbox');
    Route::get('/inbox/{conversation}', [InboxController::class, 'index'])->name('user.inbox.conversation');
    Route::post('/inbox/{conversation}/send', [InboxController::class, 'send'])->name('user.inbox.send');
    Route::patch('/inbox/{conversation}/status', [InboxController::class, 'updateStatus'])->name('user.inbox.status');
    Route::patch('/inbox/{conversation}/label', [InboxController::class, 'updateLabel'])->name('user.inbox.label');

    // JSON API endpoints consumed by the Vue inbox app
    Route::prefix('inbox/api')->group(function () {
        Route::get('/conversations',                                    [InboxApiController::class, 'conversations']);
        Route::get('/conversations/{conversation}/messages',            [InboxApiController::class, 'messages']);
        Route::post('/conversations/{conversation}/send',               [InboxApiController::class, 'send']);
        Route::post('/conversations/{conversation}/send-media',         [InboxApiController::class, 'sendMedia']);
        Route::post('/conversations/{conversation}/send-location',      [InboxApiController::class, 'sendLocation']);
        Route::post('/conversations/{conversation}/mark-read',          [InboxApiController::class, 'markRead']);
        Route::post('/conversations/{conversation}/typing',             [InboxApiController::class, 'sendTyping']);
        Route::patch('/conversations/{conversation}/status',            [InboxApiController::class, 'updateStatus']);
        Route::patch('/conversations/{conversation}/label',             [InboxApiController::class, 'updateLabel']);
        Route::get('/contacts',                                         [InboxApiController::class, 'contacts']);
    });
});

// ── Admin panel (role = superadmin | admin) ────────────────────────────────
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    Route::get('/companies',              [CompaniesController::class, 'index'])->name('companies');
    Route::post('/companies',             [CompaniesController::class, 'store'])->name('companies.store');
    Route::patch('/companies/{company}',  [CompaniesController::class, 'update'])->name('companies.update');
    Route::delete('/companies/{company}', [CompaniesController::class, 'destroy'])->name('companies.destroy');

    Route::get('/pricing-plans',                        [PricingPlansController::class, 'index'])->name('pricing-plans');
    Route::post('/pricing-plans',                       [PricingPlansController::class, 'store'])->name('pricing-plans.store');
    Route::patch('/pricing-plans/{pricingPlan}',        [PricingPlansController::class, 'update'])->name('pricing-plans.update');
    Route::delete('/pricing-plans/{pricingPlan}',       [PricingPlansController::class, 'destroy'])->name('pricing-plans.destroy');
    Route::patch('/pricing-plans/{pricingPlan}/toggle', [PricingPlansController::class, 'toggleActive'])->name('pricing-plans.toggle');

    Route::get('/apps',                         [AppsController::class, 'index'])->name('apps');
    Route::post('/apps',                        [AppsController::class, 'store'])->name('apps.store');
    Route::delete('/apps/{app}',                [AppsController::class, 'destroy'])->name('apps.destroy');
    Route::patch('/apps/{app}/toggle',          [AppsController::class, 'toggle'])->name('apps.toggle');
    Route::post('/apps/{app}/upload-module',    [AppsController::class, 'uploadModule'])->name('apps.upload-module');

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