<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\API\AuthController;
use App\Http\Controllers\API\UserController;
use App\Http\Controllers\API\BranchController;
use App\Http\Controllers\API\UserBranchController;
use App\Http\Controllers\API\CreditController;
use App\Http\Controllers\API\WithdrawalController;
use App\Http\Controllers\API\DashboardController;

/*
|--------------------------------------------------------------------------
| 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::get('/', function () {
    return response()->json([
        'status'  => TRUE,
        'message' => 'Connected successfully!',
        'data'    => [],
    ]);
});
// Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
//     return $request->user();
// });

// Auth Routes
Route::post('/login', [AuthController::class, 'login']);
Route::get('/user', [AuthController::class, 'user'])->middleware('auth:sanctum');
Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum');

// Route::get('/users', [UserController::class, 'index']);
// Route::get('/users/{user}', [UserController::class, 'show']);
// Route::put('/users/{user}', [UserController::class, 'update']);
// Route::patch('/users/{user}/toggle-active', [UserController::class, 'toggleActive']);

// User management routes - require authentication
Route::middleware('auth:sanctum')->group(function () {
    Route::post('/register', [AuthController::class, 'register']);

    Route::get('/users', [UserController::class, 'index']);
    Route::get('/users/{user}', [UserController::class, 'show']);
    Route::put('/users/{user}', [UserController::class, 'update']);
    Route::patch('/users/{user}/toggle-active', [UserController::class, 'toggleActive']);
    Route::get('/users/{user}/roles', [UserController::class, 'roles']);
    Route::post('/change-password', [UserController::class, 'changePassword']);

    // User branch relationship routes
    Route::get('/user/branches', [UserBranchController::class, 'getCurrentUserBranches']);
    Route::get('/users/{id}/branches', [UserBranchController::class, 'getUserBranches']);

    // Branch management routes
    Route::get('/branches', [BranchController::class, 'index']);
    Route::post('/branches', [BranchController::class, 'store']);
    Route::get('/branches/{id}', [BranchController::class, 'show']);
    Route::put('/branches/{id}', [BranchController::class, 'update']);
    Route::delete('/branches/{id}', [BranchController::class, 'destroy']);

    // Branch user management routes
    Route::get('/branches/{id}/users', [BranchController::class, 'users']);
    Route::post('/branches/{id}/users', [BranchController::class, 'addUser']);
    Route::delete('/branches/{id}/users/{userId}', [BranchController::class, 'removeUser']);
    Route::put('/branches/{id}/users/{userId}/role', [BranchController::class, 'updateUserRole']);

    // Credit management routes
    Route::get('/credits', [CreditController::class, 'index']);
    Route::post('/credits', [CreditController::class, 'store']);
    Route::get('/credits/{id}', [CreditController::class, 'show']);
    Route::post('/credits/{id}', [CreditController::class, 'update']);
    Route::delete('/credits/{id}', [CreditController::class, 'destroy']);
    Route::patch('/credits/{id}/status', [CreditController::class, 'updateStatus']);
    Route::patch('/credits/{id}/toggle-active', [CreditController::class, 'toggleActive']);

    // 专门用于批准和拒绝信用请求的路由
    Route::post('/credits/{id}/approve', [CreditController::class, 'approveRequest']);
    Route::post('/credits/{id}/reject', [CreditController::class, 'rejectRequest']);

    // Dashboard routes
    Route::get('/dashboard/stats', [DashboardController::class, 'getStats']);

    // Withdrawal management routes
    Route::get('/withdrawals', [WithdrawalController::class, 'index']);
    Route::post('/withdrawals', [WithdrawalController::class, 'store']);
    Route::get('/withdrawals/{id}', [WithdrawalController::class, 'show']);
    Route::put('/withdrawals/{id}', [WithdrawalController::class, 'update']);
    Route::delete('/withdrawals/{id}', [WithdrawalController::class, 'destroy']);

    // 取款审批相关路由
    Route::post('/withdrawals/{id}/approve', [WithdrawalController::class, 'approveAsShareHolder']);
    Route::post('/withdrawals/{id}/reject', [WithdrawalController::class, 'rejectAsShareHolder']);
    Route::get('/withdrawals/{id}/approval-status', [WithdrawalController::class, 'getApprovalStatus']);

    // New credit balance routes
    Route::get('/credit-balances', [CreditController::class, 'getBalance']);
    Route::post('/credit-balances/verify', [CreditController::class, 'verifyBalances']);
    Route::get('/credit-balances/check', [CreditController::class, 'checkBalance']);
});
