<?php
namespace App\Services;

use App\Models\UserSubscription;
use App\Models\Cart;
use Carbon\Carbon;

class SubscriptionService
{
    public function activate($order)
    {
        $cart = Cart::where('order_id',$order->id)
            ->where('item_type','subscription')
            ->first();

        if(!$cart) return;

        $package = $cart->subscription;

        $start = Carbon::today();
        $end   = Carbon::today()->addDays($package->duration_days);

        UserSubscription::create([
            'user_id' => $order->user_id,
            'subscription_package_id' => $package->id,
            'order_id' => $order->id,
            'start_date' => $start,
            'end_date' => $end,
            'status' => 'active'
        ]);
    }
}