<?php

namespace App\Models;

use CodeIgniter\Model;

class NotificationModel extends Model
{
    protected $table            = 'notifications';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $allowedFields    = ['user_id', 'order_id', 'type', 'title', 'body', 'read_at', 'created_at'];
    protected $useTimestamps    = false; // table has created_at only; avoid CI adding trailing comma for updated_at

    /**
     * Create an order-status notification for a user (log only; FCM can be attached later).
     */
    public static function logOrderStatus(int $userId, int $orderId, string $status): void
    {
        $titles = [
            'paid'      => 'Order confirmed',
            'dispatched' => 'Order on the way',
            'delivered' => 'Order delivered',
            'cancelled' => 'Order cancelled',
        ];
        $title = $titles[$status] ?? 'Order updated';
        $body  = sprintf('Order #%d is now %s.', $orderId, $status);

        $model = model(self::class);
        $model->insert([
            'user_id'   => $userId,
            'order_id'  => $orderId,
            'type'      => 'order_status',
            'title'     => $title,
            'body'      => $body,
            'created_at' => date('Y-m-d H:i:s'),
        ]);
    }
}
