<?php

namespace App\Models;

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

class JobAlertPreference extends Model
{
    protected $fillable = [
        'user_id',
        'enabled',
        'frequency',
        'minimum_match_score',
        'categories',
        'keywords',
        'last_sent_at',
        'unsubscribed_at',
    ];

    /**
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function isSubscribed(): bool
    {
        return $this->enabled && $this->unsubscribed_at === null;
    }

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'enabled' => 'boolean',
            'minimum_match_score' => 'integer',
            'categories' => 'array',
            'keywords' => 'array',
            'last_sent_at' => 'datetime',
            'unsubscribed_at' => 'datetime',
        ];
    }
}
