<?php

use App\Http\Controllers\UmkmController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\PaymentController;
use App\Http\Controllers\ChatController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

Route::prefix('v1')->group(function () {
    
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/register', [AuthController::class, 'register']);

    Route::post('/payment/checkout', [PaymentController::class, 'checkout']);
    Route::post('/payment/webhook', [PaymentController::class, 'webhook']);

    Route::middleware('auth:sanctum')->group(function () {
        
        Route::get('/profile', function (Request $request) {
            $user = $request->user()->load(['umkm', 'subscriptions']);
            $activeSubscription = $user->subscriptions()->latest()->first();
            $user->active_subscription = $activeSubscription;
            return $user;
        });

        Route::post('/user/profile-photo', [UmkmController::class, 'updatePhoto']);

        Route::get('/user', function (Request $request) {
            return $request->user()->load('umkm');
        });

        Route::post('/umkm', [UmkmController::class, 'storeApi']);
        Route::get('/umkm', [UmkmController::class, 'indexApi']);
        Route::get('/transactions', [UmkmController::class, 'getTransactions']);
        Route::post('/transactions', [UmkmController::class, 'storeTransaction']);
        Route::delete('/transactions/{id}', [UmkmController::class, 'deleteTransaction']);
        Route::get('/budget-plan', [UmkmController::class, 'getBudgetPlan']);
        Route::post('/budget-plan', [UmkmController::class, 'updateBudgetPlan']);
        Route::post('/contact', [UmkmController::class, 'sendContact']);

        Route::get('/chat/messages', [ChatController::class, 'index']);
        Route::post('/chat/messages', [ChatController::class, 'send']);
    });
});
