<?php namespace App\Http\Controllers;

use App\User;
use App\Repositories\ApiRepository;
use Input;
use Auth;
use DB;

class ApiController extends Controller
{
    public $apiRepository;

    public function __construct(ApiRepository $apiRepository)
    {
        $this->apiRepository = $apiRepository;
    }

    public function trafficExchange()
    {
        return view('traffic-exchange');
    }

    public function session()
    {

        $response = $this->apiRepository->session();

        foreach ($response as $value) {
            return $value;
        }

    }

    public function increment()
    {
        $client_id = Input::get('client_id');
        $this->apiRepository->increment($client_id);

    }

    public function stripe(User $user)
    {
        $token = Input::get('stripeToken');
        $memberships = DB::table('memberships')->first();
        $amount = ($memberships->amount) * 100;
        $user->charge($amount, [
            'source' => $token,
        ]);
        User::whereId(Auth::user()->id)->update(['membership' => "paid"]);
    }


    public function purchasePoints(User $user)
    {
        $token = Input::get('stripeToken');
        $amount = Input::get('amount');
        $points = Input::get('points');

        $user->charge($amount, [
            'source' => $token,
        ]);
        User::whereId(Auth::user()->id)->increment('points', $points);
    }


}
