#pragma once

#include "lite_wallet_server_selection_adapter.h"

#include "../config/settings.h"

#include <string>
#include <vector>

namespace dragonx {
namespace wallet {

enum class LiteWalletLifecycleUiExecutionStatus {
    ReadyForFutureLifecycleRequest,
    WaitingForLiteBuild,
    WaitingForBackendCapability,
    WaitingForSettings,
    WaitingForPersistedServerSelection,
    WaitingForServerSelection,
    WaitingForDisplayStatus,
    WaitingForLifecycleUi,
    WaitingForRequest,
    WaitingForPrivateDataRedaction,
    WaitingForSyncPlannerFeed,
    RuntimeExecutionDisabled
};

enum class LiteWalletLifecycleUiExecutionIssue {
    FullNodeBuild,
    LiteBackendCapabilityMissing,
    SettingsNotLoaded,
    PersistedServerSelectionIntentMissing,
    ServerSelectionMissing,
    SelectedServerDisplayMissing,
    LifecycleUiOwnerMissing,
    LifecycleOperationUnconfirmed,
    OpenWalletPathMissing,
    RestoreSeedMissing,
    PrivateDataRedactionMissing,
    SyncPlannerFeedMissing,
    ServerConnectivityCheckRequested,
    WalletExistsCheckRequested,
    WalletLifecycleExecutionRequested,
    SyncRequested,
    SyncStatusPollingRequested,
    WorkerQueueRequested,
    WalletStateMutationRequested,
    WalletFilePersistenceRequested,
    SendImportExportRequested,
    SettingsWriteRequested
};

struct LiteWalletLifecycleUiRequestIntent {
    bool requestProvided = false;
    LiteWalletLifecycleOperation operation = LiteWalletLifecycleOperation::CreateNew;
    LiteWalletCreateRequest createRequest;
    LiteWalletOpenRequest openRequest;
    LiteWalletRestoreRequest restoreRequest;
};

struct LiteWalletLifecycleUiExecutionInput {
    WalletCapabilities capabilities;
    LiteWalletLifecycleUiRequestIntent request;
    bool settingsLoaded = true;
    bool requirePersistedServerSelectionIntent = true;
    LiteWalletLifecycleUiPrerequisites ui;

    bool settingsWriteRequested = false;
    bool serverConnectivityCheckRequested = false;
    bool walletExistsCheckRequested = false;
    bool walletLifecycleExecutionRequested = false;
    bool syncStartRequested = false;
    bool syncStatusPollingRequested = false;
    bool workerQueueEnqueueRequested = false;
    bool walletStateMutationRequested = false;
    bool walletFilePersistenceRequested = false;
    bool sendImportExportExecutionRequested = false;
};

struct LiteWalletLifecycleUiExecutionIssueInfo {
    LiteWalletLifecycleUiExecutionIssue issue = LiteWalletLifecycleUiExecutionIssue::SettingsNotLoaded;
    std::string message;
};

struct LiteWalletLifecycleUiExecutionResult {
    bool ok = false;
    bool dryRunOnly = true;
    bool noNetwork = true;
    bool noBridgeCalls = true;
    bool noServerHealthChecked = true;
    bool noWalletExistsChecked = true;
    bool noWalletCreated = true;
    bool noWalletOpened = true;
    bool noWalletRestored = true;
    bool noSyncStarted = true;
    bool noSyncStatusPolled = true;
    bool noWalletPersistence = true;
    bool noSettingsPersistence = true;
    bool noSendImportExportExecution = true;
    bool settingsWritten = false;
    bool workerQueueEnqueued = false;
    bool stateMutationAllowed = false;
    bool stateMutated = false;
    bool walletStateWritten = false;
    bool selectedServerSettingsLoaded = false;
    bool selectedServerIntentAccepted = false;
    bool lifecycleReadinessAccepted = false;
    bool requestAccepted = false;
    bool privateDataRedacted = false;
    bool lifecycleReportCouldFeedSyncPlanners = false;
    LiteWalletLifecycleUiExecutionStatus status = LiteWalletLifecycleUiExecutionStatus::WaitingForSettings;
    std::string error;
    std::string selectedServerUrlRedacted;
    std::string requestSummaryRedacted;
    std::string diagnosticSummary;
    LiteConnectionSettings connectionSettings;
    LiteServerSelectionResult selectedServer;
    LiteWalletServerSelectionUiExecutionResult serverSelectionReport;
    LiteWalletServerLifecycleReadinessInput lifecycleInput;
    LiteWalletServerLifecycleReadinessResult lifecycleReadiness;
    std::vector<LiteRedactedPrivateData> privateData;
    std::vector<LiteWalletLifecycleUiExecutionIssueInfo> issues;
};

const char* liteWalletLifecycleUiExecutionStatusName(
    LiteWalletLifecycleUiExecutionStatus status);
const char* liteWalletLifecycleUiExecutionIssueName(
    LiteWalletLifecycleUiExecutionIssue issue);

LiteWalletLifecycleUiExecutionResult executeLiteWalletLifecycleUiRequest(
    config::Settings& settings,
    const LiteWalletLifecycleUiExecutionInput& input);

class LiteWalletLifecycleUiExecutionAdapter {
public:
    explicit LiteWalletLifecycleUiExecutionAdapter(config::Settings& settings);

    LiteWalletLifecycleUiExecutionResult execute(
        const LiteWalletLifecycleUiExecutionInput& input) const;

private:
    config::Settings& settings_;
};

} // namespace wallet
} // namespace dragonx