<?php

namespace App\Http\Controllers\Api\Subscription;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\SubscriptionPlan;

class SubscriptionPlanController extends Controller
{
    // 🔹 list plan
    public function index()
    {
        return SubscriptionPlan::where('is_active',1)->get();
    }

    // 🔹 create (admin only nanti)
    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required',
            'slug' => 'required|unique:subscription_plans',
            'user_type' => 'required',
            'price' => 'required',
            'billing_period' => 'required'
        ]);

        return SubscriptionPlan::create($data);
    }

    public function show($id)
    {
        return SubscriptionPlan::findOrFail($id);
    }

    public function update(Request $request, $id)
    {
        $plan = SubscriptionPlan::findOrFail($id);

        $plan->update($request->all());

        return $plan;
    }

    public function destroy($id)
    {
        SubscriptionPlan::destroy($id);

        return response()->json(['message'=>'deleted']);
    }
}