<?php

use Illuminate\Support\Facades\Route;
use Modules\User\Http\Controllers\UserController;
use App\Http\Controllers\ImpersonateController;
use Modules\User\Http\Controllers\AddressController;
use Modules\User\Http\Controllers\EmploymentController;
use Modules\User\Http\Controllers\UserEmploymentController;
use Modules\User\Http\Controllers\BankController;
use Modules\User\Http\Controllers\BankDetailController;
use Modules\User\Http\Controllers\HeirController;
use Modules\User\Http\Controllers\MemberDigitalCardController;
use Modules\User\Http\Controllers\PublicMemberProfileController;

Route::prefix('v1/public')->group(function () {
    Route::middleware('throttle:public-member-profile')
        ->get('members/{token}', [PublicMemberProfileController::class, 'show'])
        ->name('user.public.member-profile');
});

Route::middleware(['auth:sanctum', 'single.session'])->prefix('v1')->group(function () {
    Route::get('users/deleted', [UserController::class, 'deletedIndex'])->name('users.deleted.index');
    Route::get('users/stats', [UserController::class, 'stats'])->name('users.stats');
    Route::post('users/{id}/restore', [UserController::class, 'restore'])->name('users.restore');
    Route::apiResource('users', UserController::class)->names('user');
    Route::apiResource('addresses', AddressController::class)->names('address');
    // employement
    Route::apiResource('employments', EmploymentController::class)->names('employment');
    // bank and bank detail
    Route::get('banks/active', [BankController::class, 'active'])->name('bank.active');
    Route::apiResource('banks', BankController::class)->names('bank');
    Route::apiResource('bank_details', BankDetailController::class)->names('bank_detail');
    // heir
    Route::apiResource('heirs', HeirController::class)->names('heir');
    // User role management routes
    Route::post('users/{user}/roles', [UserController::class, 'assignRoles'])->name('users.roles.assign');
    Route::post('users/{user}/employments', [UserEmploymentController::class, 'store'])->name('users.employments.store');
    Route::put('users/{user}/employments/{employment}', [UserEmploymentController::class, 'update'])->name('users.employments.update');
    Route::delete('users/{user}/employments/{employment}', [UserEmploymentController::class, 'destroy'])->name('users.employments.destroy');

    // User profile management routes
    Route::post('/profile', [UserController::class, 'updateProfile']);
    Route::post('/profile/onboarding/complete', [UserController::class, 'completeOnboarding']);
    Route::put('/profile/password', [UserController::class, 'updatePassword']);
    Route::get('/profile/digital-card', [MemberDigitalCardController::class, 'download'])
        ->name('profile.digital-card.download');
    
    // Impersonation routes
    Route::get('/impersonate/take/{id}', [ImpersonateController::class, 'take'])->name('impersonate');
    Route::get('/impersonate/leave', [ImpersonateController::class, 'leave'])->name('impersonate.leave');
    Route::get('/impersonate/status', [ImpersonateController::class, 'status'])->name('impersonate.status');
});
