<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\CheckoutController;
use App\Http\Controllers\Api\ConfigurationController;
use App\Http\Controllers\Api\CustomerController;
use App\Http\Controllers\Api\HomeController;
use App\Http\Controllers\Api\OrderController;
use App\Http\Controllers\Api\ProductController;
use App\Http\Controllers\Api\ReviewController;
use App\Http\Controllers\Api\SyncController;
use App\Http\Controllers\Api\UploadController;
use Illuminate\Support\Facades\Broadcast;

Broadcast::routes(['middleware' => ['auth:sanctum']]);

// --- Public / Auth Routes ---
Route::post('/auth/login', [AuthController::class, 'login']);
Route::post('/auth/register', [AuthController::class, 'register']);

// --- Protected Routes (Perlu Login) ---
Route::middleware('auth:sanctum')->group(function () {
    // Media Upload
    Route::post('/uploads/images', [UploadController::class, 'uploadImage']);
    
    // Core Synchronization
    Route::prefix('sync')->group(function () {
        Route::post('/up', [SyncController::class, 'batchUpSync']);
        Route::get('/down', [SyncController::class, 'getDeltaSync']);
    });

    // Order Synchronization
    Route::get('orders/sync/{orderId}', [OrderController::class, 'getOrderSync']);
});

// --- Tenant Specific Routes (Ecommerce / Client Side) ---
Route::middleware('identify.tenant')->group(function() {

    Route::prefix('tenant')->group(function() {
        Route::get('/', [ConfigurationController::class, 'getConfigurationService']);
    });
    
    // Home & Storefront
    Route::get('/home/{outletId}', [HomeController::class, 'home']);
    Route::get('/outlets', [HomeController::class, 'getOutlets']);
    Route::get('/outlets/main', [HomeController::class, 'getMainOutlet']);

    // Products
    Route::prefix('products')->group(function () {
        Route::get('/{id}', [ProductController::class, 'getProductById']);
        Route::get('/search/{outletId}', [ProductController::class, 'getProductByName']); // Ganti dari search-products
        Route::get('/categories/{outletId}/{categoryId}', [ProductController::class, 'getProductByCategory']);
        Route::get('/best-sellers/{outletId}', [ProductController::class, 'getProductBestSellerByOutlet']);
    });

    // Orders & Checkout
    Route::post('/checkout', [CheckoutController::class, 'store']);
    
    Route::prefix('orders')->group(function () {
        Route::get('/customer/{customerId}', [OrderController::class, 'getOrders']);
        Route::get('/customer/{customerId}/{orderId}', [OrderController::class, 'getOrderById']);
        Route::patch('/{customerId}/{orderId}/cancel', [OrderController::class, 'cancelOrder']);
        Route::patch('/{customerId}/{orderId}/cancellation-request', [OrderController::class, 'cancellationRequest']);
    });

    Route::prefix('review')->group(function() {
        Route::get('/{orderId}', [ReviewController::class, 'getProductReviewItem']);
        Route::post('/{orderId}', [ReviewController::class, 'reviewProduct']);
    });

    Route::prefix('customer')->group(function() {
        Route::post('/auth/login', [CustomerController::class, 'login']);
        Route::post('/auth/register', [CustomerController::class, 'register']);
        Route::patch('/{customerId}', [CustomerController::class, 'updateCustomer']);
        Route::patch('/{customerId}/password', [CustomerController::class, 'updatePassword']);
        Route::post('/{customerId}', [CustomerController::class, 'saveCustomerAddress']);
        Route::get('/{customerId}/addresses', [CustomerController::class, 'getCustomerAddress']);
        Route::delete('/{customerId}/addresses/{id}', [CustomerController::class, 'deleteCustomerAddress']);
    });
});
