<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Mail;
use App\Mail\MailResetPassword;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Passport\HasApiTokens;


/**
 * App\User
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string|null $remember_token
 * @property string|null $token
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int|null $confirmed
 * @property string|null $confirmation_code
 * @property string|null $confirmation_date
 * @property string|null $confirmation_code_to
 * @property int|null $confirmation_code_remider
 * @property int|null $active
 * @property string|null $active_date
 * @property string|null $agreement
 * @property int|null $admin
 * @property string|null $identify
 * @property string|null $lang
 * @property string|null $last_login
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\Account $account
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserUpdateEmail[] $user_update_email
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\User onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereActiveDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAdmin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAgreement($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereConfirmationCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereConfirmationCodeRemider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereConfirmationCodeTo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereConfirmationDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereConfirmed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereIdentify($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereLang($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereLastLogin($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\User withoutTrashed()
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Client[] $clients
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Token[] $tokens
 */
class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $table = 'users';


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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 'token',
    ];


   public function account()
    {
        return $this->hasOne('App\Models\Account');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function user_update_email()
    {
        return $this->hasMany('App\Models\UserUpdateEmail', 'user_id', 'id');
    }

    /**
     * @return bool
     */
    public function isPasswort(){
        if($this->password == env('APP_KEY')){
            return false;
        }
        return true;
    }

    /**
     * @return bool
     */
    public function isAdmin()
    {
        if($this->admin >= 1){
            return true;
        }
        return false;
    }

    /**
     * @return bool
     */
    public function isSuperAdmin()
    {
        if($this->admin >= 2){
            return true;
        }
        return false;
    }

    /**
     * @return string
     */
    public function getConfirmationDateFormat(){
        if(!$this->attributes['confirmation_date']){ return ""; }
        return Carbon::parse($this->attributes['confirmation_date'])->format(\Util::formatDateTimeDB());
    }

    /**
     * @return string
     */
    public function getActiveDateFormat(){
        if(!$this->attributes['active_date']){ return ""; }
        return Carbon::parse($this->attributes['active_date'])->format(\Util::formatDateTimeDB());
    }

    /**
     * @return string
     */
    public function getAgreementFormat(){
        if(!$this->attributes['agreement']){ return ""; }
        return Carbon::parse($this->attributes['agreement'])->format(\Util::formatDateTimeDB());
    }

    /**
     * @return string
     */
    public function getLandByCountry(){
        if($this->account  && $this->account->country_id){
            $code = $this->account->country->code;
            if($code == "FR"){
                return 'fr';
            }
            if($code == "CH"){
                return 'de';
            }
            if($code == "NL"){
                return 'nl';
            }
            if($code == "DE"){
                return 'de';
            }
        }
        return "en";
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        Mail::to($this->email)->send(new MailResetPassword($token, $this));
        // $this->notify(new ResetPasswordNotification($token));
    }

}
