<?php

use App\Http\Controllers\Api\V1\ArticlesController;
use App\Http\Controllers\Api\V1\AuthController;
use App\Http\Controllers\Api\V1\DashboardController;
use App\Http\Controllers\Api\V1\FeedsController;
use App\Http\Controllers\Api\V1\KeywordsController;
use App\Http\Controllers\Api\V1\LogsController;
use App\Http\Controllers\Api\V1\OnboardingController;
use App\Http\Controllers\Api\V1\PlatformAccountsController;
use App\Http\Controllers\Api\V1\PlatformChannelsController;
use App\Http\Controllers\Api\V1\RouteArticlesController;
use App\Http\Controllers\Api\V1\RoutingController;
use App\Http\Controllers\Api\V1\SettingsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::prefix('v1')->group(function () {
    // Public authentication routes
    Route::post('/auth/login', [AuthController::class, 'login'])->name('api.auth.login');
    Route::post('/auth/register', [AuthController::class, 'register'])->name('api.auth.register');

    // Protected authentication routes
    Route::middleware('auth:sanctum')->group(function () {
        Route::post('/auth/logout', [AuthController::class, 'logout'])->name('api.auth.logout');
        Route::get('/auth/me', [AuthController::class, 'me'])->name('api.auth.me');
    });

    // Onboarding
    Route::get('/onboarding/status', [OnboardingController::class, 'status'])->name('api.onboarding.status');
    Route::get('/onboarding/options', [OnboardingController::class, 'options'])->name('api.onboarding.options');
    Route::post('/onboarding/complete', [OnboardingController::class, 'complete'])->name('api.onboarding.complete');
    Route::post('/onboarding/skip', [OnboardingController::class, 'skip'])->name('api.onboarding.skip');
    Route::post('/onboarding/reset-skip', [OnboardingController::class, 'resetSkip'])->name('api.onboarding.reset-skip');

    // Dashboard stats
    Route::get('/dashboard/stats', [DashboardController::class, 'stats'])->name('api.dashboard.stats');

    // Articles
    Route::get('/articles', [ArticlesController::class, 'index'])->name('api.articles.index');
    Route::post('/articles/refresh', [ArticlesController::class, 'refresh'])->name('api.articles.refresh');

    // Platform Accounts
    Route::apiResource('platform-accounts', PlatformAccountsController::class)->names([
        'index' => 'api.platform-accounts.index',
        'store' => 'api.platform-accounts.store',
        'show' => 'api.platform-accounts.show',
        'update' => 'api.platform-accounts.update',
        'destroy' => 'api.platform-accounts.destroy',
    ]);
    Route::post('/platform-accounts/{platformAccount}/set-active', [PlatformAccountsController::class, 'setActive'])
        ->name('api.platform-accounts.set-active');

    // Platform Channels
    Route::apiResource('platform-channels', PlatformChannelsController::class)->names([
        'index' => 'api.platform-channels.index',
        'store' => 'api.platform-channels.store',
        'show' => 'api.platform-channels.show',
        'update' => 'api.platform-channels.update',
        'destroy' => 'api.platform-channels.destroy',
    ]);
    Route::post('/platform-channels/{channel}/toggle', [PlatformChannelsController::class, 'toggle'])
        ->name('api.platform-channels.toggle');
    Route::post('/platform-channels/{channel}/accounts', [PlatformChannelsController::class, 'attachAccount'])
        ->name('api.platform-channels.attach-account');
    Route::delete('/platform-channels/{channel}/accounts/{account}', [PlatformChannelsController::class, 'detachAccount'])
        ->name('api.platform-channels.detach-account');
    Route::put('/platform-channels/{channel}/accounts/{account}', [PlatformChannelsController::class, 'updateAccountRelation'])
        ->name('api.platform-channels.update-account-relation');

    // Feeds
    Route::apiResource('feeds', FeedsController::class)->names([
        'index' => 'api.feeds.index',
        'store' => 'api.feeds.store',
        'show' => 'api.feeds.show',
        'update' => 'api.feeds.update',
        'destroy' => 'api.feeds.destroy',
    ]);
    Route::post('/feeds/{feed}/toggle', [FeedsController::class, 'toggle'])->name('api.feeds.toggle');

    // Routing
    Route::get('/routing', [RoutingController::class, 'index'])->name('api.routing.index');
    Route::post('/routing', [RoutingController::class, 'store'])->name('api.routing.store');
    Route::get('/routing/{feed}/{channel}', [RoutingController::class, 'show'])->name('api.routing.show');
    Route::put('/routing/{feed}/{channel}', [RoutingController::class, 'update'])->name('api.routing.update');
    Route::delete('/routing/{feed}/{channel}', [RoutingController::class, 'destroy'])->name('api.routing.destroy');
    Route::post('/routing/{feed}/{channel}/toggle', [RoutingController::class, 'toggle'])->name('api.routing.toggle');

    // Keywords
    Route::get('/routing/{feed}/{channel}/keywords', [KeywordsController::class, 'index'])->name('api.keywords.index');
    Route::post('/routing/{feed}/{channel}/keywords', [KeywordsController::class, 'store'])->name('api.keywords.store');
    Route::put('/routing/{feed}/{channel}/keywords/{keyword}', [KeywordsController::class, 'update'])->name('api.keywords.update');
    Route::delete('/routing/{feed}/{channel}/keywords/{keyword}', [KeywordsController::class, 'destroy'])->name('api.keywords.destroy');
    Route::post('/routing/{feed}/{channel}/keywords/{keyword}/toggle', [KeywordsController::class, 'toggle'])->name('api.keywords.toggle');

    // Route Articles
    Route::get('/route-articles', [RouteArticlesController::class, 'index'])->name('api.route-articles.index');
    Route::post('/route-articles/clear', [RouteArticlesController::class, 'clear'])->name('api.route-articles.clear');
    Route::post('/route-articles/{routeArticle}/approve', [RouteArticlesController::class, 'approve'])->name('api.route-articles.approve');
    Route::post('/route-articles/{routeArticle}/reject', [RouteArticlesController::class, 'reject'])->name('api.route-articles.reject');
    Route::post('/route-articles/{routeArticle}/restore', [RouteArticlesController::class, 'restore'])->name('api.route-articles.restore');

    // Settings
    Route::get('/settings', [SettingsController::class, 'index'])->name('api.settings.index');
    Route::put('/settings', [SettingsController::class, 'update'])->name('api.settings.update');

    // Logs
    Route::get('/logs', [LogsController::class, 'index'])->name('api.logs.index');
});
