#ifndef DOCUMENT_H
#define DOCUMENT_H

#include <QObject>
#include <QJsonObject>

class DesignerScene;
class QGraphicsItem;
class QUndoCommand;
class QUndoStack;

class Document : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool modified READ isModified NOTIFY modifiedChanged)

public:
    explicit Document(QObject *parent = nullptr);
    ~Document();

    // Serialization
    bool save();
    bool saveAs(const QString &filePath);
    bool load(const QString &filePath);

    // State
    bool isModified() const;
    QString filePath() const;

    // Unified command entry point
    void execute(QUndoCommand *cmd);

    // Accessors (read-only, no ownership transfer)
    DesignerScene *scene() const;
    QUndoStack *undoStack() const;

    // Reset to empty document
    void clear();

    // Called by selectors to mark dirty for non-undo operations
    // (move, scale, rotate, edit-shape)
    void markDirty();

signals:
    void modifiedChanged(bool modified);
    void filePathChanged(const QString &filePath);
    void saved();
    void loaded();

private:
    void setModified(bool modified);
    void setupNewScene();

    DesignerScene *m_pScene;
    QUndoStack    *m_pUndoStack;
    bool           m_bModified;
    int            m_nSavedIndex;
    QString        m_sFilePath;
};

#endif // DOCUMENT_H
