<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Package;
use App\Models\Payment;
use App\Models\Subscription;
use App\Models\Type;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class PaymentController extends Controller
{

    public function validatePayment($paymentID,$paymentToken,$payerID,$package,$type){
        $paymentID = request()->paymentID;
        $payment = new \App\Models\Payment();
        $result = $payment->validate($paymentID);
        
        if($result->id == $paymentID && $result->state=='approved'){
            $company_id = Auth::user()->company->id;
            $company = Company::FindOrFail($company_id);
            $package = Package::where('id','!=',1)->findOrfail($package);
            $type = Type::where('package_id',$package->id)->findOrfail($type);
            $superscription = Subscription::where('company_id',$company_id)->first() ;
            $payment = Payment::where('company_id',$company_id)->where('payment_id',$paymentID)->first();
            //return response()->json($result);
            
            if(empty($payment)){

            if(empty($superscription)){
                Subscription::create([
                    'company_id' => $company_id,
                    'package_id' => $package->id,
                    'type_id' => $type->id,
                    'status' => 'active',
                    'start_date' => date('Y-m-d'),
                    'end_date' => date('Y-m-d', strtotime('+'.$type->period.' days')),
                ]);
                $company->update([
                    'status' => 'active'
                ]);
            }else{
                if($superscription->status=='active'){
                    $to = Carbon::parse(date('Y-m-d'));
                    $from = Carbon::parse($superscription->end_date);
                    $period = $to->diffInDays($from);
                }else{
                    $period = 0;
                }
                
                $superscription->update([
                    'package_id' => $package->id,
                    'type_id' => $type->id,
                    'start_date' => date('Y-m-d'),
                    'status' => 'active',
                    'end_date' => date('Y-m-d', strtotime('+'.$type->period+$period.' days ')),
                ]);

                $company->update([
                    'status' => 'active'
                ]);

            }
                
            Payment::create([
                'company_id' => $company_id,
                'payment_id'=> $paymentID,
                'amount' => $result->transactions[0]->amount->total,
                'date' => date('Y-m-d'),
                'time' => date('H:i:s'),
            ]);

            return redirect(route('client.home'))->with('success','تم قبول عملية الدفع , و تم تفعيل الاشتراك بنجاح');

            }else{
                abort(404);
            }

        }
     }
    

}
