<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Services\SubscriptionService;
use Laracasts\Flash\Flash;

class SubscriptionsController extends Controller
{
    protected $data = [];
    protected $subscriptionService;

    public function __construct(SubscriptionService $subscriptionService)
    {
        $this->subscriptionService = $subscriptionService;
    }

    public function index()
    {
        $this->data['title'] = trans('subscription.title|Subscriptions');
        $this->data['subscriptions'] = auth()->user()->subscriptions()->orderBy('id', 'DESC')->paginate(15);
        $this->data['pageHeader'] = [
            'title' => __('subscription.title|Subscriptions'),
            'subtitle' => __('subscription.Manage your subscriptions'),
            'breadcrumbs' => [
                ['label' => __('general.Home'), 'url' => url('/')],
                ['label' => __('subscription.Subscriptions')],
            ]
        ];
        return view('theme::pages.account.subscriptions.index', $this->data);
    }

    public function show($subscription_id)
    {
        $subscription = auth()->user()->subscriptions()->with(['orders'])->findOrFail($subscription_id);
        $lastOrder = $subscription->lastActiveOrder() ?? $subscription->lastOrder();
        $subscriptionStatus = $subscription->status;
        $productFiles = $this->subscriptionService->getProductFilesFromSubscription($subscription);

        $this->data['fields'] = $this->subscriptionService->getSubscriptionFields($subscription, $lastOrder, $subscriptionStatus);

        $this->data['subscription'] = $subscription;
        $this->data['lastOrder'] = $lastOrder;
        $this->data['productFiles'] = $productFiles;
        $this->data['title'] = trans('subscription.title|Subscription Details');
        $this->data['pageHeader'] = [
            'title' => __('subscription.Subscription Details'),
            'subtitle' => __('subscription.View your subscription details'),
            'breadcrumbs' => [
                ['label' => __('general.Home'), 'url' => url('/')],
                ['label' => __('subscription.Subscriptions'), 'url' => route('frontend.subscriptions.index')],
                ['label' => __('subscription.Subscription Details')],
            ]
        ];

        return view('theme::pages.account.subscriptions.show', $this->data);
    }

    public function showSubscriptionOrder($subscription_id, $order_id)
    {
        $order = $this->subscriptionService->getOrderByEncryptIdAndSub($subscription_id, $order_id, auth()->id());

        if (!$order) {
            Flash::error(trans('subscription.Subscription Order not found!'));
            return redirect()->back();
        }

        $this->data['order'] = $order;
        $this->data['title'] = trans('subscription.title|Subscription Order :order_reference', ['order_reference' => $order->order_reference]);
        $this->data['pageHeader'] = [
            'title' => __('subscription.Subscription Order Details'),
            'subtitle' => __('subscription.View your subscription order details'),
            'breadcrumbs' => [
                ['label' => __('general.Home'), 'url' => url('/')],
                ['label' => __('subscription.Subscriptions'), 'url' => route('frontend.subscriptions.index')],
                ['label' => '#' . $order->subscription->reference_id, 'url' => route('frontend.subscriptions.show', $subscription_id)],
                ['label' => __('subscription.Subscription Order Details')],
            ]
        ];

        return view('theme::pages.account.subscriptions.show_order', $this->data);
    }

    public function cancel($subscription_id)
    {
        $subscription = $this->subscriptionService->find($subscription_id);

        if ($subscription->status === getConstant('SUBSCRIPTION_STATUS_ACTIVE')) {
            $this->subscriptionService->cancelSubscription($subscription);
        } else {
            $this->subscriptionService->cancelTrialSubscription($subscription);
        }

        Flash::success(trans('subscription.Your Subscription has been cancelled.'));

        return redirect()->back();
    }

    public function reactive($subscription_id)
    {
        $subscription = $this->subscriptionService->find($subscription_id);

        if (!$subscription->cancelled_at && $subscription->next_billing_date >= now() && $subscription->status === getConstant('SUBSCRIPTION_STATUS_ACTIVE')) {
            Flash::error(trans('subscription.You cannot reactive Subscription'));

            return redirect()->back();
        }

        $this->subscriptionService->reactive($subscription);

        Flash::success(trans('subscription.Your Subscription has been reactive again.'));

        return redirect()->back();
    }
}
