from __future__ import annotations
from datetime import date
from pathlib import Path
from PyQt6.QtWidgets import (
    QDialog, QVBoxLayout, QFormLayout, QHBoxLayout,
    QLineEdit, QComboBox, QLabel, QPushButton, QMessageBox,
)
from core.models import ARTICLE_TYPES

STUB_TEMPLATE = """\
+++
title = "{title}"
date = {date}
type = "{type}"
draft = true
excerpt = ""
tags = []
categories = []
+++

TODO: scrivi il contenuto qui.
"""

class NewArticleDialog(QDialog):
    def __init__(self, blog_root: Path, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Nuovo articolo")
        self.setMinimumWidth(400)
        self._blog_root = blog_root
        self._created_path: Path | None = None
        self._build_ui()

    @property
    def created_path(self) -> Path | None:
        return self._created_path

    def _build_ui(self):
        layout = QVBoxLayout(self)
        form = QFormLayout()

        self._slug = QLineEdit()
        self._slug.setPlaceholderText("mio-articolo-2026")
        form.addRow("Slug:", self._slug)

        self._title = QLineEdit()
        self._title.setPlaceholderText("Il mio articolo")
        form.addRow("Titolo:", self._title)

        self._lang = QComboBox()
        self._lang.addItems(["it", "en"])
        form.addRow("Lingua:", self._lang)

        self._type = QComboBox()
        self._type.addItems(ARTICLE_TYPES)
        form.addRow("Tipo:", self._type)

        layout.addLayout(form)

        btns = QHBoxLayout()
        create_btn = QPushButton("Crea")
        create_btn.clicked.connect(self._create)
        cancel_btn = QPushButton("Annulla")
        cancel_btn.clicked.connect(self.reject)
        btns.addStretch()
        btns.addWidget(cancel_btn)
        btns.addWidget(create_btn)
        layout.addLayout(btns)

    def _create(self):
        slug = self._slug.text().strip()
        title = self._title.text().strip()
        lang = self._lang.currentText()
        article_type = self._type.currentText()

        if not slug or not title:
            QMessageBox.warning(self, "Errore", "Slug e titolo sono obbligatori.")
            return

        target = self._blog_root / "content" / lang / "articles" / slug
        if target.exists():
            QMessageBox.warning(self, "Errore", f"Esiste già: {target}")
            return

        target.mkdir(parents=True)
        index_md = target / "index.md"
        index_md.write_text(STUB_TEMPLATE.format(
            title=title,
            date=date.today().isoformat(),
            type=article_type,
        ), encoding="utf-8")

        self._created_path = index_md
        self.accept()
