<?php

namespace App\Services;

use App\Models\UserUgel;
use Config\Database;
use PDO;

class NotificationService
{
    public static function createNotification($processId, $message, $userId = null, $groupId = null, $subgroup_id = null)
    {
        $db = Database::getConnection();
        $query = "INSERT INTO notifications (user_id, group_id, subgroup_id, process_id, message) VALUES (?, ?, ?, ?, ?)";
        $stmt = $db->prepare($query);
        $stmt->execute([$userId, $groupId, $subgroup_id, $processId, $message]);
    }

    public static function getNotifications()
    {
        $db = Database::getConnection();

        $user_id = $_SESSION['user_id'] ?? null;
        $role = $_SESSION['role'] ?? null;

        if (!$user_id || !$role) {
            return [];
        }

        if ($role === 'UGEL' || $role === 'ADMIN') {
            $user = UserUgel::findBy('user_id', $user_id);
            $group_id = $user->group_id;
            $subgroup_id = $user->subgroup_id ?? null;

            if ($subgroup_id) {
                $query = "SELECT * FROM notifications 
                        WHERE group_id = :group_id 
                        AND subgroup_id = :subgroup_id 
                        AND is_read = 0 
                        ORDER BY created_at DESC";
                $params = [':group_id' => $group_id, ':subgroup_id' => $subgroup_id];
            } else {
                $query = "SELECT * FROM notifications 
                        WHERE group_id = :group_id AND subgroup_id IS NULL 
                        AND is_read = 0 
                        ORDER BY created_at DESC";
                $params = [':group_id' => $group_id];
            }
        } else {
            $query = "SELECT * FROM notifications 
                    WHERE user_id = :user_id 
                    AND is_read = 0 
                    ORDER BY created_at DESC";
            $params = [':user_id' => $user_id];
        }

        $stmt = $db->prepare($query);
        $stmt->execute($params);

        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $notifications = [];
        foreach ($results as $data) {
            $notification = new static();
            foreach ($data as $key => $value) {
                $notification->$key = $value;
            }
            $notifications[] = $notification;
        }

        return $notifications;
    }

    public static function markNotificationAsReadByProcess($processId, $groupId, $subgroupId = null)
    {
        $db = Database::getConnection();

        if ($subgroupId) {
            $query = "UPDATE notifications SET is_read = 1 WHERE process_id = ? AND subgroup_id = ?";
            $stmt = $db->prepare($query);
            return $stmt->execute([$processId, $subgroupId]);
        } else {
            $query = "UPDATE notifications SET is_read = 1 WHERE process_id = ? AND group_id = ? AND subgroup_id IS NULL";
            $stmt = $db->prepare($query);
            return $stmt->execute([$processId, $groupId]);
        }
    }


    public static function markNotificationsAsReadByGroup($groupId, $subgroupId = null)
    {
        $db = Database::getConnection();

        if ($subgroupId) {
            $query = "UPDATE notifications 
                    SET is_read = 1 
                    WHERE group_id = ? AND subgroup_id = ? AND is_read = 0";
            $params = [$groupId, $subgroupId];
        } else {
            $query = "UPDATE notifications 
                    SET is_read = 1 
                    WHERE group_id = ? AND subgroup_id IS NULL AND is_read = 0";
            $params = [$groupId];
        }

        $stmt = $db->prepare($query);
        return $stmt->execute($params);
    }

    public static function markNotificationsAsReadByUser($userId)
    {
        $db = Database::getConnection();
        $query = "UPDATE notifications 
                SET is_read = 1 
                WHERE user_id = ? AND is_read = 0";
        $stmt = $db->prepare($query);
        return $stmt->execute([$userId]);
    }

}
