<?php

namespace App\Http\Controllers\Admin;

use App\Models\Business;
use Illuminate\Http\Request;
use Inertia\Inertia;

class AdminSubscriptionController extends Controller
{
    public function index()
    {
        $businesses = Business::latest()->get();

        return Inertia::render('Admin/Subscriptions/Index', [
            'businesses' => $businesses,
        ]);
    }

    public function update(Request $request, Business $business)
    {
        $request->validate([
            'plan_type' => 'required|in:free,basic,premium',
        ]);

        $expiry = $request->plan_type === 'free' ? today()->addDays(7) : today()->addMonth();

        $business->update([
            'plan_type' => $request->plan_type,
            'plan_expiry_date' => $expiry,
            'plan_status' => 'active',
        ]);

        $business->subscriptions()->create([
            'plan_type' => $request->plan_type,
            'expiry_date' => $expiry,
            'status' => 'active',
        ]);

        return redirect()->back();
    }
}
