using Microsoft.AspNetCore.Mvc;
using WarehouseAppSHIPS.Data;
using System.Linq;

namespace WarehouseAppSHIPS.Controllers
{
    public class NotificationController : Controller
    {
        private readonly ApplicationDbContext _context;

        public NotificationController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpGet]
        public IActionResult GetHistory()
        {
            // HANYA AMBIL NOTIFIKASI YANG BELUM DIBACA (IsRead == false)
            var data = _context.AppNotifications
                .Where(n => !n.IsRead)
                .OrderByDescending(n => n.CreatedAt)
                .Take(10)
                .ToList();

            return Json(data);
        }

        // FUNGSI BARU: TANDAI SUDAH DIBACA
        [HttpPost]
        public IActionResult MarkAsRead(int id)
        {
            var notif = _context.AppNotifications.Find(id);
            if (notif != null)
            {
                notif.IsRead = true; // Ubah status jadi sudah dibaca
                _context.SaveChanges();
                return Ok();
            }
            return NotFound();
        }
    }
}