<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Support\AvatarUrl;

class UserProfile extends Model
{
    protected $table = 'user_profiles';
    protected $primaryKey = 'user_id';
    public $incrementing = false;
    protected $keyType = 'int';

    protected $fillable = [
        'user_id',
        'about',
        'signature',
        'description',
        'avatar_legacy',
        'avatar_hash',
        'avatar_mime',
        'avatar_updated_at',
        'cover_image',
        'country',
        'country_code',
        'language',
        'birthdate',
        'gender',
        'website',
        'auto_post_upload',
        'email_notifications',
        'upload_notifications',
        'follower_notifications',
        'comment_notifications',
        'newsletter',
        'mature_content_visibility',
        'mature_content_warning_enabled',
    ];

    protected $casts = [
        'birthdate'        => 'date',
        'avatar_updated_at'=> 'datetime',
        'auto_post_upload' => 'boolean',
        'email_notifications' => 'boolean',
        'upload_notifications' => 'boolean',
        'follower_notifications' => 'boolean',
        'comment_notifications' => 'boolean',
        'newsletter' => 'boolean',
        'mature_content_warning_enabled' => 'boolean',
    ];

    public $timestamps = true;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function getAvatarUrlAttribute(): ?string
    {
        if (empty($this->user_id)) {
            return null;
        }

        return AvatarUrl::forUser((int) $this->user_id, $this->avatar_hash, 128);
    }
}
