import api from './api';

export interface Announcement {
    id: string;
    title: string;
    body: string;
    priority: 'INFO' | 'WARNING' | 'CRITICAL';
    requiresAck: boolean;
    targetRoles: string[];
    expiresAt?: string;
    createdBy: { id: string; name: string };
    createdAt: string;
    isRead?: boolean;
    isAcknowledged?: boolean;
}

export interface ShiftNote {
    id: string;
    roomId?: string;
    batchId?: string;
    content: string;
    importance: 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';
    createdBy: { id: string; name: string };
    createdAt: string;
    isRead?: boolean;
}

export const messagingApi = {
    // Announcements
    async getAnnouncements(): Promise<Announcement[]> {
        const response = await api.get('/messaging/announcements');
        return response.data;
    },

    async getPendingAnnouncements(): Promise<{ count: number; announcements: Announcement[] }> {
        const response = await api.get('/messaging/announcements/pending');
        return response.data;
    },

    async createAnnouncement(data: {
        title: string;
        body: string;
        priority?: 'INFO' | 'WARNING' | 'CRITICAL';
        requiresAck?: boolean;
        targetRoles?: string[];
        expiresAt?: string;
    }): Promise<Announcement> {
        const response = await api.post('/messaging/announcements', data);
        return response.data;
    },

    async markRead(id: string): Promise<void> {
        await api.post(`/messaging/announcements/${id}/read`);
    },

    async acknowledge(id: string): Promise<{ success: boolean; acknowledgedAt: string }> {
        const response = await api.post(`/messaging/announcements/${id}/acknowledge`);
        return response.data;
    },

    async getAcknowledgements(id: string): Promise<any> {
        const response = await api.get(`/messaging/announcements/${id}/acks`);
        return response.data;
    },

    // Shift Notes
    async getShiftNotes(params?: { date?: string; limit?: number; roomId?: string; batchId?: string }): Promise<ShiftNote[]> {
        const response = await api.get('/messaging/shift-notes', { params });
        return response.data;
    },

    async createShiftNote(data: {
        content: string;
        roomId?: string;
        batchId?: string;
        importance?: 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';
        expiresAt?: string;
    }): Promise<ShiftNote> {
        const response = await api.post('/messaging/shift-notes', data);
        return response.data;
    },


    async markShiftNoteRead(id: string): Promise<void> {
        await api.post(`/messaging/shift-notes/${id}/read`);
    }
};

