<?php

declare(strict_types=1);

use App\Http\Controllers\Api\ServerHunterController;
use App\Http\Controllers\Api\V1\Admin\AdminAnalyticsController;
use App\Http\Controllers\Api\V1\Admin\AdminCustomerController;
use App\Http\Controllers\Api\V1\Admin\AdminServiceController;
use App\Http\Controllers\Api\V1\CustomerInvoiceController;
use App\Http\Controllers\Api\V1\CustomerServiceController;
use App\Http\Controllers\Api\V1\CustomerSubscriptionController;
use App\Http\Controllers\Api\V1\CustomerTicketController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| ServerHunter Feed
|--------------------------------------------------------------------------
|
| Public endpoint polled by the ServerHunter spider. Access is restricted
| to ServerHunter's published spider IPs via the AllowServerHunterSpider
| middleware.
|
*/
Route::middleware('serverhunter')
    ->get('serverhunter/feed', [ServerHunterController::class, 'feed'])
    ->name('api.serverhunter.feed');

Route::middleware(['auth:api', 'throttle:api'])->prefix('v1')->group(function (): void {
    // Services
    Route::get('services', [CustomerServiceController::class, 'index'])->name('api.v1.services.index');
    Route::get('services/{service}', [CustomerServiceController::class, 'show'])->name('api.v1.services.show');
    Route::post('services/{service}/reboot', [CustomerServiceController::class, 'reboot'])->name('api.v1.services.reboot');

    // Invoices
    Route::get('invoices', [CustomerInvoiceController::class, 'index'])->name('api.v1.invoices.index');
    Route::get('invoices/{invoice}/pdf', [CustomerInvoiceController::class, 'downloadPdf'])->name('api.v1.invoices.pdf');

    // Subscriptions
    Route::get('subscriptions', [CustomerSubscriptionController::class, 'index'])->name('api.v1.subscriptions.index');
    Route::post('subscriptions/{subscription}/cancel', [CustomerSubscriptionController::class, 'cancel'])->name('api.v1.subscriptions.cancel');

    // Tickets
    Route::get('tickets', [CustomerTicketController::class, 'index'])->name('api.v1.tickets.index');
    Route::post('tickets', [CustomerTicketController::class, 'store'])->name('api.v1.tickets.store');
    Route::get('tickets/{ticket}', [CustomerTicketController::class, 'show'])->name('api.v1.tickets.show');
    Route::post('tickets/{ticket}/reply', [CustomerTicketController::class, 'reply'])->name('api.v1.tickets.reply');
});

/*
|--------------------------------------------------------------------------
| Admin API Routes
|--------------------------------------------------------------------------
|
| These routes are protected by Passport authentication and require the
| admin role. Rate limited at 120 requests per minute.
|
*/
Route::middleware(['auth:api', 'role:admin', 'throttle:120,1'])->prefix('v1/admin')->group(function (): void {
    // Customers
    Route::get('customers', [AdminCustomerController::class, 'index'])->name('api.v1.admin.customers.index');
    Route::get('customers/{user}', [AdminCustomerController::class, 'show'])->name('api.v1.admin.customers.show');

    // Services
    Route::get('services', [AdminServiceController::class, 'index'])->name('api.v1.admin.services.index');
    Route::get('services/{service}', [AdminServiceController::class, 'show'])->name('api.v1.admin.services.show');
    Route::post('services/{service}/suspend', [AdminServiceController::class, 'suspend'])->name('api.v1.admin.services.suspend');
    Route::post('services/{service}/unsuspend', [AdminServiceController::class, 'unsuspend'])->name('api.v1.admin.services.unsuspend');

    // Analytics
    Route::get('analytics', [AdminAnalyticsController::class, 'index'])->name('api.v1.admin.analytics.index');
});
