<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\Customer;
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use App\Models\Transaction;

class BillingController extends Controller
{
	protected $stripe;
	protected $user;


	public function __construct() {
		$this->stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
		Stripe::setApiKey(env('STRIPE_SECRET'));
		$this->user = Auth::user();
	}

	//Expects an array 'packs' representing the amount of each multiple of credits.
	//This controller should have a way of figuring out how much of each pack
	//was bought later.
	//Should validate that all amounts are positive integers in a reasonable range
	public function secret(Request $request) {
		Stripe::setApiKey(env('STRIPE_SECRET'));

		$user = Auth::user();
		$amount = $request->packs[ 'credits10' ]*1099 +
			$request->packs[ 'credits50' ]*5499 + $request->packs[ 'credits100' ]*10999
			+ $request->packs[ 'credits1000' ]*101000;

		$transaction = new Transaction;
		$transaction->credits = $request->packs['credits10']*10 +
			$request->packs['credits50']*50 +
			$request->packs['credits100']*100 +
			$request->packs['credits1000']*1000;
		$transaction->credits_extra =
			$request->packs['credits50']*5 +
			$request->packs['credits100']*10 +
			$request->packs['credits1000']*150;

		$transaction->user_id = $user->id;
		$transaction->charge = $amount;
		$transaction->status = 'processing';
		$transaction->completed = false;
		$total_credits = $transaction->credits + $transaction->credits_extra;

		$intent = PaymentIntent::create([
			'amount' => $amount,
			'currency' => 'usd',
			'customer' => $user->customer_id,
			'description' => "You have received $total_credits credits.",
			'receipt_email' => Auth::user()->email,
			'metadata' => ['transaction_id' => $transaction->id]
		]);
		$transaction->intent_id = $intent->id;

		//Save the card as a default if none is set and it was selected
		if ($user->payment_method == null && $request->card) {
			$this->changeDefaultCard($request->card);
		}

		$transaction->save();
		return $intent->client_secret;
	}

	public function getCards() {
		Stripe::setApiKey(env('STRIPE_SECRET'));

		return PaymentMethod::all([
			'customer' => Auth::user()->customer_id,
			'type' => 'card'
		]);
	}

	public function chargeEvent(Request $request) {
		Stripe::setApiKey(env('STRIPE_SECRET'));
		$event = \Stripe\Event::constructFrom($request->all());
		$charge = $event->data->object;
		$transaction = Transaction::where('intent_id', $charge->payment_intent);
		$user = $transaction->user;

		if ($event->type == 'charge.succeeded') {
			$user->credits = $user->credits + $transaction->credits + $transaction->credits_extra;
			$transaction->status = 'completed';
			$transaction->completed = true;
			$user->save();
			$transaction->save();

		} else {
			$transaction->status = $charge->status;
			$transaction->save();
		}
	}

	public function changeDefaultCard(String $card) {
		$user = Auth::user();
		$user->payment_method = $card;
		$user->save();
		$cards = $this->getCards();

		return PaymentMethod::all([
			'customer' => Auth::user()->customer_id,
			'type' => 'card'
		]);
	}

	public function deleteCard(Request $request) {
		$this->stripe->paymentMethods->detach($request->card);

		$user = Auth::user();
		if ($request->card == $user->payment_method) {
			$user->payment_method = null;
			$user->save();
		}
		return ($this->getCards());
	}
}
