<?php

namespace App\Models;
use App\Models\Support_Reply;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

class Support extends BaseModel
{
    use HasFactory;

    protected $fillable = ['sender_id','assigned_id','title','priority','is_read','status','created_at','updated_at'];

    public function supportreplays() {
        return $this->hasMany('App\Models\Support_Reply');
    }

    public function list($inputData){

        $queryData = Support::query();

        if($inputData['user_type'] == 0){
            $queryData->where('sender_id',$inputData['user_id']);
        }
        if(isset($inputData['status']) && is_numeric($inputData['status']))
        {
            $queryData->where('status',$inputData['status']);
        }
        if(!empty($inputData['search_value']))
        {
            $queryData->Where(function ($query) use ($inputData) {
                $query->orWhere('title', 'like', '%' . $inputData['search_value'] . '%');
            });

        }
//        dd($queryData->toSql());
        return $queryData->with('supportreplays')->paginate($inputData['pageLimit']);

//        if($inputData['user_type'] == 0){
//            return self::with('supportreplays')->where(['sender_id'=>$inputData['user_id']])->paginate($inputData['pageLimit']);
//        }else {
//            return self::with('supportreplays')->paginate($inputData['pageLimit']);
//        }
    }

    public function addSupport($inputData){

        $return_data = self::create($inputData);
        $inputData['support_id'] = $return_data['id'];
        return (new Support_Reply())->addSupportReply($inputData);
    }

    public function getSupport($inputData){
//        return self::where(['is_read'=>'0'])->count();
        $supports = [];
        if($inputData['user_type'] == 0) {
            $supports =  DB::table('supports')
                ->select('supports.id as support_id', 'supports.title as title', 'support_replies.message as message', 'support_replies.sender_id as sender_id', 'support_replies.is_read as readc')
                ->join('support_replies', 'supports.id', '=', 'support_replies.support_id')
                ->where('supports.sender_id', '=', $inputData['id'])
                ->where('support_replies.sender_id', '!=', $inputData['id'])
                ->where('support_replies.is_read', '=', 0)
                ->get();
        }else{
            $supports =   DB::table('supports')
                ->select('supports.id as support_id', 'supports.title as title', 'support_replies.message as message', 'support_replies.sender_id as sender_id', 'support_replies.is_read as readc')
                ->join('support_replies', 'supports.id', '=', 'support_replies.support_id')
                ->where('support_replies.sender_id', '!=', $inputData['id'])
                ->where('support_replies.is_read', '=', 0)
                ->get();
        }
        return $supports;
    }

    public function updateSupport($inputData)
    {
        return self::where(['status' => 0,'id'=>$inputData['id']])->update(['is_read' => 1,'status'=>2]);
    }
    public function getSupportById($inputData)
    {
//        $order = 'desc';
//        return self::with (['supportreplays' => function ($q) use ($order) {
//            $q->orderBy('id', $order);
//        }])->where(['id'=>$supportId])->first();

        return DB::table('supports')
            ->select('supports.*', 'support_replies.id as srid', 'support_replies.sender_id as srsender_id', 'support_replies.message','support_replies.attachment_path', 'support_replies.created_at as sr_create', 'users.first_name', 'users.last_name', 'users.user_type', 'users.email')
            ->join('support_replies','supports.id','=','support_replies.support_id')
            ->join('users','support_replies.sender_id','=','users.id')
            ->orderBy('support_replies.id','desc')
            ->where(['supports.id'=>$inputData['support_id']])
            ->get();

    }

    public function getNotifcation($user_id,$user_type)
    {

        $result = [];
        if($user_type == 0) {
//            $result = self::with(['supportreplays' => function ($q) use ($user_id) {
//                $q->orderBy('id','desc')
//                    ->where('sender_id', '!=', $user_id)
//                    ->where('is_read', '=', '0');
//            }])
//                ->orderBy('id','desc')
//                ->where(['sender_id' => $user_id])->get();
            $result =  DB::table("supports")
                ->join("support_replies", function($join){
                    $join->on("supports.id", "=", "support_replies.support_id");
                })
                ->select("supports.id", "supports.sender_id", "supports.title", "supports.created_at")
                ->orderBy('supports.id','desc')
                ->where("supports.sender_id", "=", $user_id)
                ->where("support_replies.sender_id", "!=", $user_id)
                ->where("support_replies.is_read", "=", 0)
                ->groupBy("supports.id")
                ->get();

            return $result;
        }
        if($user_type == 1){
            $result =  DB::table("supports")
                ->join("support_replies", function($join){
                    $join->on("supports.id", "=", "support_replies.support_id");
                })
                ->select("supports.id", "supports.sender_id", "supports.title", "supports.created_at")
                ->orderBy('supports.id','desc')
                ->where("support_replies.sender_id", "!=", $user_id)
                ->where("support_replies.is_read", "=", 0)
                ->groupBy("supports.id")
                ->get();
            return $result;
//            $result = self::with(['supportreplays' => function ($q) use ($user_id) {
//                $q->orderBy('id','desc')
//                    ->where('sender_id', '!=', $user_id)
//                    ->where('is_read', '=', '0');
//            }])->orderBy('id','desc')->get();
        }

        return $result;

    }
    public function getSupportId($supportId)
    {
        return DB::table('supports')
            ->select('supports.*', 'users.first_name', 'users.last_name', 'users.user_type', 'users.email')
            ->join('users','supports.sender_id','=','users.id')
            ->where(['supports.id' => $supportId ])
            ->first();

    }


}
