<?php

namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Recurring extends BaseModel
{
    use HasFactory;
    protected $fillable=['user_id','last_process_date','status','transaction_status','email','name','subscription_id','transaction_id','recurring_type','plan_payments','plan_amount','day_frequency','month_frequency','day_of_month'];

    public function updateByEmail($inputData){
        $insertData = [];
        if(!empty($inputData['user_id']))
        {
            $insertData['user_id'] = $inputData['user_id'];
        }
        return $this->where('email',$inputData['email'])->update($insertData);
    }
    public function  insert($inputData)
    {
        $returnData= self::create($inputData);

        appLog(['action'=>'RECURRING_INSERT','response'=>$returnData]);
        return $returnData;
    }
    public function  getTransactions($inputData)
    {
        $fromDate=$inputData['fdate'];
        $toDate=$inputData['tdate'];
        $clientName=$inputData['clientName'];
        $result=[];
        $recurring=self::query();
        if(!empty($clientName))
        {
            $recurring->where('name', 'like', '%' . $clientName . '%');
        }
        if(!empty($fromDate) && !empty($toDate))
        {
            $recurring->whereBetween('created_at',[$fromDate.' 00:00:00', $toDate.' 23:59:59']);
        }
        $result=$recurring->paginate(config('constant.PAGINATION_LIMIT'));

        return $result;
    }
    public function  deleteRecurring($inputData)
    {
        $recurringobj=self::where('user_id',$inputData['user_id'])->latest()->first();
        return $recurringobj->delete();
    }

    public function  getRecurringDataBySubscriptionId($inputData)
    {
        $recurringobj=self::where('user_id',$inputData['user_id'])->latest()->first();
        return $recurringobj->delete();
    }
    public function  deleteRecurringBySubscription_id($inputData)
    {
        $recurringobj = self::where('subscription_id',$inputData['subscription_id'])->first();

        return $recurringobj->delete();
    }

    public function  getRecurringByUserId($inputData)
    {
        $recurringobj = self::where(['user_id'=>$inputData['user_id'] , 'status'=> $inputData['status']] )->first();

        return $recurringobj;
    }

    public function getRecurringDataForScheduler()
    {
        $recurrings = [];
        $current_date = getCurrentDateTime();
        $day = 30;
        $data = 'DATEDIFF('."'$current_date'".',last_process_date)>='.$day;
//        $recurrings = self::where('subscription_id','8019590113')
//                            ->orWhere('subscription_id','8019544042')
//                            ->get();
//                $recurrings = self::where('subscription_id','8025917930')
//                            ->get();
//        dd($recurrings);

        $recurrings = self::where('status',1)
                            ->whereRaw($data)
//                            ->take(5)
                            ->get();

        return $recurrings;
    }

    public function getRecurrings()
    {
       return self::get();
    }


}
