<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\UserSubscription;
use App\Repositories\PackageRepository;
use App\Repositories\StripeRepositoryEloquent;
use App\Repositories\UserSubscriptionRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SubscriptionController extends Controller
{
    private $userSubscriptionRepo;

    private $stripeRepo;
    private $packageRepo;
    public function __construct(
        // UserSubscriptionRepository $userSubscriptionRepository,
        // PackageRepository $packageRepository,
        StripeRepositoryEloquent $stripeRepositoryEloquent
    ) {
        // $this->userSubscriptionRepo = $userSubscriptionRepository;
        // $this->packageRepo = $packageRepository;
        $this->stripeRepo = $stripeRepositoryEloquent;
    }

    public function packageSubscribe(Request $request)
    {
        $request->validate([
            'package_id' => 'required',
        ]);
        try {
            $data = $request->all();
            session()->put('formData', $request->all());
            $findPackage = $this->packageRepo->getById($request->package_id);
            return view('front.payment', compact('findPackage', 'data'));

        } catch (\Exception $e) {
            dd($e->getMessage());
            return redirect()->back()->with('error', $e->getMessage());
        }

    }

    public function stripeCheckout(Request $request)
    {
        try {
            $stripeToken = $this->stripeRepo
                ->checkFrontStripeToken($request->input('payment_method_id'));
            DB::beginTransaction();
            $sessionData = session()->get('formData');
            $findPackage = $this->packageRepo->getById($sessionData['package_id']);
            $total = $findPackage->price;

            $content = [
                'model' => $sessionData['model'],
                'vin' => $sessionData['vin'],
                'color' => $sessionData['color'],
                'owner_name' => $sessionData['name'],
                'address' => $sessionData['address'],
                'license' => $sessionData['license'],
                'location' => $sessionData['location'],
                'coverage' => $sessionData['coverage'],
                'total' => $total,
            ];
            $charge = $this->stripeRepo->frontCharge($stripeToken, $total);
            if ($charge) {
                return redirect()->route('front.success')->with('success', 'Subscription successfully processed.');
            }
        } catch (\Exception $e) {


            dd($e->getMessage());
            return redirect()->route('success')->with('error', 'Payment failed. Please try again.');
        }
    }
    public function success(Request $request)
    {
        $orderId = $request->get('order_id'); // Optional parameter
    
        if ($orderId) {
            $order = Order::with('orderItems')->find($orderId);
            return view('front.success', compact('order'));
        }
        return view('front.success');
    }
}
