<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;

class PhotoGroup extends Model
{
    use ModelTrait;

    protected $table = 'photo_groups';

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

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

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

        if (config('visit') == 'frontend' && config('lang') != config('setting.lang')) {
            return $this->hasOne(PhotoGroupDescription::class, 'photo_group_id')->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(PhotoGroupDescription::class, 'photo_group_id')->whereIn('lang', [config('managerLang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('managerLang') ."', '". config('setting.lang') ."')"));
        }

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