<?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;

class Notification extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'status','user_id','target_id','read_at'
    ];

    public function getdateAttribute()
    {
        $date = date('Y ',strtotime($this->created_at));
        $date .= __('web.'.date('F',strtotime($this->created_at)));
        $date .= date(' d ',strtotime($this->created_at));
        return $date;
    }
    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(NotificationDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(NotificationDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(NotificationDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }


    public static function withDescription($notification_id = null)
    {
        $query = self::orderByDesc('notifications.id')->join('notification_descriptions as pd', 'pd.notification_id', 'notifications.id')
            ->where('pd.language_id', currentLanguage()->id)
            ->select(['notifications.created_at', 'pd.*']);
        if ($notification_id){
            $query->where('notifications.id', $notification_id);
        }

        $query->select([
            'notifications.*',
            'pd.title',
            'pd.description',
        ]);
        return $query;
    }

    public function getShowTimeAttribute()
    {
        $date = date('g:i',strtotime($this->created_at));
        return $date;
    }

    public function getShowTimeTypeAttribute()
    {
        $date = __('web.'.date('a',strtotime($this->created_at)));
        return $date;
    }

    public function getShowDateAttribute()
    {
        $date = date('Y',strtotime($this->created_at)).' ';
        $month = date('F',strtotime($this->created_at));
        $date.= __('web.'.$month) .' ';
        $date .= date('d',strtotime($this->created_at));
        return $date;
    }

}
