import api from './api';

export interface DocumentToken {
  id: string;
  tokenIndex: number;
  termId: string | null;
  displayText: string;
  isPunctuation: boolean;
  term?: any;
}

export interface Sentence {
  id: string;
  sentenceIndex: number;
  rawText: string | null;
  tokens: DocumentToken[];
}

export interface DocumentPage {
  id: string;
  pageIndex: number;
  title: string | null;
  sentences: Sentence[];
}

export interface Document {
  id: string;
  title: string;
  description: string | null;
  visibility: string;
  createdAt: string;
  updatedAt: string;
  pages: DocumentPage[];
}

export interface CreateDocumentData {
  title: string;
  description?: string;
  visibility?: 'PRIVATE' | 'SHARED' | 'PUBLIC';
}

export interface UpdateDocumentData {
  title?: string;
  description?: string;
  visibility?: 'PRIVATE' | 'SHARED' | 'PUBLIC';
}

export interface CreateSentenceData {
  tokens: {
    termId: string | null;
    displayText: string;
    isPunctuation: boolean;
  }[];
}

export const documentApi = {
  // Get all documents for the current user
  async getDocuments(): Promise<Document[]> {
    const response = await api.get('/api/documents');
    return response.data.documents;
  },

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

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

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

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

  // Create a new page in a document
  async createPage(documentId: string, title?: string): Promise<DocumentPage> {
    const response = await api.post(`/api/documents/${documentId}/pages`, { title });
    return response.data.page;
  },

  // Create a new sentence on a page
  async createSentence(
    documentId: string,
    pageIndex: number,
    data: CreateSentenceData
  ): Promise<Sentence> {
    const response = await api.post(
      `/api/documents/${documentId}/pages/${pageIndex}/sentences`,
      data
    );
    return response.data.sentence;
  },

  // Update sentence tokens (reorder, add, remove)
  async updateSentenceTokens(
    sentenceId: string,
    tokens: {
      termId: string | null;
      displayText: string;
      isPunctuation: boolean;
    }[]
  ): Promise<Sentence> {
    const response = await api.patch(`/api/sentences/${sentenceId}/tokens`, {
      tokens,
    });
    return response.data.sentence;
  },

  // Delete a sentence
  async deleteSentence(sentenceId: string): Promise<void> {
    await api.delete(`/api/sentences/${sentenceId}`);
  },
};

