<?php

use App\Http\Controllers\Admin\FinancialReportPdfController;
use App\Http\Controllers\Admin\OrderPdfController;
use App\Http\Controllers\Auth\GoogleController;
use App\Http\Controllers\Auth\MerchantAuthController;
use App\Livewire\CatalogPage;
use App\Livewire\Merchant\CartPage;
use App\Livewire\Merchant\CheckoutPage;
use App\Livewire\Merchant\MyOrdersPage;
use App\Livewire\Merchant\OrderDetailPage;
use App\Livewire\Merchant\ProductsPage;
use App\Livewire\Merchant\ProfilePage;
use Illuminate\Support\Facades\Route;

// Public catalog (accessible to everyone)
Route::get('/', CatalogPage::class)->name('catalog');

// Guest auth routes
Route::middleware('guest')->group(function () {
    Route::get('/login', [MerchantAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [MerchantAuthController::class, 'login'])->name('merchant.login.submit');
    Route::get('/register', [MerchantAuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [MerchantAuthController::class, 'register'])->name('register.submit');
});

// Google OAuth
Route::get('/auth/google', [GoogleController::class, 'redirect'])->name('auth.google');
Route::get('/auth/google/callback', [GoogleController::class, 'callback'])->name('auth.google.callback');

// Logout (authenticated only)
Route::post('/logout', [MerchantAuthController::class, 'logout'])->name('merchant.logout')->middleware('auth');

// Admin PDF routes (require auth + is_admin in controller)
Route::middleware(['web', 'auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/orders/{order}/invoice-pdf', [OrderPdfController::class, 'customerInvoice'])->name('orders.invoice-pdf');
    Route::get('/orders/{order}/waybill-pdf', [OrderPdfController::class, 'shippingWaybill'])->name('orders.waybill-pdf');
    Route::get('/purchase-orders/{purchaseOrder}/preparation-slip', [OrderPdfController::class, 'supplierPreparationSlip'])->name('purchase-orders.preparation-slip');
    Route::get('/financial-report/export-pdf', FinancialReportPdfController::class)->name('financial-report.export-pdf');
});

// Protected merchant pages (require login)
Route::middleware('auth')->prefix('merchant')->name('merchant.')->group(function () {
    Route::get('/products', ProductsPage::class)->name('products');
    Route::get('/cart', CartPage::class)->name('cart');
    Route::get('/checkout', CheckoutPage::class)->name('checkout');
    Route::get('/orders', MyOrdersPage::class)->name('orders');
    Route::get('/orders/{order}', OrderDetailPage::class)->name('orders.show');
    Route::get('/profile', ProfilePage::class)->name('profile');
});
