<?php

namespace SoftminalB2B\Http;

use SoftminalB2B\Http\Controllers\BogoController;
use SoftminalB2B\Http\Controllers\CustomersController;
use SoftminalB2B\Http\Controllers\FeedbackController;
use SoftminalB2B\Http\Controllers\DocumentsController;
use SoftminalB2B\Import\ImportController;
use SoftminalB2B\Http\Controllers\GroupsController;
use SoftminalB2B\Http\Controllers\LookupController;
use SoftminalB2B\Http\Controllers\MessagesController;
use SoftminalB2B\Http\Controllers\BuyerMessagesController;
use SoftminalB2B\Http\Controllers\PriceRulesController;
use SoftminalB2B\Http\Controllers\PurchaseListsController;
use SoftminalB2B\Http\Controllers\PricingController;
use SoftminalB2B\Http\Controllers\QuantityRulesController;
use SoftminalB2B\Http\Controllers\RegistrationsController;
use SoftminalB2B\Http\Controllers\SettingsController;
use SoftminalB2B\Http\Controllers\StatusController;
use SoftminalB2B\Http\Controllers\StorefrontController;
use SoftminalB2B\Http\Routes\Route;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Admin API (`smb2b/v1`), per IMPLEMENTATION §2.
 *
 * Every route here manages shop configuration, so all of them require
 * `manage_woocommerce`. Buyer-facing and public routes arrive with Pro and
 * must be ownership-checked individually — none may inherit this group.
 */
Route::middleware('can:manage_woocommerce')->group(function () {

    Route::prefix('groups')->group(function () {
        Route::get('/', [GroupsController::class, 'index']);
        Route::post('/', [GroupsController::class, 'store']);
        Route::get('{id}', [GroupsController::class, 'show']);
        Route::put('{id}', [GroupsController::class, 'update']);
        Route::delete('{id}', [GroupsController::class, 'destroy']);

        Route::get('{id}/members', [GroupsController::class, 'members']);
        Route::post('{id}/members', [GroupsController::class, 'assign']);
    });

    Route::prefix('rules')->group(function () {
        Route::get('/', [PriceRulesController::class, 'index']);
        Route::post('/', [PriceRulesController::class, 'store']);
        Route::put('{id}', [PriceRulesController::class, 'update']);
        Route::delete('{id}', [PriceRulesController::class, 'destroy']);
    });

    Route::prefix('quantity-rules')->group(function () {
        Route::get('/', [QuantityRulesController::class, 'index']);
        Route::post('/', [QuantityRulesController::class, 'store']);
        Route::put('{id}', [QuantityRulesController::class, 'update']);
        Route::delete('{id}', [QuantityRulesController::class, 'destroy']);
    });

    Route::prefix('bogo')->group(function () {
        Route::get('/', [BogoController::class, 'index']);
        Route::post('/', [BogoController::class, 'store']);
        Route::put('{id}', [BogoController::class, 'update']);
        Route::delete('{id}', [BogoController::class, 'destroy']);
    });

    Route::prefix('registrations')->group(function () {
        Route::get('/', [RegistrationsController::class, 'index']);
        Route::post('approve', [RegistrationsController::class, 'approve']);
        Route::post('reject', [RegistrationsController::class, 'reject']);
        Route::post('{id}/approve', [RegistrationsController::class, 'approve']);
        Route::post('{id}/reject', [RegistrationsController::class, 'reject']);
    });

    Route::prefix('customers')->group(function () {
        Route::get('/', [CustomersController::class, 'index']);
        Route::put('{id}/group', [CustomersController::class, 'updateGroup']);
    });

    Route::prefix('documents')->group(function () {
        Route::get('/', [DocumentsController::class, 'index']);
        Route::post('select', [DocumentsController::class, 'select']);
        Route::get('{slug}/preview', [DocumentsController::class, 'preview']);
    });

    // Migration from other B2B plugins — free ("switch in 15 minutes").
    Route::prefix('import')->group(function () {
        Route::get('/', [ImportController::class, 'sources']);
        Route::get('{slug}/preview', [ImportController::class, 'preview']);
        Route::post('{slug}/run', [ImportController::class, 'run']);
    });

    Route::prefix('settings')->group(function () {
        Route::get('/', [SettingsController::class, 'index']);
        Route::post('/', [SettingsController::class, 'update']);
    });

    // The Price Explainer (A7) and the §2 `/price` query share one handler, so
    // the explanation can never diverge from the resolver.
    Route::get('price', [PricingController::class, 'explain']);

    Route::prefix('status')->group(function () {
        Route::get('/', [StatusController::class, 'index']);
        Route::get('revenue', [StatusController::class, 'revenue']);
        Route::get('sales', [StatusController::class, 'sales']);
        Route::post('complete-setup', [StatusController::class, 'completeSetup']);
        Route::post('create-registration-page', [StatusController::class, 'createRegistrationPage']);
        Route::post('dismiss-review', [StatusController::class, 'dismissReview']);
    });

    Route::prefix('lookup')->group(function () {
        Route::get('products', [LookupController::class, 'products']);
        Route::get('customers', [LookupController::class, 'customers']);
        Route::get('terms', [LookupController::class, 'terms']);
    });

    // Communication & Negotiations — merchant side. Basic messaging is Free;
    // company threads / convert-to-quote arrive with Pro on the same tables.
    Route::prefix('messages')->group(function () {
        Route::get('/', [MessagesController::class, 'index']);
        Route::get('unread-count', [MessagesController::class, 'unreadCount']);
        Route::get('{id}', [MessagesController::class, 'show']);
        Route::post('{id}/reply', [MessagesController::class, 'reply']);
        Route::post('{id}/status', [MessagesController::class, 'setStatus']);
        Route::delete('{id}', [MessagesController::class, 'destroy']);
    });
});

/**
 * Deactivation-survey feedback. Only someone who can deactivate the plugin can
 * submit it, and it forwards nothing unless they explicitly click Submit.
 */
Route::middleware('can:activate_plugins')->group(function () {
    Route::prefix('feedback')->group(function () {
        Route::post('/deactivation', [FeedbackController::class, 'deactivation']);
    });
});

/**
 * Storefront routes for the bulk order form. `can:read` means any logged-in
 * customer, not a shop manager — these serve buyers, and prices come from the
 * resolver, which already shows guests and pending applicants retail rates.
 */
Route::middleware('can:read')->group(function () {
    Route::prefix('storefront')->group(function () {
        Route::get('products', [StorefrontController::class, 'products']);
        Route::post('bulk-add', [StorefrontController::class, 'bulkAdd']);
        Route::post('quick-add', [StorefrontController::class, 'quickAdd']);

        // Saved purchase lists + one-click reorder. Every method re-checks that
        // the list belongs to the current user (ownership is not leaked).
        Route::get('purchase-lists', [PurchaseListsController::class, 'index']);
        Route::post('purchase-lists', [PurchaseListsController::class, 'store']);
        Route::put('purchase-lists/{id}', [PurchaseListsController::class, 'update']);
        Route::delete('purchase-lists/{id}', [PurchaseListsController::class, 'destroy']);
        Route::post('purchase-lists/{id}/reorder', [PurchaseListsController::class, 'reorder']);
        Route::post('purchase-lists/{id}/share', [PurchaseListsController::class, 'share']);
    });

    // Communication & Negotiations — buyer side. Every method re-checks that the
    // thread belongs to the current user (or their company on Pro).
    Route::prefix('my-messages')->group(function () {
        Route::get('/', [BuyerMessagesController::class, 'index']);
        Route::post('/', [BuyerMessagesController::class, 'store']);
        Route::get('unread-count', [BuyerMessagesController::class, 'unreadCount']);
        Route::get('{id}', [BuyerMessagesController::class, 'show']);
        Route::post('{id}/reply', [BuyerMessagesController::class, 'reply']);
    });
});
