<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property mixed id
 */
class Account extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'iban','account_number'
    ];

    /**
     * Return descriptions for area
     * @return HasMany
     */
    public static function withDescription($account_id = null)
    {
        $query = self::orderByDesc('accounts.id')->join('account_descriptions as ad', 'ad.account_id', 'accounts.id')
            ->where('ad.language_id', currentLanguage()->id)
            ->select(['accounts.*', 'ad.*']);

        if ($account_id)
        {
            if (is_array($account_id))
            {
                $query->whereIn('accounts.id', $account_id);
            }
            else
            {
                $query->where('accounts.id', $account_id);
            }
        }
        $query->select([
            'accounts.*',
            'ad.name',
            'ad.bank',
        ]);
        return $query;
    }

    public function currentDescription()
    {
        return $this->hasOne(AccountDescription::class)->where('language_id',currentLanguage()['id']);
    }
}
