<?php

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\BillingController;
use App\Http\Controllers\Api\BusinessController;
use App\Http\Controllers\Api\ChartOfAccountController;
use App\Http\Controllers\Api\ContactController;
use App\Http\Controllers\Api\DocumentController;
use App\Http\Controllers\Api\InternalController;
use App\Http\Controllers\Api\LookupController;
use App\Http\Controllers\Api\PaymentMethodController;
use App\Http\Controllers\Api\UserController;
use App\Http\Controllers\Api\VatGstRateController;
use Illuminate\Support\Facades\Route;

// Laravel routes are case-sensitive by default; the frontend mixes /api/Documents
// and /api/documents. We register both. URL helpers/middleware are not affected.

// ---------------- Auth ----------------
foreach (['user', 'User'] as $userSeg) {
    Route::prefix($userSeg)->group(function () {
        Route::post('register', [AuthController::class, 'register']);
        Route::post('login', [AuthController::class, 'login']);
        Route::post('verify/{token}', [AuthController::class, 'verify']);
        Route::post('password/forget', [AuthController::class, 'forgetPassword']);
        Route::post('password/reset', [AuthController::class, 'resetPassword']);
        Route::post('refresh-token', [AuthController::class, 'refresh']);
    });
}

// ---------------- Authenticated ----------------
Route::middleware('auth:sanctum')->group(function () {

    // Lookups for dropdowns (case variations)
    foreach (['currency', 'Currency', 'currencies', 'Currencies'] as $seg) {
        Route::get($seg, [LookupController::class, 'currencies']);
    }
    foreach (['countries', 'country', 'Countries', 'Country'] as $seg) {
        Route::get($seg, [LookupController::class, 'countries']);
    }
    foreach (['states', 'state', 'States', 'State'] as $seg) {
        Route::get($seg, [LookupController::class, 'states']);
    }
    Route::get('business-types', [LookupController::class, 'businessTypes']);

    Route::get('user/me', [AuthController::class, 'me']);
    Route::get('User', [AuthController::class, 'me']); // swagger: GET /api/User
    Route::get('user', [AuthController::class, 'me']);
    Route::post('user/logout', [AuthController::class, 'logout']);
    Route::post('User/logout', [AuthController::class, 'logout']);
    Route::post('user/password/change', [AuthController::class, 'changePassword']);
    Route::post('User/password/change', [AuthController::class, 'changePassword']);

    // Invoice lookups (swagger)
    Route::get('InvoiceTitle', [LookupController::class, 'invoiceTitles']);
    Route::get('invoicetitle', [LookupController::class, 'invoiceTitles']);
    Route::get('InvoiceRecordAs', [LookupController::class, 'invoiceRecordAs']);
    Route::get('invoicerecordas', [LookupController::class, 'invoiceRecordAs']);

    // Users
    Route::get('users/{userId}', [UserController::class, 'show']);
    Route::put('users/{userId}', [UserController::class, 'update']);
    Route::post('users/{userId}/profile-picture', [UserController::class, 'uploadProfilePicture']);
    Route::delete('users/{userId}/profile-picture', [UserController::class, 'deleteProfilePicture']);
    Route::get('users/{userId}/activity', [UserController::class, 'activity']);

    // Businesses (case variations to match swagger /api/Business)
    foreach (['business', 'Business'] as $seg) {
        Route::get($seg . '/list', [BusinessController::class, 'index']);
        Route::post($seg . '/create', [BusinessController::class, 'store']);
        Route::get($seg . '/{id}', [BusinessController::class, 'show'])->where('id', '[0-9]+');
        Route::put($seg . '/{id}', [BusinessController::class, 'update'])->where('id', '[0-9]+');
    }

    // Documents — register under both casings to match the React calls verbatim.
    foreach (['Documents', 'documents'] as $segment) {
        Route::prefix($segment)->group(function () use ($segment) {
            // Generic
            Route::post('create', [DocumentController::class, 'upload']);
            Route::get('Documentlist', [DocumentController::class, 'internalList']);
            Route::get('exportDocument', [DocumentController::class, 'exportDocument']);
            Route::get('exportDataFile', [DocumentController::class, 'exportDataFile']);
            Route::get('GetImage', [DocumentController::class, 'getImage']);
            Route::get('getDocumentHistory', [DocumentController::class, 'history']);
            Route::get('getDocumnetsByContact', [DocumentController::class, 'byContact']);
            Route::get('getDocumentsByContact', [DocumentController::class, 'byContact']);

            Route::put('exported', [DocumentController::class, 'markExported']);
            Route::put('bulkProcess', [DocumentController::class, 'bulkProcess']);
            Route::put('bulkReject', [DocumentController::class, 'bulkReject']);
            Route::put('save/{id}', [DocumentController::class, 'saveData']);
            Route::put('approve/{id}', [DocumentController::class, 'approve']);
            Route::put('processed/{id}', [DocumentController::class, 'processed'])->where('id', '[0-9]+');
            Route::put('restore/{id}', [DocumentController::class, 'restore'])->where('id', '[0-9]+');
            Route::put('reAssign/{id}', [DocumentController::class, 'reAssign']);
            Route::put('attachDoc/{id}', [DocumentController::class, 'attachDoc']);
            Route::put('reclassify', [DocumentController::class, 'reclassify']);

            Route::post('{id}/transfer/{businessId}', [DocumentController::class, 'transfer'])
                ->where('id', '[0-9]+')->where('businessId', '[0-9]+');

            Route::delete('deleteDocuments', [DocumentController::class, 'deleteDocuments']);
            Route::delete('{id}', [DocumentController::class, 'destroy']);

            Route::get('{businessId}/DeletedDocs', [DocumentController::class, 'deletedList']);
            Route::get('{businessId}/{docType}/list', [DocumentController::class, 'listByType']);
            // Internal data-entry pages need a wrapped "docs[].documents" shape.
            Route::get('{businessId}/{docType}/InvoiceList', [DocumentController::class, 'internalListByType']);
            Route::get('{businessId}/{docType}/invoicelist', [DocumentController::class, 'internalListByType']);

            Route::get('{id}', [DocumentController::class, 'show']);

            // Dynamic status PUT: PUT /api/Documents/{status}/{id}
            // Constrained to known status names so it doesn't shadow other routes.
            Route::put('{status}/{id}', [DocumentController::class, 'updateStatus'])
                ->where('status', 'Uploaded|Data%20Entered|DataEntered|Processing|Processed|Exported|Review|Deleted');
        });
    }

    // Contacts
    // Contacts (case variations to match all callers in the frontend)
    foreach (['contacts', 'Contacts'] as $seg) {
        Route::post($seg, [ContactController::class, 'store']);
        Route::get($seg . '/{businessId}', [ContactController::class, 'index']);
        Route::post($seg . '/{businessId}', [ContactController::class, 'store']);
        Route::put($seg . '/{businessId}/{id}', [ContactController::class, 'update']);
        Route::delete($seg . '/{businessId}/{id}', [ContactController::class, 'destroy']);
    }

    // Chart of accounts
    foreach (['chartofaccounts', 'ChartOfAccounts'] as $seg) {
        Route::post($seg, [ChartOfAccountController::class, 'store']);
        Route::get($seg . '/{businessId}', [ChartOfAccountController::class, 'index']);
        Route::post($seg . '/{businessId}', [ChartOfAccountController::class, 'store']);
        Route::put($seg . '/{businessId}/{id}', [ChartOfAccountController::class, 'update']);
        Route::delete($seg . '/{businessId}/{id}', [ChartOfAccountController::class, 'destroy']);
    }

    // Payment methods
    foreach (['paymentmethods', 'PaymentMethods'] as $seg) {
        Route::post($seg, [PaymentMethodController::class, 'store']);
        Route::get($seg . '/{businessId}', [PaymentMethodController::class, 'index']);
        Route::post($seg . '/{businessId}', [PaymentMethodController::class, 'store']);
        Route::put($seg . '/{businessId}/{id}', [PaymentMethodController::class, 'update']);
        Route::delete($seg . '/{businessId}/{id}', [PaymentMethodController::class, 'destroy']);
    }

    // VAT/GST
    foreach (['vatgst', 'VATGST', 'VatGst'] as $seg) {
        Route::post($seg, [VatGstRateController::class, 'store']);
        Route::get($seg . '/{businessId}', [VatGstRateController::class, 'index']);
        Route::post($seg . '/{businessId}', [VatGstRateController::class, 'store']);
        Route::put($seg . '/{businessId}/{id}', [VatGstRateController::class, 'update']);
        Route::delete($seg . '/{businessId}/{id}', [VatGstRateController::class, 'destroy']);
    }

    // Internal / back-office ("Service Portal") admin views
    foreach (['internal', 'Internal'] as $seg) {
        Route::get($seg . '/user-activity', [InternalController::class, 'userActivity']);
        Route::get($seg . '/exceptions', [InternalController::class, 'exceptions']);
        Route::get($seg . '/export-errors', [InternalController::class, 'exportErrors']);
        Route::get($seg . '/deleted-summary', [InternalController::class, 'deletedSummary']);
        Route::get($seg . '/processing-summary', [InternalController::class, 'processingSummary']);
        Route::get($seg . '/upload-history', [InternalController::class, 'uploadHistory']);
    }

    // Billing
    Route::get('billing/{businessId}', [BillingController::class, 'show']);
    Route::get('billing/{businessId}/history', [BillingController::class, 'history']);
    Route::get('billing/{businessId}/usage', [BillingController::class, 'usage']);
    Route::get('subscription/{businessId}', [BillingController::class, 'subscription']);
});
