import apiService from './apiService';
import API_CONFIG from '../config/api';

// ============================================================
// Comment Service - CRUD operations for ERPNext Comment doctype
// ============================================================

export interface CommentData {
  name: string;
  owner: string;
  creation: string;
  modified: string;
  modified_by: string;
  comment_type: string;
  comment_email: string;
  comment_by: string;
  content: string;
  reference_doctype: string;
  reference_name: string;
  published: number;
  seen: number;
}

export interface MentionUser {
  name: string;       // email / user ID
  full_name: string;  // display name
  user_image?: string; // avatar URL
}

const commentService = {
  // ─── Fetch comments for a specific document ───────────────
  async getComments(
    referenceDoctype: string,
    referenceName: string
  ): Promise<CommentData[]> {
    const filters = JSON.stringify([
      ['reference_doctype', '=', referenceDoctype],
      ['reference_name', '=', referenceName],
      ['comment_type', 'in', ['Comment', 'Info', 'Edit', 'Attachment', 'Like']],
    ]);
    const fields = JSON.stringify([
      'name', 'owner', 'creation', 'modified', 'modified_by',
      'comment_type', 'comment_email', 'comment_by',
      'content', 'reference_doctype', 'reference_name',
      'published', 'seen',
    ]);

    const res = await apiService.apiCall<any>(
      `/api/resource/Comment?filters=${encodeURIComponent(filters)}&fields=${encodeURIComponent(fields)}&order_by=creation asc&limit_page_length=100`
    );
    return res?.data ?? [];
  },

  // ─── Post a new comment ────────────────────────────────────
  async postComment(
    referenceDoctype: string,
    referenceName: string,
    content: string
  ): Promise<CommentData> {
    const baseUrl = API_CONFIG.BASE_URL || '';

    const res = await fetch(`${baseUrl}/api/resource/Comment`, {
      method: 'POST',
      credentials: 'include',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        comment_type: 'Comment',
        reference_doctype: referenceDoctype,
        reference_name: referenceName,
        content,
      }),
    });

    if (!res.ok) {
      const err = await res.json().catch(() => ({}));
      throw new Error(err?.exc || err?._server_messages || 'Failed to post comment');
    }

    const json = await res.json();
    return json.data;
  },

  // ─── Delete a comment ──────────────────────────────────────
  async deleteComment(commentName: string): Promise<void> {
    const baseUrl = API_CONFIG.BASE_URL || '';

    const res = await fetch(`${baseUrl}/api/resource/Comment/${encodeURIComponent(commentName)}`, {
      method: 'DELETE',
      credentials: 'include',
    });

    if (!res.ok) {
      const err = await res.json().catch(() => ({}));
      throw new Error(err?.exc || 'Failed to delete comment');
    }
  },

  // ─── Search users for @mention ─────────────────────────────
  async searchUsers(query: string): Promise<MentionUser[]> {
    const baseUrl = API_CONFIG.BASE_URL || '';

    // Use frappe.client.get_list via API to search User doctype
    const filters = query
      ? JSON.stringify([
          ['enabled', '=', 1],
          ['user_type', '=', 'System User'],
          ['name', 'like', `%${query}%`],
        ])
      : JSON.stringify([
          ['enabled', '=', 1],
          ['user_type', '=', 'System User'],
        ]);

    const fields = JSON.stringify(['name', 'full_name', 'user_image']);

    const res = await fetch(
      `${baseUrl}/api/resource/User?filters=${encodeURIComponent(filters)}&fields=${encodeURIComponent(fields)}&limit_page_length=20&order_by=full_name asc`,
      { credentials: 'include' }
    );

    if (!res.ok) return [];

    const json = await res.json();
    return (json.data ?? []).filter(
      (u: MentionUser) => u.name !== 'Administrator' && u.name !== 'Guest'
    );
  },

  // ─── Get current user email ────────────────────────────────
  async getCurrentUser(): Promise<string> {
    const baseUrl = API_CONFIG.BASE_URL || '';
    const res = await fetch(`${baseUrl}/api/method/frappe.auth.get_logged_user`, {
      credentials: 'include',
    });
    const json = await res.json();
    return json.message || '';
  },
};

export default commentService;