<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShopPayment;
use Illuminate\Http\Request;
use Inertia\Inertia;

class AdminSubscriptionPaymentController extends Controller
{
    public function index()
    {
        $payments = ShopPayment::with('shopUser')
            ->whereNotNull('plan_type')
            ->latest('date')
            ->get();

        return Inertia::render('Admin/Pages/SubscriptionPayments/Index', [
            'payments' => $payments,
        ]);
    }

    public function approve(ShopPayment $payment)
    {
        if ($payment->plan_type) {
            $expiryDate = $payment->plan_type === 'basic'
                ? today()->addMonth()
                : today()->addMonth();

            $payment->shopUser->renewSubscription($payment->plan_type, $expiryDate);
        }

        $payment->update(['status' => 'completed']);

        return redirect()->back()->with('success', 'Payment approved and subscription activated.');
    }
}
