<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DonateurController;
use App\Http\Controllers\GroupController;
use App\Http\Controllers\CampaignController;
use \App\Http\Controllers\WebhookController;
use App\Http\Controllers\DonationController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\LinkController;

// ...

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

// Pages protégées
Route::middleware(['auth','is_admin'])->group(function(){
    Route::get('/', [DonateurController::class,'index'])->name('welcome');
});

Route::get('login',   [AuthController::class,'showLoginForm'])->name('login');
Route::post('login',  [AuthController::class,'login']);
Route::get('forgot-password', [AuthController::class,'showForgotPasswordForm'])->name('password.request');
Route::post('forgot-password', [AuthController::class,'sendResetLinkEmail'])->name('password.email');
Route::get('reset-password/{token}', [AuthController::class,'showResetForm'])->name('password.reset');
Route::post('reset-password', [AuthController::class,'resetPassword'])->name('password.update');

// Déconnexion
Route::post('logout',[AuthController::class,'logout'])->name('logout');

Route::middleware('auth')->group(function(){
    Route::get('account', [\App\Http\Controllers\UserController::class,'edit'])->name('account.edit');
    Route::post('account', [\App\Http\Controllers\UserController::class,'update'])->name('account.update');
    Route::get('account/password', [\App\Http\Controllers\UserController::class,'editPassword'])->name('account.password.edit');
    Route::post('account/password', [\App\Http\Controllers\UserController::class,'updatePassword'])->name('account.password.update');

    Route::get('users/invite', [\App\Http\Controllers\UserController::class,'create'])->name('users.create');
    Route::post('users/invite', [\App\Http\Controllers\UserController::class,'store'])->name('users.store');
});

Route::prefix('admin')->middleware(['auth','is_admin'])->group(function(){
    Route::get('/dashboard', [DonateurController::class,'index'])->name('admin.dashboard');
    Route::get('donateurs', [DonateurController::class,'index'])->name('donateurs.index');
    Route::resource('donateurs', DonateurController::class);
    Route::post('donateurs/import', [DonateurController::class,'import'])->name('donateurs.import');
    Route::resource('groups', GroupController::class);
    Route::get('groups/{group}/edit-members', [GroupController::class,'editMembers'])->name('groups.edit_members');
    Route::put('groups/{group}/update-members', [GroupController::class,'updateMembers'])->name('groups.update_members');
    Route::post('groups/{group}/import-members', [GroupController::class,'importMembers'])->name('groups.import_members');
    Route::get('groups/{group}/export-members', [GroupController::class, 'exportMembers'])->name('groups.export_members');

    Route::resource('campaigns', CampaignController::class);
    // Formulaire d'envoi
    Route::get('campaigns/{campaign}/send', [CampaignController::class,'sendForm'])->name('campaigns.send_form');
    Route::post('campaigns/{campaign}/send', [CampaignController::class,'send'])->name('campaigns.send');

    Route::get('reports', [\App\Http\Controllers\ReportController::class,'index'])->name('reports.index');
    Route::get('reports/export', [\App\Http\Controllers\ReportController::class,'export'])->name('reports.export');
    Route::get('donateurs/{donateur}/details', [DonateurController::class,'details'])->name('donateurs.details');
    Route::get('donateurs/{donateur}/details-ajax', [DonateurController::class,'detailsAjax'])->name('donateurs.details_ajax');
    Route::get('campaigns/{campaign}/details', [CampaignController::class,'details'])->name('campaigns.details');
    Route::get('campaigns/{campaign}/details-ajax', [CampaignController::class,'detailsAjax'])->name('campaigns.details_ajax');
    Route::get('links/{link}/details', [LinkController::class, 'details'])->name('links.details');
    Route::post('links/{link}/manual-transaction', [DonationController::class,'manualTransaction'])->name('links.manual_transaction');

    // routes/web.php
    Route::post('campaigns/{campaign}/remind', [CampaignController::class,'remind'])
        ->name('campaigns.remind');
});

Route::get('/checkout/callback', [\App\Http\Controllers\DonationController::class,'checkoutCallback'])->name('donation.callback');
Route::post('/webhook/fedapay', [WebhookController::class, 'handle']);
// Public donation pages
Route::get('don/{campaign}/{token}', [DonationController::class,'showForm'])->name('don.show');
Route::post('don/{campaign}/{token}/pay', [DonationController::class,'createTransaction'])->name('don.pay');
Route::get('don/{campaign}/{token}/return', [DonationController::class,'checkoutReturn'])->name('don.return');
Route::get('/campaigns/{campaign}/links/{token}/optout', [LinkController::class, 'optOut'])->name('links.optout');




