<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
 * @property int $id
 * @property string $username
 * @property string $local_group
 * @property string $firstname
 * @property string $nickname
 * @property string $lastname
 * @property string $membership_id
 * @property string $address_1
 * @property string $address_2
 * @property string $postcode
 * @property string $city
 * @property string $email
 * @property string $phone
 * @property string $birthday
 * @property string $medications
 * @property string $allergies
 * @property string $intolerances
 * @property string $eating_habits
 * @property string $swimming_permission
 * @property string $first_aid_permission
 * @property string $bank_account_owner
 * @property string $bank_account_iban
 * @property string $password
 * @property boolean $active
 * @property string $user_role_main
 * @property string $user_role_local_group
 * @property string $activation_token
 * @property string $activation_token_expires_at
 */
class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'user_role_main',
        'user_role_local_group',
        'activation_token',
        'activation_token_expires_at',
        'username',
        'local_group',
        'firstname',
        'nickname',
        'lastname',
        'membership_id',
        'address_1',
        'address_2',
        'postcode',
        'city',
        'email',
        'phone',
        'birthday',
        'medications',
        'allergies',
        'intolerances',
        'tetanus_vaccination',
        'eating_habits',
        'swimming_permission',
        'first_aid_permission',
        'bank_account_owner',
        'bank_account_iban',
        'password',
        'active',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'password' => 'hashed',
        ];
    }

    public function getOfficialName() : string {
        return sprintf('%1$s %2$s', $this->firstname, $this->lastname);
    }

    public function getFullname() : string {
        return sprintf('%1$1s %2$s %3$s',
            $this->firstname,
            $this->lastname,
            $this->nickname !== null ? '('  . $this->nickname . ')' : '',
        )
        |>trim(...);
    }

    public function getNicename() : string {
        return $this->nickname ?? $this->firstname;
    }

    public function localGroup() : Tenant {
        return $this->belongsTo(Tenant::class, 'local_group', 'slug')->first();
    }

    public function costUnits()
    {
        return $this->belongsToMany(CostUnit::class, 'cost_unit_treasurers', 'user_id', 'cost_unit_id')
            ->withTimestamps();
    }
}
