<?php

namespace FleekDash\Models;

defined('ABSPATH') || exit;

class Notifications extends BaseModel
{
    protected $table = 'fleekdash_notifications';

    protected $fillable = [
        'thread_id',
        'source',
        'source_id',
        'sender_info',
        'type',
        'status',
        'subject',
        'message',
        'actions',
        'meta',
        'snoozed_until',
        'resolved_at',
        'resolved_by',
        'read_at',
        'read_by',
    ];

    protected $casts = [
        'sender_info' => 'json',
        'actions' => 'json',
        'meta' => 'json',
    ];

    /**
     * Get unread notifications
     */
    public static function getUnread()
    {
        $instance = new static();

        $results = $instance->wpdb->get_results(
            "SELECT * FROM {$instance->table} WHERE status = 'unread' ORDER BY created_at DESC",
            ARRAY_A
        );

        return array_map([$instance, 'castAttributes'], $results);
    }

    /**
     * Get snoozed notifications
     */
    public static function getSnoozed()
    {
        $instance = new static();

        $results = $instance->wpdb->get_results(
            "SELECT * FROM {$instance->table} WHERE status = 'snoozed' ORDER BY created_at DESC",
            ARRAY_A
        );

        return array_map([$instance, 'castAttributes'], $results);
    }

    /**
     * Get visible notifications
     */
    public static function getVisible()
    {
        $instance = new static();

        $results = $instance->wpdb->get_results(
            "SELECT * FROM {$instance->table}
             WHERE status != 'resolved'
             AND (status != 'snoozed' OR snoozed_until <= NOW())
             ORDER BY created_at DESC",
            ARRAY_A
        );

        return array_map([$instance, 'castAttributes'], $results);
    }

    /**
     * Mark as read
     */
    public static function markAsRead($id, $userId = null)
    {
        $instance = new static();
        $userId = $userId ?: \get_current_user_id();

        return $instance->wpdb->update(
            $instance->table,
            [
                'status' => 'read',
                'read_at' => \current_time('mysql'),
                'read_by' => $userId
            ],
            ['id' => $id]
        );
    }

    /**
     * Resolve notification
     */
    public static function resolve($id, $userId = null)
    {
        $instance = new static();
        $userId = $userId ?: \get_current_user_id();

        return $instance->wpdb->update(
            $instance->table,
            [
                'status' => 'resolved',
                'resolved_at' => \current_time('mysql'),
                'resolved_by' => $userId
            ],
            ['id' => $id]
        );
    }

    /**
     * Snooze notification
     */
    public static function snooze($id, $until, $userId = null)
    {
        $instance = new static();
        $userId = $userId ?: \get_current_user_id();

        return $instance->wpdb->update(
            $instance->table,
            [
                'status' => 'snoozed',
                'snoozed_until' => $until,
                'read_at' => \current_time('mysql'),
                'read_by' => $userId
            ],
            ['id' => $id]
        );
    }

    /**
     * Add note to notification
     */
    public static function addNote($notification_id, $note, $userId = null)
    {
        $userId = $userId ?: \get_current_user_id();

        return NotificationNote::create([
            'notification_id' => $notification_id,
            'note' => $note,
            'user_id' => $userId
        ]);
    }

    /**
     * Get notes for notification
     */
    public static function getNotes($notification_id)
    {
        return NotificationNote::getByNotificationId($notification_id);
    }
}
