<?php

namespace Mehrab\Payment\Http\Controllers;

use App\Http\Controllers\Controller;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Mehrab\Payment\Events\PaymentOnlineWasSuccessfulEvent;
use Mehrab\Payment\excel\ViewExcel;
use Mehrab\Payment\Gateways\Gateway;
use Mehrab\Payment\Models\Payment;
use Mehrab\Payment\Repositories\PaymentRepo;


class PaymentController extends Controller
{
    private $paymentRepo;

    public function __construct(PaymentRepo $paymentRepo)
    {
        $this->paymentRepo = $paymentRepo;
    }

    public function index(Request $request)
    {
        $this->authorize('index',Payment::class);
        $payments = $this->paymentRepo->get_payments($request,['paymentable', 'customer']);
        return view('Payment::Admin.list_payment',compact('payments'));
    }

    public function info()
    {
        $paymentRepo = $this->paymentRepo;
        $this->authorize('manage',Payment::class);
        $last_N_DaysTotal = $paymentRepo->lastDaysTotal(Payment::STATUS_SUCCESS,-30);
        $last_N_DaysTotalPure = $paymentRepo->lastDaysTotalPure(Payment::STATUS_SUCCESS,-30);
        $last_N_DaysTotalPureSeller = $paymentRepo->lastDaysTotalPureSeller(Payment::STATUS_SUCCESS,-30);
        $lastDaysTotal = $paymentRepo->lastDaysTotal(Payment::STATUS_SUCCESS);
        $lastDaysTotalPure = $paymentRepo->lastDaysTotalPure(Payment::STATUS_SUCCESS);
        $last30Days = CarbonPeriod::create(now()->addDays(-30),now());

        $dates = collect();
        foreach (range(-30,0) as $i){
            $dates->put(now()->addDays($i)->format('Y-m-d'),0);
        }
        $summery = $this->paymentRepo->getDailySummery($dates);
        return view('Payment::Admin.info_payment',compact('last_N_DaysTotal','last_N_DaysTotalPure','lastDaysTotal','lastDaysTotalPure','last30Days','paymentRepo','summery','dates','last_N_DaysTotalPureSeller'));
    }

    public function callback(Request $request){

        $gateway = resolve(Gateway::class);
        $payment = $this->paymentRepo->findByInvoiceId($gateway->getInvoiceIdFromRequest($request));

        if (!$payment){
            return redirect('home')->with('status','تراکنش مورد نظر یافت نشد !');
        }

        $result = $gateway->verify($payment);
        if ($result == false){
            $this->paymentRepo->changeStatusPayment($payment,Payment::STATUS_FAIL);
            return redirect()->route('panel.payments')->with('status','پرداخت ناموفق !');
        }else{
            event(new PaymentOnlineWasSuccessfulEvent($payment));

            $this->paymentRepo->changeStatusPayment($payment,Payment::STATUS_SUCCESS,$result);
            $routeNameParam = route('panel.rate',$payment->paymentable->id);
            return redirect()->route('panel.orders',['route'=>$routeNameParam])->with('okPaymentFeedback','ok')->send();
        }

    }

    public function excel(Request $request){
        $this->authorize('index',Payment::class);
        return Excel::download(new ViewExcel, 'transactions.xlsx');

    }



}
