from __future__ import annotations
from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QTabWidget,
    QListWidget, QListWidgetItem, QLabel,
)
from PyQt6.QtCore import Qt, pyqtSignal, QSize
from PyQt6.QtGui import QColor
from core.models import Article

class ArticleItem(QListWidgetItem):
    def __init__(self, article: Article):
        super().__init__()
        self.article = article
        if article.has_translation:
            badge = "🇬🇧 ✓" if article.lang == "it" else "🇮🇹 ✓"
        else:
            badge = "🇬🇧 ✗" if article.lang == "it" else "🇮🇹 ✗"
        line1 = f"{article.slug}  [{badge}]"
        if article.draft:
            line1 = f"[DRAFT]  {line1}"

        parts = []
        if article.meta_type:
            parts.append(article.meta_type)
        if article.meta_date:
            parts.append(article.meta_date)
        if article.meta_tags:
            parts.append(f"#{article.meta_tags.replace(', ', '  #')}")
        if article.meta_categories:
            parts.append(f"[{article.meta_categories}]")
        line2 = "  ".join(parts) if parts else ""

        text = f"{line1}\n{line2}" if line2 else line1
        self.setText(text)
        self.setSizeHint(QSize(0, 42) if line2 else QSize(0, 26))

        if article.draft:
            self.setForeground(QColor("#f59e0b"))
        elif not article.has_translation:
            self.setForeground(QColor("#ff6b6b"))

class ArticlesView(QWidget):
    article_selected = pyqtSignal(object)  # Article
    translate_requested = pyqtSignal(object)  # Article

    def __init__(self, parent=None):
        super().__init__(parent)
        self._articles: list[Article] = []
        self._build_ui()

    def _build_ui(self):
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        self._tabs = QTabWidget()
        self._tabs.setStyleSheet("QTabBar::tab { padding: 6px 16px; }")
        self._list_it = QListWidget()
        self._list_it.setWordWrap(True)
        self._list_en = QListWidget()
        self._list_en.setWordWrap(True)
        self._tabs.addTab(self._list_it, "🇮🇹 Italiano")
        self._tabs.addTab(self._list_en, "🇬🇧 English")
        layout.addWidget(self._tabs)
        self._list_it.itemClicked.connect(lambda item: self.article_selected.emit(item.article))
        self._list_en.itemClicked.connect(lambda item: self.article_selected.emit(item.article))

    def set_articles(self, articles: list[Article]):
        self._articles = articles
        self._list_it.clear()
        self._list_en.clear()
        for a in articles:
            item = ArticleItem(a)
            if a.lang == "it":
                self._list_it.addItem(item)
            else:
                self._list_en.addItem(item)

class MissingTranslationView(QWidget):
    article_selected = pyqtSignal(object)  # Article
    translate_requested = pyqtSignal(object)  # Article

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

    def _build_ui(self):
        layout = QVBoxLayout(self)
        header = QLabel("⚠️ Articoli senza traduzione")
        header.setStyleSheet("color: #ff6b6b; font-weight: bold; font-size: 13px; padding: 10px;")
        layout.addWidget(header)
        self._list = QListWidget()
        self._list.setWordWrap(True)
        layout.addWidget(self._list)
        self._list.itemClicked.connect(lambda item: self.article_selected.emit(item.article))

    def set_articles(self, articles: list[Article]):
        self._list.clear()
        missing = [a for a in articles if not a.has_translation]
        for a in missing:
            lang_other = "🇬🇧" if a.lang == "it" else "🇮🇹"
            item = ArticleItem(a)
            item.setText(f"{a.slug}  [manca {lang_other}]  ({a.lang.upper()})")
            item.setSizeHint(QSize(0, 26))
            self._list.addItem(item)
