<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

    protected $fillable = [
        'membership_number',
        'first_name',
        'last_name',
        'dob',
        'email',
        'phone',
        'address',
        'city',
        'state',
        'profile_image',
        'password',
        'status',
        'suspension_reason',
        'suspended_until',
        'enrolled_at',
        'points_expiry_date',
        'points_balance',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'enrolled_at'       => 'datetime',
            'suspended_until'   => 'datetime',
            'dob'               => 'date',
            'points_expiry_date' => 'date',
            'password'          => 'hashed',
        ];
    }

    public function getFullNameAttribute(): string
    {
        return "{$this->first_name} {$this->last_name}";
    }

    public function currentTier()
    {
        return $this->hasOne(MemberTier::class)->where('is_current', true)->with('tier');
    }

    public function tiers()
    {
        return $this->hasMany(MemberTier::class)->with('tier')->latest('assigned_at');
    }

    public function pointsTransactions()
    {
        return $this->hasMany(PointsTransaction::class)->latest();
    }

    public function visits()
    {
        return $this->hasMany(Visit::class)->latest('visit_date');
    }

    public function tierAssessments()
    {
        return $this->hasMany(TierAssessment::class)->latest();
    }

    public function isActive(): bool
    {
        return $this->status === 'active';
    }

    public function isSuspended(): bool
    {
        return $this->status === 'suspended';
    }
}
