<?php

use App\Http\Controllers\Api\AdminController;
use App\Http\Controllers\Api\ImportController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\CmsController;
use App\Http\Controllers\Api\UploadController;
use App\Http\Controllers\Api\CartController;
use App\Http\Controllers\Api\CategoryController;
use App\Http\Controllers\Api\CheckoutController;
use App\Http\Controllers\Api\PaymentController;
use App\Http\Controllers\Api\ShipmentController;
use App\Http\Controllers\Api\StockNotificationController;
use App\Http\Controllers\Api\LabReportController;
use App\Http\Controllers\Api\SlideController;
use App\Http\Controllers\Api\ProductController;
use App\Http\Controllers\Api\GiftTierController;
use App\Http\Controllers\Api\NavigationController;
use App\Http\Controllers\Api\AnnouncementController;
use Illuminate\Support\Facades\Route;

// Public auth routes
Route::prefix('auth')->group(function () {
    Route::post('/register', [AuthController::class, 'register']);
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/forgot-password', [AuthController::class, 'forgotPassword']);
    Route::post('/reset-password', [AuthController::class, 'resetPassword']);

    Route::get('/google/redirect', [AuthController::class, 'googleRedirect']);
    Route::get('/google/callback', [AuthController::class, 'googleCallback']);
    Route::get('/facebook/redirect', [AuthController::class, 'facebookRedirect']);
    Route::get('/facebook/callback', [AuthController::class, 'facebookCallback']);
});

// Authenticated routes
Route::middleware('auth:sanctum')->group(function () {
    Route::prefix('auth')->group(function () {
        Route::post('/logout', [AuthController::class, 'logout']);
        Route::get('/me', [AuthController::class, 'me']);
        Route::put('/profile', [AuthController::class, 'updateProfile']);
        Route::put('/password', [AuthController::class, 'changePassword']);
        Route::post('/verify-age', [AuthController::class, 'verifyAge']);
        Route::delete('/account', [AuthController::class, 'deleteAccount']);
        Route::get('/loyalty', [AuthController::class, 'loyaltyTransactions']);
        Route::get('/orders', [AuthController::class, 'orders']);
        Route::get('/addresses', [AuthController::class, 'addresses']);
        Route::post('/addresses', [AuthController::class, 'storeAddress']);
        Route::put('/addresses/{id}', [AuthController::class, 'updateAddress']);
        Route::delete('/addresses/{id}', [AuthController::class, 'destroyAddress']);
        Route::get('/discount-codes', [AuthController::class, 'discountCodes']);
    });
});

// Public CMS routes
Route::get('/pages/{page}', [CmsController::class, 'getPage']);
Route::get('/blog', [CmsController::class, 'posts']);
Route::get('/blog/categories', [CmsController::class, 'categories']);
Route::get('/blog/{slug}', [CmsController::class, 'post']);
Route::post('/blog/{postId}/comments', [CmsController::class, 'submitComment']);

// Public product & category routes
Route::get('/products/search', [ProductController::class, 'search']);
Route::get('/products/featured', [ProductController::class, 'featured']);
Route::get('/products/bestsellers', [ProductController::class, 'bestsellers']);
Route::get('/products/{slug}', [ProductController::class, 'show']);
Route::get('/products', [ProductController::class, 'index']);
Route::get('/categories', [CategoryController::class, 'index']);

// Public tracking config (pixel IDs only — no secrets)
Route::get('/settings/tracking', [AdminController::class, 'publicTrackingConfig']);

// Cart — public (session-based), optional auth
Route::prefix('cart')->group(function () {
    Route::get('/', [CartController::class, 'index']);
    Route::post('/add', [CartController::class, 'add']);
    Route::put('/update', [CartController::class, 'update']);
    Route::delete('/remove', [CartController::class, 'remove']);
    Route::post('/apply-discount', [CartController::class, 'applyDiscount']);
    Route::delete('/remove-discount', [CartController::class, 'removeDiscount']);
});

// Admin routes — requires auth + admin role
Route::middleware(['auth:sanctum', 'role:admin'])->prefix('admin')->group(function () {
    Route::get('/dashboard', [AdminController::class, 'dashboard']);
    Route::get('/analytics/channels', [AdminController::class, 'analyticsChannels']);

    Route::get('/orders', [AdminController::class, 'orders']);
    Route::get('/orders/{id}', [AdminController::class, 'order']);
    Route::patch('/orders/{id}/status', [AdminController::class, 'updateOrderStatus']);
    Route::post('/orders/{id}/ship', [ShipmentController::class, 'ship']);
    Route::get('/orders/{id}/label', [ShipmentController::class, 'label']);
    Route::get('/shipment/config', [ShipmentController::class, 'config']);

    Route::get('/products', [AdminController::class, 'products']);
    Route::post('/products', [AdminController::class, 'createProduct']);
    Route::get('/products/{id}', [AdminController::class, 'product']);
    Route::put('/products/{id}', [AdminController::class, 'updateProduct']);
    Route::delete('/products/{id}', [AdminController::class, 'deleteProduct']);

    // Product images
    Route::post('/products/{id}/images', [AdminController::class, 'uploadProductImage']);
    Route::delete('/product-images/{imageId}', [AdminController::class, 'deleteProductImage']);
    Route::patch('/product-images/{imageId}/primary', [AdminController::class, 'setProductImagePrimary']);

    // Product variants
    Route::post('/products/{id}/variants', [AdminController::class, 'createVariant']);
    Route::put('/variants/{variantId}', [AdminController::class, 'updateVariant']);
    Route::delete('/variants/{variantId}', [AdminController::class, 'deleteVariant']);

    // Upload
    Route::post('/upload/image', [UploadController::class, 'image']);

    Route::get('/customers', [AdminController::class, 'customers']);
    Route::get('/customers/{id}', [AdminController::class, 'customer']);

    Route::get('/discount-codes', [AdminController::class, 'discountCodes']);
    Route::post('/discount-codes', [AdminController::class, 'createDiscountCode']);
    Route::patch('/discount-codes/{id}', [AdminController::class, 'updateDiscountCode']);
    Route::delete('/discount-codes/{id}', [AdminController::class, 'deleteDiscountCode']);

    // CMS - Pages
    Route::put('/pages/{page}', [CmsController::class, 'updatePage']);

    // CMS - Blog
    Route::get('/blog-admin', [CmsController::class, 'adminPosts']);
    Route::post('/blog', [CmsController::class, 'createPost']);
    Route::put('/blog/{id}', [CmsController::class, 'updatePost']);
    Route::delete('/blog/{id}', [CmsController::class, 'deletePost']);

    // CMS - Categories
    Route::post('/blog-categories', [CmsController::class, 'createCategory']);
    Route::put('/blog-categories/{id}', [CmsController::class, 'updateCategory']);
    Route::delete('/blog-categories/{id}', [CmsController::class, 'deleteCategory']);

    // CMS - Comments
    Route::get('/comments', [CmsController::class, 'adminComments']);
    Route::patch('/comments/{id}', [CmsController::class, 'updateComment']);
    Route::delete('/comments/{id}', [CmsController::class, 'deleteComment']);

    // Subscribers (newsletter)
    Route::get('/subscribers', [AdminController::class, 'subscribers']);

    // Stock notifications
    Route::get('/stock-notifications', [StockNotificationController::class, 'index']);
    Route::post('/stock-notifications/trigger/{productId}', [StockNotificationController::class, 'trigger']);

    // Slides admin CRUD
    Route::get('/slides', [SlideController::class, 'adminIndex']);

    // Gift tiers
    Route::get('/gift-tiers', [GiftTierController::class, 'adminIndex']);

    // Navigation
    Route::get('/navigation', [NavigationController::class, 'adminIndex']);
    Route::post('/navigation', [NavigationController::class, 'store']);
    Route::put('/navigation/reorder', [NavigationController::class, 'reorder']);
    Route::put('/navigation/{id}', [NavigationController::class, 'update']);
    Route::delete('/navigation/{id}', [NavigationController::class, 'destroy']);
    Route::post('/gift-tiers', [GiftTierController::class, 'store']);
    Route::put('/gift-tiers/{id}', [GiftTierController::class, 'update']);
    Route::delete('/gift-tiers/{id}', [GiftTierController::class, 'destroy']);
    Route::post('/slides', [SlideController::class, 'store']);
    Route::put('/slides/reorder', [SlideController::class, 'reorder']);
    Route::put('/slides/{slide}', [SlideController::class, 'update']);
    Route::delete('/slides/{slide}', [SlideController::class, 'destroy']);

    // Announcement bar admin CRUD
    Route::get('/announcements', [AnnouncementController::class, 'adminIndex']);
    Route::post('/announcements', [AnnouncementController::class, 'store']);
    Route::put('/announcements/reorder', [AnnouncementController::class, 'reorder']);
    Route::put('/announcements/{id}', [AnnouncementController::class, 'update']);
    Route::delete('/announcements/{id}', [AnnouncementController::class, 'destroy']);

    // Lab reports admin CRUD
    Route::get('/lab-reports', [LabReportController::class, 'adminIndex']);
    Route::post('/lab-reports', [LabReportController::class, 'store']);
    Route::put('/lab-reports/{id}', [LabReportController::class, 'update']);
    Route::delete('/lab-reports/{id}', [LabReportController::class, 'destroy']);

    // Collections (product categories)
    Route::get('/collections', [AdminController::class, 'collections']);
    Route::post('/collections', [AdminController::class, 'createCollection']);
    Route::put('/collections/{id}', [AdminController::class, 'updateCollection']);
    Route::delete('/collections/{id}', [AdminController::class, 'deleteCollection']);

    // Store settings
    Route::get('/settings', [AdminController::class, 'getSettings']);
    Route::put('/settings', [AdminController::class, 'updateSettings']);

    // Shopify CSV Import
    Route::post('/import/preview', [ImportController::class, 'preview']);
    Route::post('/import/products', [ImportController::class, 'import']);

    // Upload
    Route::post('/upload/image', [UploadController::class, 'image']);
    Route::post('/upload/pdf', [UploadController::class, 'pdf']);
});

// Lab Reports — public
Route::get('/lab-reports', [LabReportController::class, 'index']);

// Slides — public
Route::get('/slides', [SlideController::class, 'index']);

// Gift tiers — public
Route::get('/gift-tiers', [GiftTierController::class, 'index']);

// Navigation — public
Route::get('/navigation', [NavigationController::class, 'index']);

// Announcement bar — public
Route::get('/announcements', [AnnouncementController::class, 'index']);

// Stock Notifications — public subscribe
Route::post('/stock-notify', [StockNotificationController::class, 'subscribe']);

// Newsletter subscription — public
Route::post('/subscribe', [\App\Http\Controllers\Api\NewsletterController::class, 'subscribe']);

// Admin stock notifications
// (routes inside the admin middleware block below)

// Checkout
Route::prefix('checkout')->group(function () {
    Route::post('/order', [CheckoutController::class, 'createOrder']);
    Route::get('/rates', [CheckoutController::class, 'shippingRates']);
    Route::get('/address-lookup', [CheckoutController::class, 'addressLookup']);
    Route::middleware('auth:sanctum')->get('/order/{orderNumber}', [CheckoutController::class, 'getOrder']);

    // Payment — public (guest + auth both supported)
    Route::get('/payment/config', [PaymentController::class, 'config']);
    Route::middleware('auth:sanctum')->post('/payment/create', [PaymentController::class, 'create']);
    Route::post('/payment/create-guest', [PaymentController::class, 'create']); // no auth required
    Route::get('/payment/verify/{sessionId}', [PaymentController::class, 'verify']);
    Route::post('/payment/webhook/{gateway}', [PaymentController::class, 'webhook']);
});
