<?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;
use Illuminate\Support\Str;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    const GENDER_MALE = 'male';
    const GENDER_FEMALE = 'female';



    protected $fillable = [
        'name',
        'chat_id',
        'username',
        'goal',
        'level',
        'gender',
        'practice_time',
        'description',
        'mobile',
        'ref_code',
        'ref_by',
        'filters'
    ];


    /**
     * 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',
            'password' => 'hashed',
        ];
    }

    public function getGenderAttribute($value)
    {
        return match($value) {
            self::GENDER_MALE => 'آقا',
            self::GENDER_FEMALE => 'خانم',
            default => 'نامشخص',
        };
    }

    public function getPracticeTimeAttribute($value)
    {
        return match($value) {
            'morning' => 'صبح',
            'noon' => 'ظهر',
            'afternoon' => 'عصر',
            'night' => 'شب',
            'deal' => 'توافقی',
            default => 'نامشخص',
        };
    }


    public static function generateReferralCode()
    {
        do {
            $code = bin2hex(random_bytes(5));
        } while (self::where('ref_code', $code)->exists());

        return $code;
    }

}
