#pragma once

#include "wallet_security_controller.h"

#include <chrono>
#include <cstdint>
#include <string>

namespace dragonx {
namespace services {

class WalletSecurityWorkflow {
public:
    enum class DecryptPhase {
        PassphraseEntry = 0,
        Working = 1,
        Success = 2,
        Error = 3
    };

    enum class DecryptStep {
        Unlock = 0,
        ExportKeys = 1,
        StopDaemon = 2,
        BackupWallet = 3,
        RestartDaemon = 4,
        ImportKeys = 5
    };

    struct DecryptSnapshot {
        DecryptPhase phase = DecryptPhase::PassphraseEntry;
        DecryptStep step = DecryptStep::Unlock;
        std::string status;
        bool inProgress = false;
        bool importActive = false;
        std::chrono::steady_clock::time_point stepStarted{};
        std::chrono::steady_clock::time_point overallStarted{};
    };

    struct WalletFilePlan {
        std::string dataDir;
        std::string exportFile;
        std::string exportPath;
        std::string walletPath;
        std::string backupPath;
    };

    void reset();
    void start(std::chrono::steady_clock::time_point now);
    void advanceTo(DecryptStep step, std::string status,
                   std::chrono::steady_clock::time_point now);
    void failEntry(std::string status);
    void fail(std::string status);
    void closeDialogForImport();
    void finishImport();

    DecryptSnapshot snapshot() const { return state_; }
    DecryptPhase phase() const { return state_.phase; }
    DecryptStep step() const { return state_.step; }
    const std::string& status() const { return state_.status; }
    bool inProgress() const { return state_.inProgress; }
    bool importActive() const { return state_.importActive; }
    bool canClose() const { return state_.phase != DecryptPhase::Working; }

    static WalletFilePlan planWalletFiles(const std::string& dataDir,
                                          std::uint64_t timestampSeconds);
    static const char* stepStatus(DecryptStep step);
    static const char* stepLabel(DecryptStep step);
    static int stepIndex(DecryptStep step) { return static_cast<int>(step); }
    static DecryptStep stepFromIndex(int step);
    static bool stepIsComplete(DecryptStep current, DecryptStep candidate);

private:
    DecryptSnapshot state_;
};

} // namespace services
} // namespace dragonx
