<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Notification extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    // Fetch notifications for the logged-in user
    public function get_notifications() {
        $user_id = $this->input->get('user_id'); // Assume passed via query param or auth token
        if (empty($user_id)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(400)
                ->set_output(json_encode(['success' => false, 'message' => 'USER_ID_REQUIRED']));
        }

        $this->db->where('user_id', $user_id);
        $this->db->where('is_active', 1);
        $this->db->order_by('created_at', 'DESC');
        $notifications = $this->db->get('notifications')->result_array();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode([
                'success' => true,
                'data' => $notifications
            ]));
    }

    // Fetch unread notification count
    public function get_unread_count() {
        $user_id = $this->input->get('user_id');
        if (empty($user_id)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(400)
                ->set_output(json_encode(['success' => false, 'message' => 'USER_ID_REQUIRED']));
        }

        $this->db->where('user_id', $user_id);
        $this->db->where('is_read', 0);
        $this->db->where('is_active', 1);
        $count = $this->db->count_all_results('notifications');

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode([
                'success' => true,
                'data' => ['unread_count' => $count]
            ]));
    }

    // Mark notification as read
    public function mark_as_read() {
        $notification_id = $this->input->post('notification_id');
        $user_id = $this->input->post('user_id');

        if (empty($notification_id) || empty($user_id)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(400)
                ->set_output(json_encode(['success' => false, 'message' => 'INVALID_INPUT']));
        }

        $this->db->where('id', $notification_id);
        $this->db->where('user_id', $user_id);
        $this->db->update('notifications', ['is_read' => 1, 'updated_at' => date('Y-m-d H:i:s')]);

        if ($this->db->affected_rows() > 0) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(['success' => true, 'message' => 'NOTIFICATION_MARKED_AS_READ']));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(400)
                ->set_output(json_encode(['success' => false, 'message' => 'NOTIFICATION_NOT_FOUND_OR_ALREADY_READ']));
        }
    }
}