<?php

namespace App\Models;

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

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'role',
        'firebase_uid',
        'firebase_provider',
        'phone',
        'address',
        'avatar_url',
        'bio',
        'payment_method',
        'payout_method',
        'is_kyc_verified',
        'is_available',
        'email_notifications_enabled',
        'language',
        'region',
        'push_notifications_enabled',
        'sms_notifications_enabled',
        'email_marketing_enabled',
        'last_seen_at',
        'current_latitude',
        'current_longitude',
        'current_location_updated_at',
        'email_verified_at',
        'phone_verified_at',
        'password',
    ];

    /**
     * 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',
            'phone_verified_at' => 'datetime',
            'password' => 'hashed',
            'payment_method' => 'array',
            'payout_method' => 'array',
            'is_kyc_verified' => 'boolean',
            'is_available' => 'boolean',
            'email_notifications_enabled' => 'boolean',
            'push_notifications_enabled' => 'boolean',
            'sms_notifications_enabled' => 'boolean',
            'email_marketing_enabled' => 'boolean',
            'last_seen_at' => 'datetime',
            'current_latitude' => 'decimal:7',
            'current_longitude' => 'decimal:7',
            'current_location_updated_at' => 'datetime',
        ];
    }

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

    public function clientMissions(): HasMany
    {
        return $this->hasMany(Mission::class, 'client_id');
    }

    public function linerMissions(): HasMany
    {
        return $this->hasMany(Mission::class, 'liner_id');
    }

    public function missionApplications(): HasMany
    {
        return $this->hasMany(MissionApplication::class, 'liner_id');
    }

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

    public function payouts(): HasMany
    {
        return $this->hasMany(Payout::class, 'liner_id');
    }

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

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

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

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

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

    public function isAdmin(): bool
    {
        return strtolower($this->role) === 'admin';
    }
}
