from PyQt5 import QtWidgets, QtCore
import requests
from datetime import datetime
from .api import APIGetOrderAllMessaging, APIGetOrderLinkedMessaging, APIPostOrderLinkedMessaging


class ChatMessageWidget(QtWidgets.QWidget):
    """Einzelne Chatnachricht"""
    def __init__(self, activity_item: dict, current_company_id: str, parent=None):
        super().__init__(parent)
        self.activity_item = activity_item
        self.current_company_id = current_company_id
        self._build_ui()

    def _build_ui(self):
        layout = QtWidgets.QVBoxLayout(self)


        created = self._format_datetime(self.activity_item.get("createdAt"))
        msg = self.activity_item.get("message", "")
        username = self.activity_item.get("username", "-")
        company = self.activity_item.get("companyname", "-")

        link_type = self.activity_item.get("linkType")

        container = QtWidgets.QWidget()
        inner = QtWidgets.QVBoxLayout(container)
        inner.setContentsMargins(5, 5, 5, 5)

 

        if link_type in ["progressUpdate", "progressUpdateStageBack"]:
            meta = QtWidgets.QLabel(f"Update – {created}")
            meta.setStyleSheet("font-size: 11px; color: #6b7280; border-radius:8px;")
            meta.setAlignment(QtCore.Qt.AlignCenter)
            text = QtWidgets.QLabel(msg)
            text.setWordWrap(True)
            text.setAlignment(QtCore.Qt.AlignCenter)
            text.setStyleSheet("color: #374151;")
            inner.addWidget(meta)
            inner.addWidget(text)
            container.setStyleSheet("background-color: #e5e7eb; border-radius:8px;")
        elif link_type == "progressUpdateCompleted":
            meta = QtWidgets.QLabel(f"Auftrag abgeschlossen – {created}")
            meta.setStyleSheet("font-size: 11px; color: #6b7280; border-radius:8px; border-radius:8px;")
            meta.setAlignment(QtCore.Qt.AlignCenter)
            text = QtWidgets.QLabel(msg)
            text.setWordWrap(True)
            text.setAlignment(QtCore.Qt.AlignCenter)
            text.setStyleSheet("color: #111827; font-weight: bold; border-radius:8px;")
            inner.addWidget(meta)
            inner.addWidget(text)
            container.setStyleSheet("background-color: #d1d5db; border-radius:8px;")
        else:
            # Normale Nachrichten
            meta = QtWidgets.QLabel(f"{username} ({company}) – {created}")
            meta.setStyleSheet("font-size: 11px; color: gray; border-radius:8px;")
            text = QtWidgets.QLabel(msg)
            text.setWordWrap(True)
            inner.addWidget(meta)
            inner.addWidget(text)
            if self.activity_item.get("createdByCompany") == self.current_company_id:
                inner.setAlignment(meta, QtCore.Qt.AlignRight)
                inner.setAlignment(text, QtCore.Qt.AlignRight)
                container.setStyleSheet("background-color: #f3f4f6; border-radius:8px;")
            else:
                inner.setAlignment(meta, QtCore.Qt.AlignLeft)
                inner.setAlignment(text, QtCore.Qt.AlignLeft)
                container.setStyleSheet("background-color: #ffffff; border-radius:8px;")

        layout.addWidget(container)

    def _format_datetime(self, dt_str):
        if not dt_str:
            return "-"
        import re
        try:
            # Numerisch (int, float oder numerischer String wie "1706000000000")
            raw = dt_str
            if isinstance(raw, str):
                raw = raw.strip()
            if isinstance(raw, (int, float)) or (isinstance(raw, str) and re.fullmatch(r'\d+\.?\d*', raw)):
                ts = float(raw)
                ts = ts / 1000 if ts > 1e12 else ts
                return datetime.fromtimestamp(ts).strftime("%d.%m.%Y %H:%M")
            s = str(dt_str).replace("Z", "+00:00")
            # Überlange Mikrosekunden kürzen (fromisoformat verträgt max 6 Stellen)
            s = re.sub(r'(\.\d{6})\d+', r'\1', s)
            dt = datetime.fromisoformat(s)
            return dt.strftime("%d.%m.%Y %H:%M")
        except Exception:
            return str(dt_str)


class MessageInputWidget(QtWidgets.QWidget):
    """Eingabefeld für neue Nachrichten"""
    sendMessage = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._build_ui()

    def _build_ui(self):
        layout = QtWidgets.QHBoxLayout(self)
        self.text_edit = QtWidgets.QTextEdit()
        self.text_edit.setFixedHeight(60)
        self.text_edit.setStyleSheet("border: 1px solid #ccc; border-radius:8px;")

        send_btn = QtWidgets.QPushButton("Senden")
        send_btn.setStyleSheet("""
            QPushButton {
                cursor:pointer; 
                border: 1px solid #000; 
                padding: 8px 24px; 
                margin:2px; 
                font-size:12px;
                height:24px;
                border-radius:6px;
            }
            QPushButton:hover {
                background-color: #000;
                color: white;
                border: 1px solid #000;
            }
            QPushButton:pressed {
                background-color: #333;
                color: white;
            }""")
        send_btn.clicked.connect(self._on_send)

        layout.addWidget(self.text_edit)
        layout.addWidget(send_btn)

    def _on_send(self):
        text = self.text_edit.toPlainText().strip()
        if text:
            self.sendMessage.emit(text)
            self.text_edit.clear()


class MessagingWidget(QtWidgets.QWidget):
    """Haupt-Nachrichtenbereich"""
    def __init__(self, cognito_session, order_id, link_id=None, link_type=None,
                 company_id=None, stage=None, parent=None):
        super().__init__(parent)
        self.cognito_session = cognito_session
        self.order_id = order_id
        self.link_id = link_id
        self.link_type = link_type
        self.company_id = company_id
        self.stage = stage
        self.activities = []

        self._build_ui()
        self._load_messages()

    def _build_ui(self):
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)

        title = QtWidgets.QLabel("Nachrichtenbereich")
        title.setStyleSheet("font-weight: normal;margin-top:10px;  padding:0px; font-size: 16px;")
        layout.addWidget(title)
        
        
        # Eingabefeld nur, wenn Auftrag nicht abgeschlossen
        if self.stage != "completed":
            self.message_input = MessageInputWidget()
            self.message_input.sendMessage.connect(self._save_message)
            self.message_input.setFixedHeight(80)
            layout.addWidget(self.message_input)

        # Scrollbereich für Nachrichten
        self.scroll_area = QtWidgets.QScrollArea()
        self.scroll_area.setWidgetResizable(True)
        self.messages_container = QtWidgets.QWidget()
        self.messages_layout = QtWidgets.QVBoxLayout(self.messages_container)
        self.messages_layout.setContentsMargins(0, 0, 0, 0)
        self.scroll_area.setWidget(self.messages_container)
        self.scroll_area.setStyleSheet("background-color: white; border: none;")
        layout.addWidget(self.scroll_area)


    def _load_messages(self):
        try:
            if self.link_type == "all":
                fetched = APIGetOrderAllMessaging(self.cognito_session, self.order_id)
            else:
                fetched = APIGetOrderLinkedMessaging(
                    self.cognito_session,
                    self.order_id,
                    self.link_id,
                    self.link_type
                )
            self.activities = fetched or []
            self._render_messages()
        except Exception as e:
            QtWidgets.QMessageBox.critical(self, "Fehler", f"Nachrichten konnten nicht geladen werden:\n{e}")

    def _render_messages(self):
        # Alte Widgets (außer Stretch) löschen
        for i in reversed(range(self.messages_layout.count())):
            item = self.messages_layout.itemAt(i)
            if item and item.widget():
                item.widget().deleteLater()

        # Nachrichten in chronologischer Reihenfolge einfügen
        for activity in self.activities:
            msg_widget = ChatMessageWidget(activity, self.company_id)
            msg_widget.setMinimumHeight(60)
            self.messages_layout.addWidget(msg_widget)



        # Automatisch nach unten scrollen
        QtCore.QTimer.singleShot(0, lambda: self.scroll_area.verticalScrollBar().setValue(
            self.scroll_area.verticalScrollBar().maximum()
        ))

    def _save_message(self, text):
        try:
            message_data = {
                "ordersId": self.order_id,
                "linkId": self.link_id,
                "linkType": self.link_type,
                "message": text,
            }
            response = APIPostOrderLinkedMessaging(self.cognito_session, message_data, company_id=self.company_id)
            if response:
                self.activities.insert(0, response)
                self._render_messages()
        except Exception as e:
            QtWidgets.QMessageBox.critical(self, "Fehler", f"Nachricht konnte nicht gesendet werden:\n{e}")
