{% extends 'layouts/base.html' %}
{% load static i18n %}

{% block title %}{% trans "Assistente Virtual de IA" %}{% endblock title %}

{% block extrastyle %}
<link rel="stylesheet" href="{% static 'css/chatbot.css' %}">
{% endblock extrastyle %}

{% block extrahead %}
<style>
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@600&display=swap');

  .chatbot-container {
    background: radial-gradient(ellipse at center, #1a1a2e 0%, #121220 100%);
    min-height: 100vh;
    padding: 2rem 0;
  }

  .chatbot-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(15px);
    border-radius: 20px;
    box-shadow: 0 0 25px rgba(111, 66, 193, 0.3), 0 0 40px rgba(13, 202, 240, 0.2);
    border: 2px solid transparent;
    background-clip: padding-box;
    position: relative;
    overflow: hidden;
    height: calc(100vh - 200px);
    max-height: 800px;
    display: flex;
    flex-direction: column;
  }

  .chatbot-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #6f42c1, #e83e8c, #0dcaf0);
    border-radius: 20px;
    padding: 2px;
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask-composite: exclude;
    z-index: -1;
  }

  .chatbot-header {
    background: linear-gradient(135deg, #6f42c1, #e83e8c);
    color: white;
    padding: 1.5rem 2rem;
    position: relative;
    overflow: hidden;
  }

  .chatbot-header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
  }

  .chatbot-header-content {
    position: relative;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .chatbot-header h2 {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.8rem;
    font-weight: 700;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  }

  .chatbot-header p {
    font-size: 0.9rem;
    margin: 0.5rem 0 0 0;
    opacity: 0.9;
  }

  .status-indicator {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    padding: 0.25rem 0.75rem;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    backdrop-filter: blur(10px);
  }

  .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #4ade80;
    animation: pulse 2s infinite;
  }

  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
  }

  .chatbot-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .message {
    display: flex;
    gap: 0.75rem;
    animation: fadeInUp 0.3s ease-out;
  }

  @keyframes fadeInUp {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .message.user {
    flex-direction: row-reverse;
  }

  .message-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    flex-shrink: 0;
  }

  .message.user .message-avatar {
    background: linear-gradient(135deg, #6f42c1, #e83e8c);
    color: white;
  }

  .message.assistant .message-avatar {
    background: linear-gradient(135deg, #0dcaf0, #6f42c1);
    color: white;
  }

  .message-content {
    max-width: 70%;
    padding: 1rem 1.25rem;
    border-radius: 18px;
    position: relative;
    word-wrap: break-word;
  }

  .message.user .message-content {
    background: linear-gradient(135deg, #6f42c1, #e83e8c);
    color: white;
    border-bottom-right-radius: 4px;
  }

  .message.assistant .message-content {
    background: #f0f0f0;
    color: #333;
    border-bottom-left-radius: 4px;
  }

  .message-time {
    font-size: 0.75rem;
    opacity: 0.6;
    margin-top: 0.25rem;
  }

  .typing-indicator {
    display: flex;
    gap: 0.5rem;
    padding: 1rem 1.25rem;
    background: #f0f0f0;
    border-radius: 18px;
    width: fit-content;
  }

  .typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #999;
    animation: typing 1.4s infinite;
  }

  .typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
  }

  .typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
  }

  @keyframes typing {
    0%, 60%, 100% {
      transform: translateY(0);
    }
    30% {
      transform: translateY(-10px);
    }
  }

  .chatbot-input-area {
    padding: 1.5rem;
    border-top: 2px solid #e9ecef;
    background: #f8f9fa;
  }

  .input-wrapper {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
  }

  .message-input {
    flex: 1;
    min-height: 50px;
    max-height: 150px;
    padding: 0.75rem 1rem;
    border: 2px solid #6f42c1;
    border-radius: 25px;
    font-size: 1rem;
    resize: none;
    transition: all 0.3s ease;
  }

  .message-input:focus {
    outline: none;
    border-color: #0dcaf0;
    box-shadow: 0 0 10px rgba(13, 202, 240, 0.3);
  }

  .send-button {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6f42c1, #e83e8c);
    border: none;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .send-button:hover:not(:disabled) {
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(111, 66, 193, 0.5);
  }

  .send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .solicitation-suggestion {
    margin-top: 1rem;
    padding: 1rem;
    background: linear-gradient(135deg, #fff3cd, #ffe69c);
    border-left: 4px solid #ffc107;
    border-radius: 8px;
    display: none;
  }

  .solicitation-suggestion.show {
    display: block;
    animation: slideDown 0.3s ease-out;
  }

  @keyframes slideDown {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .solicitation-suggestion h5 {
    margin: 0 0 0.5rem 0;
    color: #856404;
  }

  .solicitation-suggestion p {
    margin: 0 0 1rem 0;
    color: #856404;
  }

  .btn-create-solicitation {
    background: #ffc107;
    border: none;
    color: #333;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  .btn-create-solicitation:hover {
    background: #ffb300;
    transform: translateY(-2px);
  }
</style>
{% endblock extrahead %}

{% block content %}
{% comment %}Desabilitar notificações Django - chatbot usa WebSocket{% endcomment %}
<script>
  // Desabilitar exibição automática de mensagens Django nesta página
  (function() {
    // Esconder notificações Django imediatamente quando a página carregar
    setTimeout(function() {
      const notificationsContainer = document.getElementById('pdl-notifications-container');
      if (notificationsContainer) {
        const djangoNotifications = notificationsContainer.querySelectorAll('.pdl-notification[data-message-id]');
        djangoNotifications.forEach(function(notif) {
          notif.style.display = 'none';
          notif.remove();
        });
      }
    }, 100);
  })();
</script>

<div class="chatbot-container">
  <div class="container">
    <div class="chatbot-card">
      <!-- Header -->
      <div class="chatbot-header">
        <div class="chatbot-header-content">
          <div>
            <h2><i class="fas fa-robot me-2"></i>{% trans "Assistente Virtual" %}</h2>
            <p>{% trans "Como posso ajudá-lo hoje?" %}</p>
          </div>
          <div style="display: flex; align-items: center; gap: 1rem;">
            <a href="{% url 'ai_assistant:chatbot_sessions' %}" style="color: white; text-decoration: none; font-size: 0.9rem; opacity: 0.9;">
              <i class="fas fa-history me-1"></i>{% trans "Histórico" %}
            </a>
            <div class="status-indicator">
              <span class="status-dot"></span>
              <span id="connectionStatus">{% trans "Conectado" %}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- Messages Area -->
      <div class="chatbot-messages" id="messagesContainer">
        <!-- Mensagens serão inseridas aqui via JavaScript -->
      </div>

      <!-- Solicitation Suggestion -->
      <div class="solicitation-suggestion" id="solicitationSuggestion">
        <h5><i class="fas fa-lightbulb me-2"></i>{% trans "Sugestão de Solicitação" %}</h5>
        <p id="solicitationMessage"></p>
        <button class="btn-create-solicitation" id="btnCreateSolicitation">
          <i class="fas fa-ticket-alt me-2"></i>{% trans "Criar Solicitação" %}
        </button>
      </div>

      <!-- Input Area -->
      <div class="chatbot-input-area">
        <div class="input-wrapper">
          <textarea 
            id="messageInput" 
            class="message-input" 
            placeholder="{% trans 'Digite sua mensagem...' %}"
            rows="1"
          ></textarea>
          <button id="sendButton" class="send-button">
            <i class="fas fa-paper-plane"></i>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock content %}

{% block extra_js %}
<script src="{% static 'js/chatbot.js' %}"></script>
{% endblock extra_js %}
