<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class NotificationModel extends Model
{
    public function create(array $data): int
    {
        $stmt = $this->db->prepare("
            INSERT INTO notifications (
                user_id, type, title, message, related_table, related_id, is_read, created_at
            ) VALUES (
                :user_id, :type, :title, :message, :related_table, :related_id, :is_read, NOW()
            )
        ");
        $stmt->execute([
            ':user_id' => $data['user_id'],
            ':type' => $data['type'],
            ':title' => $data['title'],
            ':message' => $data['message'],
            ':related_table' => $data['related_table'] ?? null,
            ':related_id' => $data['related_id'] ?? null,
            ':is_read' => $data['is_read'] ?? 0,
        ]);

        return (int) $this->db->lastInsertId();
    }

    public function byUser(int $userId, int $limit = 50): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM notifications
            WHERE user_id = :user_id
            ORDER BY id DESC
            LIMIT {$limit}
        ");
        $stmt->execute([':user_id' => $userId]);
        return $stmt->fetchAll();
    }

    public function unreadByUser(int $userId, int $limit = 10): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM notifications
            WHERE user_id = :user_id AND is_read = 0
            ORDER BY id DESC
            LIMIT {$limit}
        ");
        $stmt->execute([':user_id' => $userId]);
        return $stmt->fetchAll();
    }

    public function unreadCount(int $userId): int
    {
        $stmt = $this->db->prepare("
            SELECT COUNT(*)
            FROM notifications
            WHERE user_id = :user_id AND is_read = 0
        ");
        $stmt->execute([':user_id' => $userId]);
        return (int) $stmt->fetchColumn();
    }

    public function findForUser(int $id, int $userId): ?array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM notifications
            WHERE id = :id AND user_id = :user_id
            LIMIT 1
        ");
        $stmt->execute([
            ':id' => $id,
            ':user_id' => $userId,
        ]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    public function markRead(int $id, int $userId): void
    {
        $stmt = $this->db->prepare("
            UPDATE notifications
            SET is_read = 1, read_at = NOW()
            WHERE id = :id AND user_id = :user_id
        ");
        $stmt->execute([
            ':id' => $id,
            ':user_id' => $userId,
        ]);
    }

    public function markAllRead(int $userId): void
    {
        $stmt = $this->db->prepare("
            UPDATE notifications
            SET is_read = 1, read_at = NOW()
            WHERE user_id = :user_id AND is_read = 0
        ");
        $stmt->execute([':user_id' => $userId]);
    }
}
