<?php

namespace App\Models;

use App\Models\Traits\HasUidTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use HasTeams;
    use HasUidTrait;
    use Notifiable;
    use Prunable;
    use TwoFactorAuthenticatable;

    /** @var string */
    protected $table = 'users';

    /** @var string */
    protected $keyType = 'string';

    /** @var bool */
    public $incrementing = false;

    /** @var array */
    protected $fillable = [
        'name',
        'surname',
        'timezone_name',
        'current_team_id',
        'profile_photo_path',
        'email',
        'password',
    ];

    /** @var array */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /** @var array */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /** @var array */
    protected $dates = [];

    /** @var array */
    protected $appends = [
        'full_name',
        'surname_full',
        'profile_photo_url',
    ];

    /** @var array */
    protected $touches = [];

    /** @var array */
    protected $dispatchesEvents = [];

    /*
    |--------------------------------------------------------------------------
    | Class Constants
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Custom/Private Methods
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Get the prunable model query.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function prunable()//: Builder
    {
        //return static::where('created_at', '<=', now()->subMonth());
    }

    /**
     * Prepare the model for pruning.
     *
     * @since 1.0.0
     *
     * @return void
     */
    protected function pruning(): void
    {
        //
    }

    /*
    |--------------------------------------------------------------------------
    | Accessors
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Return both the (first)name and surname.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    public function fullName(): Attribute
    {
        return Attribute::make(
            get: fn ($value, $attributes) => "{$attributes['name']} {$attributes['surname']}",
        );
    }

    /**
     * Return the surname then (first)name separated by a comma.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    public function surnameFull(): Attribute
    {
        return Attribute::make(
            get: fn ($value, $attributes) => "{$attributes['surname']}, {$attributes['name']}",
        );
    }

    /*
    |--------------------------------------------------------------------------
    | Mutators
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Scopes
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Return the associated Address with a User.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function addresses(): MorphOne
    {
        return $this->morphOne(Address::class, 'addressable');
    }
}
