<?php

namespace App\Models;

use App\Enums\PacePreference;
use App\Enums\PhysicalActivityLevel;
use App\Enums\UserRole;
use App\Enums\UserSex;
use App\Enums\WeightGoal;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasAvatar;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Storage;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'locale',
        'role',
        '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',
        'suspended_at',
        'suspended_by',
        'suspended_reason',
        'ai_credits_balance',
    ];

    /**
     * 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,
            'role' => UserRole::class,
            'date_of_birth' => 'immutable_date',
            'suspended_at' => 'datetime',
            'trial_ends_at' => 'datetime',
            'ai_credits_balance' => 'integer',
        ];
    }

    public function getFilamentAvatarUrl(): ?string
    {
        return $this->avatar_url ? Storage::url($this->avatar_url) : null;
    }

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

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

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

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

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

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

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

    public function reportsMade(): HasMany
    {
        return $this->hasMany(Report::class, 'reporter_id');
    }

    public function receivedReports(): MorphMany
    {
        return $this->morphMany(Report::class, 'reportable');
    }

    public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(self::class, 'follows', 'following_id', 'follower_id')
            ->withPivot('status')
            ->withTimestamps();
    }

    public function following(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(self::class, 'follows', 'follower_id', 'following_id')
            ->withPivot('status')
            ->withTimestamps();
    }

    public function moderationCase(): MorphOne
    {
        return $this->morphOne(ModerationCase::class, 'caseable');
    }

    public function assignedModerationCases(): HasMany
    {
        return $this->hasMany(ModerationCase::class, 'assigned_to');
    }

    public function resolvedModerationCases(): HasMany
    {
        return $this->hasMany(ModerationCase::class, 'resolved_by');
    }

    public function suspendedBy(): BelongsTo
    {
        return $this->belongsTo(self::class, 'suspended_by');
    }

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

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

    public function preferredLocale(): ?string
    {
        return $this->locale ?: 'en';
    }

    public function isAdmin(): bool
    {
        return $this->role === UserRole::ADMIN;
    }

    public function canAccessPanel(Panel $panel): bool
    {
        return $this->role === UserRole::ADMIN || $this->role === UserRole::MODERATOR;
    }
}
