<?php

namespace App\Http\Controllers;

use App\Services\MidtransService;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    protected MidtransService $midtrans;

    public function __construct(MidtransService $midtrans)
    {
        $this->midtrans = $midtrans;
    }

    /**
     * Get Midtrans token for payment
     */
    public function getToken(Request $request, $package)
    {
        // Validate package
        $validPackages = ['pro', 'enterprise'];
        if (!in_array($package, $validPackages)) {
            return response()->json(['success' => false, 'message' => 'Paket tidak valid'], 400);
        }

        try {
            $response = $this->midtrans->createSnapToken(auth()->user(), $package);
            return response()->json(['success' => true, 'data' => $response]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }
}
