<?php

namespace App\Http\Controllers;

use App\Enum\OfferStatus;
use App\Enum\UserType;
use App\Models\Offer;
use App\Models\Transaction;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class TransactionController extends Controller
{
    //
    public function __construct()
    {
        $this->middleware(['auth', 'verified', 'onboarding']);
        $this->middleware(['role:artist,organizer'])->only('index');
        $this->middleware(['role:organizer'])->except('index');
    }

    /**
     * @return Application|Factory|View
     */
    public function index()
    {
        $payments = null;

        switch (Auth::user()->type) {
            case UserType::ORGANIZER->value:
                $organizerPayments = Transaction::select('transactions.*')
                    ->join('offers', 'offers.id', '=', 'transactions.offer_id')
                    ->join('events', 'events.id', '=', 'offers.event_id')
                    ->join('organizers', 'organizers.user_id', '=', 'events.organizer_id')
                    ->where('organizers.user_id', Auth::id())
                    ->orderBy('transactions.created_at', 'desc')
                    ->get();

                $payments = $organizerPayments;
                break;

            case UserType::ARTIST->value:
                $artistPayments = Transaction::select('transactions.*')
                    ->join('offers', 'offers.id', '=', 'transactions.offer_id')
                    ->where('offers.artist_id', Auth::id())
                    ->orderBy('transactions.created_at', 'desc')
                    ->get();

                $payments = $artistPayments;
                break;
        }

        return view('web.payments.payments')
            ->with('payments', $payments);
    }

    public function purchase($id, Request $request)
    {
        $user = Auth::user();
        $paymentMethod = $request->input('payment_method');
        $offer = Offer::where('id', $id)->firstOrFail();

        try {
            $user->createOrGetStripeCustomer();
            $user->updateDefaultPaymentMethod($paymentMethod);

            // Use the line below to charge the card
            //$user->charge($offer->price * 100, $paymentMethod);

            // Use the line below to make payment and issue invoice
            $invoice = $user->invoiceFor($offer->event->title .' - ' . $offer->artist->user->name, $offer->price * 100);
        } catch (\Exception $e) {
            return back()
                ->with('error', $e->getMessage());
        }

        $transaction = new Transaction();
        $transaction->name = 't-' . Str::random(25);
        $transaction->stripe_id = $user->asStripeCustomer()->id;
        $transaction->stripe_price = $offer->price;
        $transaction->invoice_id = $invoice->id;
        $transaction->offer()->associate($offer);

        try {
            $transaction->save();
        } catch (\Exception $e) {
            return back()
                ->with('error', $e->getMessage());
        }

        $offer->status = OfferStatus::COMPLETED;
        $offer->save();


        return back()
            ->with('success', 'Successfully paid! Hope you have a great event!');

    }
}
