<?php

namespace App\Models;

use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements FilamentUser, MustVerifyEmail
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasApiTokens, HasFactory, HasUlids, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'email_verified_at',
        'password',
        'avatar_url',
        'height',
        'bio',
        'account_verified_at',
        'daily_calorie_goal',
        'daily_protein_goal',
        'daily_carbs_goal',
        'daily_fats_goal',
        'physical_activity_level',
        'weight_goal',
        'pace_preference',
        'sex',
        'date_of_birth',
    ];

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

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'account_verified_at' => 'datetime',
            'password' => 'hashed',
            'daily_calorie_goal' => 'integer',
            'daily_protein_goal' => 'float',
            'daily_carbs_goal' => 'float',
            'daily_fats_goal' => 'float',
            'physical_activity_level' => PhysicalActivityLevel::class,
            'weight_goal' => WeightGoal::class,
            'pace_preference' => PacePreference::class,
            'sex' => UserSex::class,
            'date_of_birth' => 'immutable_date',
        ];
    }

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

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

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

    public function stravaConnection(): HasOne
    {
        return $this->hasOne(StravaConnection::class);
    }

    public function likedMealPosts(): BelongsToMany
    {
        return $this->belongsToMany(MealPosts::class, 'meal_post_likes', 'user_id', 'meal_posts_id')
            ->withTimestamps();
    }

    public function routeNotificationForExpoPush(): array
    {
        return $this->deviceTokens()
            ->orderBy('id')
            ->pluck('expo_push_token')
            ->all();
    }

    public function isAdmin()
    {
        return true;
        //        return $this->role === 'admin';
    }

    public function canAccessPanel(Panel $panel): bool
    {

        return true;
    }
}
