<?php

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\CartController;
use App\Http\Controllers\Api\OrderController;
use App\Http\Controllers\Api\AbaPaymentController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\FaqController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\SlideController;
use App\Http\Controllers\Api\PageController;
use App\Http\Controllers\Api\SettingController;
use App\Http\Controllers\Api\PaymentMethodController;
use App\Http\Controllers\Api\ShippingMethodController;
use App\Http\Controllers\Api\BrandController;
use App\Http\Controllers\Api\UnitController;
use Illuminate\Support\Facades\Route;

// Public API Routes (No Authentication Required)
Route::prefix('v1')->group(function () {
    Route::prefix('auth')->group(function () {
        Route::post('/register', [AuthController::class, 'register']);
        Route::post('/login', [AuthController::class, 'login']);
        Route::post('/refresh', [AuthController::class, 'refresh']);

        Route::middleware('auth:api')->group(function () {
            Route::get('/user', [AuthController::class, 'me']);
            Route::post('/logout', [AuthController::class, 'logout']);

            // Orders
            Route::prefix('orders')->group(function () {
                Route::post('/', [OrderController::class, 'store']);
                Route::get('/', [OrderController::class, 'index']);
                Route::get('/{id}', [OrderController::class, 'show']);
            });

            // Wishlist
            Route::prefix('wishlists')->group(function () {
                Route::get('/', [\App\Http\Controllers\Api\WishlistController::class, 'index']);
                Route::post('/', [\App\Http\Controllers\Api\WishlistController::class, 'store']);
                Route::post('/toggle', [\App\Http\Controllers\Api\WishlistController::class, 'toggle']);
                Route::delete('/{productId}', [\App\Http\Controllers\Api\WishlistController::class, 'destroy']);
            });

            // Cart
            Route::prefix('cart')->group(function () {
                Route::get('/', [CartController::class, 'index']);
                Route::post('/', [CartController::class, 'store']);
                Route::delete('/{productId}', [CartController::class, 'destroy']);
            });

            // Payments (ABA PayWay Initiate)
            Route::prefix('payments')->group(function () {
                Route::post('/aba/initiate', [AbaPaymentController::class, 'initiate']);
            });
        });
    });

    // Public payment callback webhook
    Route::post('/payments/aba/callback', [AbaPaymentController::class, 'callback']);

    // Products - Public Routes
    Route::prefix('products')->group(function () {
        Route::get('/', [ProductController::class, 'index']); // List products with filters (category_id, search, etc.)
        Route::get('/{id}', [ProductController::class, 'show']); // Get single product by ID
        Route::get('/{id}/gallery', [ProductController::class, 'getGallery']); // Get product gallery images
    });

     // products sales
    Route::prefix('sales')->group(function () {
        Route::get('/', [ProductController::class, 'sales']); // List products with filters (category_id, search, etc.)
    });

    // Categories - Public Routes
    Route::prefix('categories')->group(function () {
        Route::get('/', [CategoryController::class, 'index']); // List categories (main or filtered by parent_id)
        Route::get('/{id}', [CategoryController::class, 'show']); // Get single category by ID
        Route::get('/{id}/subcategories', [CategoryController::class, 'getSubcategories']); // Get subcategories of a category
    });

    // Brands & Units - Public Routes
    Route::prefix('brands')->group(function () {
        Route::get('/', [BrandController::class, 'index']);
        Route::get('/featured', [BrandController::class, 'featured']);
    });
    Route::prefix('units')->group(function () {
        Route::get('/', [UnitController::class, 'index']);
    });

    // Slides - Public Routes
    Route::prefix('slides')->group(function () {
        Route::get('/', [SlideController::class, 'index']); // List slides (filtered by status)
        Route::get('/{id}', [SlideController::class, 'show']); // Get single slide by ID
    });

    // FAQs - Public Routes
    Route::prefix('faqs')->group(function () {
        Route::get('/', [FaqController::class, 'index']); // List FAQs (filter by status, search, sort, pagination)
        Route::get('/{faq}', [FaqController::class, 'show']); // Get single FAQ by ID
    });

    // Pages - Public Routes
    Route::prefix('pages')->group(function () {
        Route::get('/homepage', [PageController::class, 'homepage']); // Get home + home_promise sections
        Route::get('/{section}', [PageController::class, 'show']); // Get single page by section
    });

    // Settings - Public Routes
    Route::prefix('settings')->group(function () {
        Route::get('/', [SettingController::class, 'index']); // Get all settings
        Route::get('/group/{group}', [SettingController::class, 'getByGroup']); // Get settings by group
        Route::get('/{key}', [SettingController::class, 'show']); // Get single setting by key
    });

    Route::get('/payment-methods', [PaymentMethodController::class, 'index']);
    Route::get('/shipping-methods', [ShippingMethodController::class, 'index']);
});
