<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class VideoSetting extends BaseModel
{
    use HasFactory;
    protected $fillable=['link','section_name','status','is_welcome_video'];

    public function getVideoById($inputData)
    {
        return self::where('id',$inputData['section_id'])->first();
    }

    public function getVideos($inputData = [])
    {
        $videos = self::query();
        if(isset($inputData['is_welcome_video']))
        {
            $videos->where(['status'=> 1 , 'is_welcome_video'=> $inputData['is_welcome_video']]);
        }
        return $videos->get();
    }
    public function deleteVideos($inputData)
    {
        $video=$this->getVideoById($inputData);
        if(!empty($video))
        {
           return self::where('id', $video->id)->delete();
        }

    }
    public function insertVideo($inputData){
        $result = [];
        $videoObj =self::whereRaw( 'UPPER(`section_name`) LIKE ?', strtoupper($inputData['section_name']) )->first();
        if(empty($videoObj)) {
            $result = self::create($inputData);
        }
        return $result;
    }
    public function updateVideo($inputData){
        $result = [];
        $videoObj = $this->getVideoById($inputData);
        if(!empty($videoObj)){
            $videoObj->link = $inputData['link'];
            $videoObj->status = $inputData['status'];
            $videoObj->is_welcome_video = $inputData['is_welcome_video'];
            $videoObj->save();
            $result = $videoObj;
        }
        return $result;
    }
    public function getVideoBySectionId($sectionid)
    {
        $response=[];
        $video=self::where('section_id',$sectionid)->first();

        if(!empty($video)) {
            $response = ['id' => $video->id, 'link' => $video->link, 'section_id' => $video->section_id, 'status' => $video->status];
        }
        return $response;
    }

}
