<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\ProductController;
use App\Http\Controllers\Api\CategoryController;
use App\Http\Controllers\Api\CartController;
use App\Http\Controllers\Api\OrderController;
use App\Http\Controllers\Api\CouponController;
use App\Http\Controllers\Api\WishlistController;
use App\Http\Controllers\Api\ReviewController;
use App\Http\Controllers\Api\AddressController;
use App\Http\Controllers\Api\PaymentController;
use App\Http\Controllers\Api\UserController;

/*
|--------------------------------------------------------------------------
| API Routes — Bdzoon E-Commerce
|--------------------------------------------------------------------------
*/

Route::prefix('v1')->group(function () {

    // ── AUTH (Public) ──────────────────────────────────────────────────
    Route::post('/register',            [AuthController::class, 'register']);
    Route::post('/login',               [AuthController::class, 'login']);
    Route::post('/login/otp',           [AuthController::class, 'loginWithOtp']);
    Route::post('/otp/send',            [AuthController::class, 'sendOtp']);
    Route::post('/password/email',      [AuthController::class, 'forgotPassword']);
    Route::post('/password/reset',      [AuthController::class, 'resetPassword']);

    // ── PRODUCTS (Public) ──────────────────────────────────────────────
    Route::get('/products',             [ProductController::class, 'index']);
    Route::get('/products/featured',    [ProductController::class, 'featured']);
    Route::get('/products/flash-deals', [ProductController::class, 'flash']);
    Route::get('/products/search',      [ProductController::class, 'search']);
    Route::get('/products/{slug}',      [ProductController::class, 'show']);

    // ── CATEGORIES (Public) ────────────────────────────────────────────
    Route::get('/categories',           [CategoryController::class, 'index']);
    Route::get('/categories/{slug}',    [CategoryController::class, 'show']);
    Route::get('/categories/{slug}/products', [CategoryController::class, 'products']);

    // ── REVIEWS (Public read) ──────────────────────────────────────────
    Route::get('/products/{id}/reviews', [ReviewController::class, 'index']);

    // ── COUPON VALIDATE (Public) ───────────────────────────────────────
    Route::post('/coupons/validate',    [CouponController::class, 'validate']);

    // ── ORDER TRACKING (Public) ────────────────────────────────────────
    Route::post('/orders/track',        [OrderController::class, 'track']);

    // ── PAYMENT WEBHOOKS (Public) ──────────────────────────────────────
    Route::post('/payment/bkash/callback',      [PaymentController::class, 'bkashCallback']);
    Route::post('/payment/sslcommerz/success',  [PaymentController::class, 'sslSuccess']);
    Route::post('/payment/sslcommerz/fail',     [PaymentController::class, 'sslFail']);
    Route::post('/payment/sslcommerz/ipn',      [PaymentController::class, 'sslIpn']);

    // ── AUTHENTICATED ROUTES ───────────────────────────────────────────
    Route::middleware('auth:sanctum')->group(function () {

        // Auth
        Route::post('/logout',          [AuthController::class, 'logout']);
        Route::get('/me',               [AuthController::class, 'me']);
        Route::put('/profile',          [UserController::class, 'update']);
        Route::post('/profile/avatar',  [UserController::class, 'uploadAvatar']);
        Route::get('/loyalty/points',   [UserController::class, 'loyaltyPoints']);

        // Addresses
        Route::apiResource('/addresses', AddressController::class);
        Route::patch('/addresses/{id}/default', [AddressController::class, 'setDefault']);

        // Cart
        Route::get('/cart',             [CartController::class, 'index']);
        Route::post('/cart',            [CartController::class, 'add']);
        Route::put('/cart/{id}',        [CartController::class, 'update']);
        Route::delete('/cart/{id}',     [CartController::class, 'remove']);
        Route::delete('/cart',          [CartController::class, 'clear']);

        // Wishlist
        Route::get('/wishlist',         [WishlistController::class, 'index']);
        Route::post('/wishlist/{productId}', [WishlistController::class, 'toggle']);

        // Orders
        Route::get('/orders',           [OrderController::class, 'index']);
        Route::post('/orders',          [OrderController::class, 'store']);
        Route::get('/orders/{id}',      [OrderController::class, 'show']);
        Route::post('/orders/{id}/cancel',  [OrderController::class, 'cancel']);
        Route::post('/orders/{id}/return',  [OrderController::class, 'requestReturn']);

        // Payments (initiate)
        Route::post('/payment/bkash/init',      [PaymentController::class, 'bkashInit']);
        Route::post('/payment/sslcommerz/init', [PaymentController::class, 'sslInit']);
        Route::post('/payment/cod',             [PaymentController::class, 'cod']);

        // Reviews
        Route::post('/products/{id}/reviews',  [ReviewController::class, 'store']);
        Route::put('/reviews/{id}',            [ReviewController::class, 'update']);
        Route::delete('/reviews/{id}',         [ReviewController::class, 'destroy']);
    });

    // ── ADMIN ROUTES ───────────────────────────────────────────────────
    Route::middleware(['auth:sanctum', 'role:admin'])->prefix('admin')->group(function () {

        Route::get('/dashboard',        [App\Http\Controllers\Api\Admin\DashboardController::class, 'index']);
        Route::get('/analytics/overview', [App\Http\Controllers\Api\Admin\AnalyticsController::class, 'overview']);
        Route::get('/analytics/sales',  [App\Http\Controllers\Api\Admin\AnalyticsController::class, 'sales']);

        Route::apiResource('/products', App\Http\Controllers\Api\Admin\ProductController::class);
        Route::post('/products/{id}/images', [App\Http\Controllers\Api\Admin\ProductController::class, 'uploadImages']);

        Route::apiResource('/categories', App\Http\Controllers\Api\Admin\CategoryController::class);

        Route::get('/orders',           [App\Http\Controllers\Api\Admin\OrderController::class, 'index']);
        Route::put('/orders/{id}/status', [App\Http\Controllers\Api\Admin\OrderController::class, 'updateStatus']);

        Route::get('/users',            [App\Http\Controllers\Api\Admin\UserController::class, 'index']);
        Route::put('/users/{id}/role',  [App\Http\Controllers\Api\Admin\UserController::class, 'updateRole']);

        Route::apiResource('/coupons',  App\Http\Controllers\Api\Admin\CouponController::class);
        Route::apiResource('/banners',  App\Http\Controllers\Api\Admin\BannerController::class);
    });
});
