<?php

namespace App\Models;

use CodeIgniter\Model;

class NotificationPreferenceModel extends Model
{
    protected $table         = 'notification_preferences';
    protected $useTimestamps = false;
    protected $allowedFields = ['user_id', 'email_enabled', 'sms_enabled', 'in_app_enabled', 'whatsapp_enabled'];

    public function forUser(int $userId): array
    {
        $existing = $this->where('user_id', $userId)->first();
        if ($existing) {
            return $existing;
        }
        // Defaults: email + in_app on, sms + whatsapp off — matches the
        // notification_preferences table defaults, materialized lazily.
        $id = $this->insert(['user_id' => $userId, 'email_enabled' => 1, 'sms_enabled' => 0, 'in_app_enabled' => 1, 'whatsapp_enabled' => 0], true);
        return $this->find($id);
    }
}
