<?php

namespace App\Http\Controllers\Admin;

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

class AdminSubscriptionController extends Controller
{
    public function history(Request $request)
    {
        $subscriptions = Subscription::with(['user', 'plan'])
            ->latest()
            ->paginate(15)
            ->withQueryString();

        return view('admin.subscriptions.history', compact('subscriptions'));
    }

    public function active(Request $request)
    {
        $subscriptions = Subscription::with(['user', 'plan'])
            ->where('status', 'active')
            ->latest()
            ->paginate(15)
            ->withQueryString();

        return view('admin.subscriptions.active', compact('subscriptions'));
    }

    public function expired(Request $request)
    {
        $subscriptions = Subscription::with(['user', 'plan'])
            ->where('status', 'expired')
            ->latest()
            ->paginate(15)
            ->withQueryString();

        return view('admin.subscriptions.expired', compact('subscriptions'));
    }

    public function forceExpire(Subscription $subscription)
    {
        if ($subscription->status !== 'active') {
            return redirect()
                ->back()
                ->with('error', 'Only active subscriptions can be force expired.');
        }

        $subscription->update([
            'status'   => 'expired',
            'end_date' => now(),
        ]);

        return redirect()
            ->back()
            ->with('success', 'Subscription force expired successfully.');
    }
}