# Specification: Live Team Collaboration Features

## Overview
Implement and validate real-time collaboration features for the Jomblo Admin Dashboard, including team chat, user presence, and entity edit locking.

## User Stories
- **Presence:** As an admin, I want to see who else is currently online in the dashboard to know which team members are available.
- **Chat:** As an admin, I want to chat with other online team members in real-time to coordinate tasks and share information.
- **Edit Locking:** As an admin, I want to know if someone else is currently editing a product or category to avoid conflicting changes and data loss.

## Functional Requirements
- **Presence System:**
    - Track online/offline status of users using heartbeats.
    - Display a list of online users with their avatars/names in the dashboard sidebar or a dedicated panel.
- **Team Chat:**
    - Real-time message delivery using Redis (Pub/Sub) and Server-Sent Events (SSE).
    - Persistent chat history stored in PostgreSQL.
    - Support for text messages and basic system notifications (e.g., "User X joined").
- **Edit Locking:**
    - Automatically acquire a lock when a user enters an "Edit" page for a Product or Category.
    - Display a clear "Currently being edited by [User]" indicator for other users attempting to edit the same entity.
    - Release locks when the user navigates away or after a 5-minute inactivity timeout.

## Technical Requirements
- **Backend:** 
    - Next.js API Routes for presence heartbeats and chat message handling.
    - Redis for high-frequency presence tracking and real-time message broadcasting.
    - PostgreSQL for reliable storage of chat history and entity lock states (if needed for persistence).
- **Frontend:** 
    - Custom React hooks (`usePresence`, `useChat`) to manage real-time state.
    - Shadcn UI components for a polished and responsive "Team Chat" widget.
    - Lucide icons for status indicators.
- **Security:** 
    - All collaboration APIs must require a valid session via Auth.js.
    - Rate limiting for chat messages to prevent abuse.

## Acceptance Criteria
- Online status updates within 10 seconds of a user joining or leaving.
- Chat messages delivered to all online users in less than 500ms.
- Edit locks correctly prevent simultaneous editing and display the correct "In use" state.
- All features work correctly on mobile viewports.
