from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
import tomlkit

DEFAULT_CONFIG_PATH = Path.home() / ".config" / "my-publisher" / "config.toml"

@dataclass
class Config:
    blog_repo: str = ""
    transart_script: str = "/home/danix/bin/transart.py"
    typora_bin: str = "typora"
    font_size: int = 10

    def is_complete(self) -> bool:
        return bool(self.blog_repo)

    def save(self, path: Path = DEFAULT_CONFIG_PATH) -> None:
        path.parent.mkdir(parents=True, exist_ok=True)
        doc = tomlkit.document()
        doc.add("blog_repo", self.blog_repo)
        doc.add("transart_script", self.transart_script)
        doc.add("typora_bin", self.typora_bin)
        doc.add("font_size", self.font_size)
        path.write_text(tomlkit.dumps(doc))

    @classmethod
    def load(cls, path: Path = DEFAULT_CONFIG_PATH) -> "Config":
        if not path.exists():
            return cls()
        data = tomlkit.loads(path.read_text())
        defaults = cls()
        return cls(
            blog_repo=str(data.get("blog_repo", defaults.blog_repo)),
            transart_script=str(data.get("transart_script", defaults.transart_script)),
            typora_bin=str(data.get("typora_bin", defaults.typora_bin)),
            font_size=int(data.get("font_size", defaults.font_size)),
        )
