<?php

namespace App\Livewire\Subscription;

use App\Models\Feature;
use App\Models\Plan;
use Livewire\Component;

class Index extends Component
{
    public string $billing = 'monthly';

    public function selectPlan(string $planId): void
    {
        $this->redirect(route('checkout.index', [
            'planId'  => $planId,
            'billing' => $this->billing,
        ]));
    }

    public function render()
    {
        $user         = auth()->user();
        $subscription = $user->subscription()->with('plan')->first()
            ?? $user->latestSubscription()->with('plan')->first();

        $plans = Plan::public()->with(['features.group'])
            ->where('active', true)
            ->orderBy('sort')
            ->get();

        $allFeatures = Feature::with('group')
            ->where('active', true)
            ->orderBy('sort')
            ->get();

        return view('livewire.subscription.index', [
            'subscription' => $subscription,
            'plans'        => $plans,
            'allFeatures'  => $allFeatures,
        ])->layout('layouts.app');
    }
}
