<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserPreference extends Model
{
    protected $fillable = [
        'user_id',
        'notify_due_payments',
        'notify_due_payments_time',
        'notify_due_payments_email',
        'language',
        'timezone',
        'currency',
    ];

    protected $casts = [
        'notify_due_payments' => 'boolean',
        // notify_due_payments_time is stored as TIME in DB, no cast needed
    ];

    /**
     * Get the user that owns the preferences.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the email to send notifications to.
     * Falls back to user's email if not set.
     */
    public function getNotificationEmail(): string
    {
        return $this->notify_due_payments_email ?? $this->user->email;
    }
}
