// backend/models/Assistant.js
import pool from '../config/db.js';

const Assistant = {
  async createAssistant(user_id, assistant_name) {
    try {
      const [result] = await pool.execute(
        'INSERT INTO ai_assistants (user_id, assistant_name) VALUES (?, ?)',
        [user_id, assistant_name]
      );
      return result.insertId;
    } catch (error) {
      throw new Error('Database error: Unable to create assistant');
    }
  },

  async getUserAssistants(user_id) {
    try {
      const [rows] = await pool.execute('SELECT * FROM ai_assistants WHERE user_id = ?', [user_id]);
      return rows;
    } catch (error) {
      throw new Error('Database error: Unable to fetch assistants');
    }
  },

  async deleteAssistant(id, user_id) {
    try {
      await pool.execute('DELETE FROM ai_assistants WHERE id = ? AND user_id = ?', [id, user_id]);
    } catch (error) {
      throw new Error('Database error: Unable to delete assistant');
    }
  },

  async assignFileToAssistant(assistant_id, file_id) {
    try {
      await pool.execute('INSERT INTO assistant_files (assistant_id, file_id) VALUES (?, ?)', [assistant_id, file_id]);
    } catch (error) {
      throw new Error('Database error: Unable to assign file to assistant');
    }
  },

  async removeFileFromAssistant(assistant_id, file_id) {
    try {
      await pool.execute('DELETE FROM assistant_files WHERE assistant_id = ? AND file_id = ?', [assistant_id, file_id]);
    } catch (error) {
      throw new Error('Database error: Unable to remove file from assistant');
    }
  }
};

export default Assistant;
