<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    protected $fillable = [
        'name',
        'email',
        'password',
        'date_of_birth',
        'age_verified_at',
        'loyalty_points',
        'google_id',
        'facebook_id',
        'phone',
        'email_marketing_consent',
        'phone_verified_at',
    ];

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

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'age_verified_at' => 'datetime',
            'date_of_birth' => 'date',
            'password' => 'hashed',
            'loyalty_points'           => 'integer',
            'email_marketing_consent'  => 'boolean',
            'phone_verified_at'        => 'datetime',
        ];
    }

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function addresses(): HasMany
    {
        return $this->hasMany(Address::class);
    }

    public function loyaltyTransactions(): HasMany
    {
        return $this->hasMany(LoyaltyTransaction::class);
    }

    public function isAgeVerified(): bool
    {
        return $this->age_verified_at !== null;
    }

    public function isAdult(): bool
    {
        return $this->date_of_birth !== null && $this->date_of_birth->age >= 18;
    }
}
