<?php

use App\Http\Controllers\CommunityController;
use App\Http\Controllers\RateMyPayController;
use Illuminate\Support\Facades\Route;

Route::middleware('auth')->prefix('communities')->group(function () {
    // Route::get('/', [CommunityController::class, 'index'])->name('communities.index');
    Route::get('/', [CommunityController::class, 'networkIndex'])->name('communities.index');
    Route::get('/my-communities', [CommunityController::class, 'myCommunities'])->name('communities.my');
    Route::get('/create', [CommunityController::class, 'create'])->name('communities.create');
    Route::post('/', [CommunityController::class, 'store'])->name('communities.store');
    Route::get('/{community}', [CommunityController::class, 'show'])->name('communities.show');
    Route::get('/{community}/edit', [CommunityController::class, 'edit'])->name('communities.edit');
    Route::put('/{community}', [CommunityController::class, 'update'])->name('communities.update');
    Route::delete('/{community}', [CommunityController::class, 'destroy'])->name('communities.destroy');
    
    // Membership routes
    Route::post('/{community}/join', [CommunityController::class, 'join'])->name('communities.join');
    Route::post('/{community}/leave', [CommunityController::class, 'leave'])->name('communities.leave');
    
    // Member management
    Route::get('/{community}/members', [CommunityController::class, 'members'])->name('communities.members');
    Route::put('/{community}/members/{user}/role', [CommunityController::class, 'updateMemberRole'])->name('communities.members.update-role');
    Route::delete('/{community}/members/{user}', [CommunityController::class, 'removeMember'])->name('communities.members.remove');
    
    // Rate My Pay posts under communities prefix
    Route::prefix('rate-my-pay')->group(function () {
        Route::get('/{rateMyPay}', [RateMyPayController::class, 'show'])->name('rate-my-pay.show');
        Route::post('/{rateMyPay}/rate', [RateMyPayController::class, 'rate'])->name('rate-my-pay.rate');
        Route::delete('/{rateMyPay}/rating', [RateMyPayController::class, 'removeRating'])->name('rate-my-pay.remove-rating');
        Route::post('/{rateMyPay}/comments', [RateMyPayController::class, 'storeComment'])->name('rate-my-pay.comments.store');
        Route::put('/comments/{comment}', [RateMyPayController::class, 'updateComment'])->name('rate-my-pay.comments.update');
        Route::delete('/comments/{comment}', [RateMyPayController::class, 'destroyComment'])->name('rate-my-pay.comments.destroy');
        Route::post('/{rateMyPay}/like', [RateMyPayController::class, 'like'])->name('rate-my-pay.like');
        Route::post('/comments/{comment}/like', [RateMyPayController::class, 'likeComment'])->name('rate-my-pay.comments.like');
        Route::delete('/{rateMyPay}/like', [RateMyPayController::class, 'unlike'])->name('rate-my-pay.unlike');
        Route::delete('/comments/{comment}/like', [RateMyPayController::class, 'unlikeComment'])->name('rate-my-pay.comments.unlike');
        Route::get('/{rateMyPay}/chart-data', [RateMyPayController::class, 'getChartData'])->name('rate-my-pay.chart-data');
    });
    
    // API routes for dropdowns
    Route::get('/api/list', [CommunityController::class, 'apiIndex'])->name('communities.api.index');
    Route::get('/api/dropdown', [CommunityController::class, 'apiDropdown'])->name('communities.api.dropdown');
    Route::post('/api/join-multiple', [CommunityController::class, 'apiJoinMultiple'])->name('communities.api.join-multiple');
});

Route::middleware('auth')->prefix('network')->group(function () {
    Route::post('/connect/{user}', [CommunityController::class, 'connect'])->name('network.connect');
    Route::post('/accept/{user}', [CommunityController::class, 'accept'])->name('network.accept');
    Route::post('/ignore/{user}', [CommunityController::class, 'ignore'])->name('network.ignore');
});