import api from './api';

export type DocumentType = 'SOP' | 'POLICY' | 'FORM' | 'CHECKLIST' | 'TRAINING' | 'OTHER';
export type DocumentStatus = 'DRAFT' | 'PENDING_APPROVAL' | 'APPROVED' | 'ARCHIVED';

export interface Document {
    id: string;
    title: string;
    type: DocumentType;
    category?: string;
    content: string;
    status: DocumentStatus;
    version: number;
    effectiveDate?: string;
    expiresAt?: string;
    requiresAck: boolean;
    createdById: string;
    createdBy?: { id: string; name: string };
    approvedById?: string;
    approvedBy?: { id: string; name: string };
    approvedAt?: string;
    createdAt: string;
    updatedAt: string;
}

export interface DocumentVersion {
    id: string;
    documentId: string;
    version: number;
    content: string;
    changeNotes?: string;
    createdById: string;
    createdBy?: { id: string; name: string };
    createdAt: string;
}

export interface DocumentAck {
    id: string;
    documentId: string;
    userId: string;
    user?: { id: string; name: string };
    acknowledgedAt: string;
}

export interface CreateDocumentData {
    title: string;
    type: DocumentType;
    category?: string;
    content: string;
    requiresAck?: boolean;
    effectiveDate?: string;
    expiresAt?: string;
}

export interface UpdateDocumentData extends Partial<CreateDocumentData> {
    changeNotes?: string;
}

export const documentsApi = {
    async getDocuments(params?: {
        type?: DocumentType;
        status?: DocumentStatus;
        category?: string;
        search?: string;
    }): Promise<Document[]> {
        const response = await api.get('/documents', { params });
        return response.data;
    },

    async getDocument(id: string): Promise<Document> {
        const response = await api.get(`/documents/${id}`);
        return response.data;
    },

    async createDocument(data: CreateDocumentData): Promise<Document> {
        const response = await api.post('/documents', data);
        return response.data;
    },

    async updateDocument(id: string, data: UpdateDocumentData): Promise<Document> {
        const response = await api.patch(`/documents/${id}`, data);
        return response.data;
    },

    async deleteDocument(id: string): Promise<void> {
        await api.delete(`/documents/${id}`);
    },

    async getVersions(id: string): Promise<DocumentVersion[]> {
        const response = await api.get(`/documents/${id}/versions`);
        return response.data;
    },

    async submitForApproval(id: string): Promise<Document> {
        const response = await api.post(`/documents/${id}/submit`);
        return response.data;
    },

    async approveDocument(id: string): Promise<Document> {
        const response = await api.post(`/documents/${id}/approve`);
        return response.data;
    },

    async rejectDocument(id: string, reason?: string): Promise<Document> {
        const response = await api.post(`/documents/${id}/reject`, { reason });
        return response.data;
    },

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

    async getAcknowledgements(id: string): Promise<{
        document: { id: string; title: string };
        requiresAck: boolean;
        acknowledged: DocumentAck[];
        pending: { id: string; name: string }[];
    }> {
        const response = await api.get(`/documents/${id}/acks`);
        return response.data;
    },

    async getPendingAcks(): Promise<Document[]> {
        const response = await api.get('/documents/pending-acks');
        // Backend returns { count, documents }
        return response.data.documents || [];
    }
};

