<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class UserSession
 *
 * @package App\Models
 */
class UserSession extends Model
{
    /**
     * The table field that stores the primary key.
     *
     * @var string
     */
    protected $primaryKey = 'oid';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_session';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'user_oid' => 'integer'
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['ip_address', 'token'];

    /**
     * The user associated with this session
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne('App\Models\User', 'oid', 'user_oid');
    }

    public function getCreatedAtAttribute($value)
    {
        // or default TZ as set in .env.php
        $timezone = config('app.timezone');

        // use Illuminate\Database\Eloquent\Model;
        $datetime = $this->asDateTime($value);

        // Carbon instance modified with TZ
        return $datetime->timezone($timezone);
    }
}