<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use App\Traits\TaggableTrait as Taggable;
use Laracasts\Presenter\PresentableTrait;

class Video extends Model
{
    use Taggable, PresentableTrait, ModelTrait;

    protected $presenter = 'App\Presenters\ContentPresenter';

    protected $table = 'videos';

    protected $guarded = ['tags', 'referer', 'description'];

    public function cates()
    {
        return $this->belongsTo(VideoCate::class, 'pid');
    }

    public function description()
    {
        $lang = config('lang') ?: config('setting.lang');

        if (config('visit') == 'frontend' && config('lang') != config('setting.lang')) {
            return $this->hasOne(VideoDescription::class)->whereIn('lang', [config('lang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('lang') ."', '". config('setting.lang') ."')"));
        } elseif (config('visit') == 'admin' && config('managerLang') != config('setting.lang')) {
            return $this->hasOne(VideoDescription::class)->whereIn('lang', [config('managerLang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('managerLang') ."', '". config('setting.lang') ."')"));
        }

        return $this->hasOne(VideoDescription::class)->where('lang', $lang);
    }

}
