/**
 * WelcomeMessage Model
 * Manages welcome messages with tenant isolation
 * Matches 2.0 working implementation
 * 
 * @module models/WelcomeMessage
 */

const { pool } = require('../config/database');
const { logger } = require('../config/logger');

class WelcomeMessage {
  /**
   * Get all welcome messages for a tenant
   * @param {number} tenantId - Tenant ID
   * @param {number} instanceId - Optional Evolution Instance ID filter
   * @returns {Promise<Array>} List of welcome messages
   */
  static async getByTenantId(tenantId, instanceId = null) {
    try {
      let query = 'SELECT * FROM welcome_messages WHERE tenant_id = ? AND active = TRUE';
      const params = [tenantId];

      if (instanceId) {
        query += ' AND instance_id = ?';
        params.push(instanceId);
      }

      query += ' ORDER BY order_position ASC';

      const [rows] = await pool.query(query, params);
      return rows;
    } catch (error) {
      logger.error('Error getting welcome messages', { tenantId, error: error.message });
      throw error;
    }
  }

  /**
   * Create a welcome message
   * @param {number} tenantId - Tenant ID
   * @param {Object} data - Message data
   * @returns {Promise<Object>} Created message
   */
  static async create(tenantId, data) {
    try {
      const [result] = await pool.query(
        'INSERT INTO welcome_messages (tenant_id, instance_id, message_text, order_position, active) VALUES (?, ?, ?, ?, TRUE)',
        [tenantId, data.instance_id || null, data.message_text, data.order_position]
      );
      
      return {
        id: result.insertId,
        tenant_id: tenantId,
        instance_id: data.instance_id || null,
        message_text: data.message_text,
        order_position: data.order_position,
        active: true
      };
    } catch (error) {
      logger.error('Error creating welcome message', { tenantId, error: error.message });
      throw error;
    }
  }

  /**
   * Delete all welcome messages for a tenant
   * @param {number} tenantId - Tenant ID
   * @param {number} instanceId - Optional Evolution Instance ID filter
   * @returns {Promise<boolean>} Success status
   */
  static async deleteByTenantId(tenantId, instanceId = null) {
    try {
      let query = 'UPDATE welcome_messages SET active = FALSE WHERE tenant_id = ?';
      const params = [tenantId];

      if (instanceId) {
        query += ' AND instance_id = ?';
        params.push(instanceId);
      }

      await pool.query(query, params);
      return true;
    } catch (error) {
      logger.error('Error deleting welcome messages', { tenantId, error: error.message });
      throw error;
    }
  }

  /**
   * Save welcome messages (replace all) - matches 2.0 implementation
   * @param {number} tenantId - Tenant ID
   * @param {Array} messages - Array of message objects with text property
   * @param {number} instanceId - Optional Evolution Instance ID
   * @returns {Promise<boolean>} Success status
   */
  static async saveAll(tenantId, messages, instanceId = null) {
    const connection = await pool.getConnection();
    try {
      await connection.beginTransaction();

      // Deactivate all existing messages for this tenant/instance
      let deleteQuery = 'UPDATE welcome_messages SET active = FALSE WHERE tenant_id = ?';
      const deleteParams = [tenantId];

      if (instanceId) {
        deleteQuery += ' AND instance_id = ?';
        deleteParams.push(instanceId);
      }

      await connection.query(deleteQuery, deleteParams);

      // Insert new messages
      for (let i = 0; i < messages.length; i++) {
        const message = messages[i];
        await connection.query(
          'INSERT INTO welcome_messages (tenant_id, instance_id, message_text, order_position, active) VALUES (?, ?, ?, ?, TRUE)',
          [tenantId, instanceId || null, message.text, i + 1]
        );
      }

      await connection.commit();
      return true;
    } catch (error) {
      await connection.rollback();
      logger.error('Error saving welcome messages', { tenantId, error: error.message });
      throw error;
    } finally {
      connection.release();
    }
  }
}

module.exports = WelcomeMessage;
