<?php

use Illuminate\Support\Facades\Route;

// Public Controllers
use App\Http\Controllers\Public\HomeController;
use App\Http\Controllers\Public\InventoryController;
use App\Http\Controllers\Public\AuctionController;
use App\Http\Controllers\Public\CalculatorController;
use App\Http\Controllers\Public\PageController;
use App\Http\Controllers\Public\BlogController;
use App\Http\Controllers\Public\LeadController;
use App\Http\Controllers\Public\LocaleController;
use App\Http\Controllers\Auth\AuthController;

// Customer Controllers
use App\Http\Controllers\Customer\DashboardController as CustomerDashboardController;
use App\Http\Controllers\Customer\BidController as CustomerBidController;
use App\Http\Controllers\Customer\OrderController as CustomerOrderController;
use App\Http\Controllers\Customer\WatchlistController as CustomerWatchlistController;
use App\Http\Controllers\Customer\ProfileController as CustomerProfileController;
use App\Http\Controllers\Customer\NotificationController as CustomerNotificationController;

// Admin Controllers
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\BidAdminController;
use App\Http\Controllers\Admin\CustomerAdminController;
use App\Http\Controllers\Admin\InventoryAdminController;
use App\Http\Controllers\Admin\AuctionAdminController;
use App\Http\Controllers\Admin\OrderAdminController;
use App\Http\Controllers\Admin\PaymentAdminController;
use App\Http\Controllers\Admin\LeadAdminController;
use App\Http\Controllers\Admin\CalculatorAdminController;
use App\Http\Controllers\Admin\CMSAdminController;
use App\Http\Controllers\Admin\SEOAdminController;
use App\Http\Controllers\Admin\UserAdminController;
use App\Http\Controllers\Admin\SettingAdminController;
use App\Http\Controllers\Admin\AuditLogController;

/*
|--------------------------------------------------------------------------
| Public Routes
|--------------------------------------------------------------------------
*/
Route::get('/', [HomeController::class, 'index'])->name('home');

// Ready Stock Marketplace
Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index');
Route::get('/inventory/{slug}', [InventoryController::class, 'show'])->name('inventory.show');

// Japan Live Auctions & Bidding
Route::get('/auction', [AuctionController::class, 'index'])->name('auction.index');
Route::get('/auction/{auctionId}', [AuctionController::class, 'show'])->name('auction.show');
Route::post('/auction/{auctionId}/bid', [AuctionController::class, 'placeBid'])->name('auction.bid');

// Landing Pages & Services
Route::get('/preorder', [PageController::class, 'preorder'])->name('preorder');
Route::get('/calculator', [CalculatorController::class, 'index'])->name('calculator');
Route::post('/calculator/calculate', [CalculatorController::class, 'calculate'])->name('calculator.compute');
Route::get('/how-it-works', [PageController::class, 'howItWorks'])->name('how-it-works');
Route::get('/about', [PageController::class, 'about'])->name('about');
Route::get('/services', [PageController::class, 'services'])->name('services');
Route::get('/faq', [PageController::class, 'faq'])->name('faq');
Route::get('/auction-sheet', [PageController::class, 'auctionSheet'])->name('auction-sheet');
Route::get('/contact', [PageController::class, 'contact'])->name('contact');

// Blog CMS
Route::get('/blog', [BlogController::class, 'index'])->name('blog.index');
Route::get('/blog/{slug}', [BlogController::class, 'show'])->name('blog.show');

// Leads & Inquiries
Route::post('/lead', [LeadController::class, 'store'])->name('lead.store');

// Language Switcher
Route::get('/locale/{lang}', [LocaleController::class, 'switch'])->name('locale.switch');

// SEO XML Sitemap & Robots.txt
Route::get('/sitemap.xml', [SEOAdminController::class, 'sitemapXml'])->name('seo.sitemap');
Route::get('/robots.txt', [SEOAdminController::class, 'robotsTxt'])->name('seo.robots');

/*
|--------------------------------------------------------------------------
| Authentication Routes
|--------------------------------------------------------------------------
*/
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
Route::get('/forgot-password', [AuthController::class, 'showForgotPassword'])->name('password.request');
Route::post('/forgot-password', [AuthController::class, 'forgotPassword'])->name('password.email');

/*
|--------------------------------------------------------------------------
| Customer Portal (Authenticated Customers)
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->prefix('customer')->name('customer.')->group(function () {
    Route::get('/dashboard', [CustomerDashboardController::class, 'index'])->name('dashboard');
    Route::get('/bids', [CustomerBidController::class, 'index'])->name('bids.index');
    Route::get('/bids/{id}', [CustomerBidController::class, 'show'])->name('bids.show');
    Route::post('/bids/{id}/cancel', [CustomerBidController::class, 'cancel'])->name('bids.cancel');
    
    Route::get('/watchlist', [CustomerWatchlistController::class, 'index'])->name('watchlist');
    Route::post('/watchlist/toggle', [CustomerWatchlistController::class, 'toggle'])->name('watchlist.toggle');
    
    Route::get('/orders', [CustomerOrderController::class, 'index'])->name('orders.index');
    Route::get('/orders/{id}', [CustomerOrderController::class, 'show'])->name('orders.show');
    
    Route::get('/profile', [CustomerProfileController::class, 'show'])->name('profile');
    Route::post('/profile', [CustomerProfileController::class, 'update'])->name('profile.update');
    
    Route::get('/notifications', [CustomerNotificationController::class, 'index'])->name('notifications');
    Route::post('/notifications/read-all', [CustomerNotificationController::class, 'markAllAsRead'])->name('notifications.read-all');
});

/*
|--------------------------------------------------------------------------
| Admin Panel & Automotive CRM
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'role:super_admin,admin,auction_manager,sales_manager,accounts,content_manager,seo_manager'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');

        // Bids Management
        Route::get('/bids', [BidAdminController::class, 'index'])->name('bids.index');
        Route::get('/bids/{id}', [BidAdminController::class, 'show'])->name('bids.show');
        Route::post('/bids/{id}/approve', [BidAdminController::class, 'approveAndSend'])->name('bids.approve');
        Route::post('/bids/{id}/reject', [BidAdminController::class, 'reject'])->name('bids.reject');
        Route::post('/bids/{id}/won', [BidAdminController::class, 'markWon'])->name('bids.won');
        Route::post('/bids/{id}/lost', [BidAdminController::class, 'markLost'])->name('bids.lost');

        // Customers CRM
        Route::get('/customers', [CustomerAdminController::class, 'index'])->name('customers.index');
        Route::get('/customers/{id}', [CustomerAdminController::class, 'show'])->name('customers.show');
        Route::post('/customers/{id}/notes', [CustomerAdminController::class, 'updateNotes'])->name('customers.notes');
        Route::post('/customers/{id}/status', [CustomerAdminController::class, 'updateStatus'])->name('customers.status');

        // Ready Stock Inventory
        Route::get('/inventory', [InventoryAdminController::class, 'index'])->name('inventory.index');
        Route::get('/inventory/create', [InventoryAdminController::class, 'create'])->name('inventory.create');
        Route::post('/inventory', [InventoryAdminController::class, 'store'])->name('inventory.store');
        Route::get('/inventory/{id}/edit', [InventoryAdminController::class, 'edit'])->name('inventory.edit');
        Route::put('/inventory/{id}', [InventoryAdminController::class, 'update'])->name('inventory.update');
        Route::delete('/inventory/{id}', [InventoryAdminController::class, 'destroy'])->name('inventory.destroy');

        // Japan Live Auctions & API Sync
        Route::get('/auctions', [AuctionAdminController::class, 'index'])->name('auctions.index');
        Route::post('/auctions/sync', [AuctionAdminController::class, 'syncNow'])->name('auctions.sync');

        // Orders & 12-Step Import Tracking
        Route::get('/orders', [OrderAdminController::class, 'index'])->name('orders.index');
        Route::get('/orders/{id}', [OrderAdminController::class, 'show'])->name('orders.show');
        Route::post('/orders/{id}/tracking', [OrderAdminController::class, 'updateTracking'])->name('orders.tracking');
        Route::post('/orders/{id}/shipping', [OrderAdminController::class, 'updateShipping'])->name('orders.shipping');

        // Invoices & Payments
        Route::get('/payments', [PaymentAdminController::class, 'index'])->name('payments.index');
        Route::post('/payments/{id}/verify', [PaymentAdminController::class, 'verify'])->name('payments.verify');

        // Leads CRM
        Route::get('/leads', [LeadAdminController::class, 'index'])->name('leads.index');
        Route::post('/leads/{id}', [LeadAdminController::class, 'update'])->name('leads.update');

        // Calculator & Customs Tariff Rules
        Route::get('/calculator', [CalculatorAdminController::class, 'index'])->name('calculator.index');
        Route::post('/calculator/charge/{id}', [CalculatorAdminController::class, 'updateCharge'])->name('calculator.charge');
        Route::post('/calculator/tax-rule/{id}', [CalculatorAdminController::class, 'updateTaxRule'])->name('calculator.tax-rule');

        // CMS (Blog & FAQs)
        Route::get('/cms/blog', [CMSAdminController::class, 'blogIndex'])->name('cms.blog');
        Route::post('/cms/blog', [CMSAdminController::class, 'storePost'])->name('cms.blog.store');
        Route::delete('/cms/blog/{id}', [CMSAdminController::class, 'deletePost'])->name('cms.blog.destroy');
        Route::get('/cms/faqs', [CMSAdminController::class, 'faqIndex'])->name('cms.faqs');
        Route::post('/cms/faqs', [CMSAdminController::class, 'storeFaq'])->name('cms.faqs.store');
        Route::delete('/cms/faqs/{id}', [CMSAdminController::class, 'deleteFaq'])->name('cms.faqs.destroy');

        // SEO Management
        Route::get('/seo', [SEOAdminController::class, 'index'])->name('seo.index');
        Route::post('/seo/{id}', [SEOAdminController::class, 'update'])->name('seo.update');

        // Users & RBAC
        Route::get('/users', [UserAdminController::class, 'index'])->name('users.index');
        Route::post('/users', [UserAdminController::class, 'store'])->name('users.store');
        Route::post('/users/{id}/role', [UserAdminController::class, 'updateRole'])->name('users.role');

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

        // Audit Logs
        Route::get('/audit-logs', [AuditLogController::class, 'index'])->name('audit-logs.index');
    });
