var LudoChat = (function() {
  'use strict';

  var C = LudoConstants;
  var containerEl = null;
  var messagesEl = null;
  var inputEl = null;
  var quickEl = null;
  var matchId = null;
  var lastSentAt = 0;
  var COOLDOWN = 2000;

  function init(container, matchIdVal, onSend) {
    matchId = matchIdVal;
    containerEl = typeof container === 'string' ? document.querySelector(container) : container;
    if (!containerEl) return;

    containerEl.innerHTML = '';
    containerEl.className = 'ludo-chat';

    messagesEl = document.createElement('div');
    messagesEl.className = 'ludo-chat-messages';

    quickEl = document.createElement('div');
    quickEl.className = 'ludo-chat-quick';
    C.QUICK_MESSAGES.forEach(function(msg) {
      var btn = document.createElement('button');
      btn.className = 'ludo-chat-quick-btn';
      btn.textContent = msg;
      btn.addEventListener('click', function() {
        send(msg, onSend);
      });
      quickEl.appendChild(btn);
    });

    var inputRow = document.createElement('div');
    inputRow.className = 'ludo-chat-input-row';

    inputEl = document.createElement('input');
    inputEl.type = 'text';
    inputEl.className = 'ludo-chat-input';
    inputEl.placeholder = 'اكتب رسالة...';
    inputEl.maxLength = 100;

    var sendBtn = document.createElement('button');
    sendBtn.className = 'ludo-chat-send-btn';
    sendBtn.textContent = 'ارسل';
    sendBtn.addEventListener('click', function() {
      var text = inputEl.value.trim();
      if (text) {
        send(text, onSend);
        inputEl.value = '';
      }
    });

    inputEl.addEventListener('keydown', function(e) {
      if (e.key === 'Enter') {
        var text = inputEl.value.trim();
        if (text) {
          send(text, onSend);
          inputEl.value = '';
        }
      }
    });

    inputRow.appendChild(inputEl);
    inputRow.appendChild(sendBtn);

    containerEl.appendChild(messagesEl);
    containerEl.appendChild(quickEl);
    containerEl.appendChild(inputRow);
  }

  function send(text, onSend) {
    var now = Date.now();
    if (now - lastSentAt < COOLDOWN) return;
    lastSentAt = now;

    if (onSend) onSend(text);
  }

  function addMessage(senderName, text, isSystem) {
    if (!messagesEl) return;

    var msg = document.createElement('div');
    msg.className = 'ludo-chat-msg';
    if (isSystem) msg.classList.add('ludo-chat-msg--system');

    if (!isSystem && senderName) {
      var nameEl = document.createElement('span');
      nameEl.className = 'ludo-chat-msg-name';
      nameEl.textContent = senderName;
      msg.appendChild(nameEl);
    }

    var textEl = document.createElement('span');
    textEl.className = 'ludo-chat-msg-text';
    textEl.textContent = text;
    msg.appendChild(textEl);

    messagesEl.appendChild(msg);
    messagesEl.scrollTop = messagesEl.scrollHeight;
  }

  function loadMessages(chatArray) {
    if (!messagesEl) return;
    messagesEl.innerHTML = '';
    chatArray.forEach(function(m) {
      addMessage(m.sender_name, m.text, false);
    });
  }

  function addSystemMessage(text) {
    addMessage(null, text, true);
  }

  return {
    init: init,
    addMessage: addMessage,
    addSystemMessage: addSystemMessage,
    loadMessages: loadMessages
  };
})();
