<?php

declare(strict_types=1);

namespace App\Domain\Notifications\Models;

use App\Domain\Account\Models\Account;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property int|null $user_id
 * @property int|null $account_id
 * @property string $category
 * @property string $priority
 * @property string $title
 * @property string $body
 * @property string|null $icon
 * @property string|null $link
 * @property Carbon|null $read_at
 * @property Carbon|null $expires_at
 * @property array|null $meta
 */
class PanelNotification extends Model
{
    protected $fillable = [
        'user_id', 'account_id', 'category', 'priority',
        'title', 'body', 'icon', 'link',
        'read_at', 'expires_at', 'meta',
    ];

    protected function casts(): array
    {
        return [
            'read_at' => 'datetime',
            'expires_at' => 'datetime',
            'meta' => 'array',
        ];
    }

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

    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    public function isRead(): bool
    {
        return $this->read_at !== null;
    }

    public function markAsRead(): void
    {
        if (! $this->isRead()) {
            $this->update(['read_at' => now()]);
        }
    }
}
