<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Branch extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'email', 'phone', 'map',
    ];

    /*********************************************
     * Relations
     **********************************************/
    /**
     * @return HasOne
     */
    public function currentDescription(): HasOne
    {
        return $this->hasOne(BranchDescription::class)->where('language_id', currentLanguage()->id);
    }


public static function withDescription($branch_id = null)
    {
        $query = self::orderByDesc('branches.id')->join('branch_descriptions as cd', 'cd.branch_id', 'branches.id')
            ->where('cd.language_id', currentLanguage()->id)
            ->select(['branches.created_at', 'cd.*']);

        if ($branch_id)
        {
            if (is_array($branch_id))
            {
                $query->whereIn('branches.id', $branch_id);
            }
            else
            {
                $query->where('branches.id', $branch_id);
            }
        }

        $query->select([
            'branches.*',
            'cd.title',
            'cd.address',
        ]);
        return $query;
    }
}
