<?php

declare(strict_types=1);

namespace App\Http\Controllers\Academy;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

final class AcademyCheckoutController extends Controller
{
    public function store(Request $request, string $plan): JsonResponse|RedirectResponse
    {
        abort_unless((bool) config('academy.enabled', true), 404);

        // TODO: Replace this placeholder with Laravel Cashier + Stripe Checkout when academy payments are enabled.
        if (! (bool) config('academy.payments_enabled', false)) {
            $payload = [
                'ok' => false,
                'code' => 'academy_payments_disabled',
                'message' => 'Academy payments are disabled for this launch phase.',
                'plan' => $plan,
            ];

            if ($request->expectsJson()) {
                return response()->json($payload, 423);
            }

            return redirect()->route('academy.pricing')->with('error', $payload['message']);
        }

        return response()->json([
            'ok' => false,
            'code' => 'academy_checkout_not_implemented',
            'message' => 'Checkout is not implemented yet.',
            'plan' => $plan,
        ], 501);
    }
}