<?php

namespace App\Http\Controllers;

use App\Helpers\FirstParty\Purchase\PurchaseHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class UserPurchaseController extends Controller
{
    public function billingPortal(Request $request)
    {
        return response()->json([
            'success' => [
                'data' => [
                    'redirect' => Auth::user()->billingPortalUrl(route('home')),
                ],
            ],
        ]);
    }

    public function pricingPage(Request $request)
    {

        $subscriptions = PurchaseHelper::getPricingPageSubscriptions();

        return response()->json([
            'success' => [
                'data' => [
                    'subscription' => $subscriptions[0],
                    'one_times' => PurchaseHelper::getPricingPageOneTime(),
                ],
            ],
        ]);
    }

    // SUBSCRIBE (RECURRING)

    public function subscribe(Request $request)
    {
        $price_id = $request->input('price_id');

        $payload = [
            'mode' => 'subscription',
            'success_url' => route('subscribe.success').'?'.'session_id={CHECKOUT_SESSION_ID}',
            'cancel_url' => route('subscribe.cancelled').'?'.'session_id={CHECKOUT_SESSION_ID}',
            'line_items' => [[
                'price' => $price_id,
                'quantity' => 1,
            ]],
        ];

        $checkout_session = Auth::user()->checkout([$price_id => 1], $payload);

        Session::put('checkout_session_id', $checkout_session->id);

        return response()->json([
            'success' => [
                'data' => [
                    'redirect' => $checkout_session->url,
                ],
            ],
        ]);
    }

    public function subscribeSuccess(Request $request)
    {
        if (! Session::has('checkout_session_id')) {
            abort(401);
        }

        Session::forget('checkout_session_id');

        return redirect()->route('home')->with('success', 'Purchase successful! Your purchase should be active momentarily. Please refresh if you did not see any changes.');
    }

    public function subscribeCancelled(Request $request)
    {

        if (Session::has('checkout_session_id')) {
            Session::forget('checkout_session_id');
        }

        return redirect()->route('home')->with('error', "You've decided not to complete the payment at this time. No charges have been made to your account.");
    }

    // PURCHASE (ONE TIME)

    public function purchase(Request $request)
    {
        $price_id = $request->input('price_id');

        $payload = [
            'success_url' => route('subscribe.success').'?'.'session_id={CHECKOUT_SESSION_ID}',
            'cancel_url' => route('subscribe.cancelled').'?'.'session_id={CHECKOUT_SESSION_ID}',
        ];

        $checkout_session = Auth::user()->checkout([$price_id => 1], $payload);

        Session::put('checkout_session_id', $checkout_session->id);

        return response()->json([
            'success' => [
                'data' => [
                    'redirect' => $checkout_session->url,
                ],
            ],
        ]);
    }

    public function purchaseSuccess(Request $request)
    {

        if (! Session::has('checkout_session_id')) {
            abort(401);
        }

        Session::forget('checkout_session_id');

        return redirect()->route('home')->with('success', 'Thank you for purchasing! Your purchase should be active momentarily. Please refresh the page if you do not see your plan.');
    }

    public function purchaseCancelled(Request $request)
    {
        if (Session::has('checkout_session_id')) {
            Session::forget('checkout_session_id');
        }

        return redirect()->route('home')->with('error', "You've decided not to complete the payment at this time. No charges have been made to your account.");
    }
}
