/*
 * SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#pragma once

#include "linglong/api/types/v1/BuilderProject.hpp"
#include "linglong/api/types/v1/ContainerProcessStateInfo.hpp"
#include "linglong/api/types/v1/ExtensionDefine.hpp"
#include "linglong/oci-cfg-generators/container_cfg_builder.h"
#include "linglong/repo/ostree_repo.h"
#include "linglong/runtime/security_context.h"
#include "linglong/utils/error/error.h"

#include <filesystem>
#include <list>
#include <optional>
#include <string>
#include <vector>

namespace linglong::runtime {

class RunContext;

class RuntimeLayer
{
public:
    RuntimeLayer(package::Reference ref, RunContext &context);
    ~RuntimeLayer();

    using ExtensionRuntimeLayerInfo =
      std::pair<api::types::v1::ExtensionDefine, std::reference_wrapper<RuntimeLayer>>;

    utils::error::Result<void>
    resolveLayer(const std::vector<std::string> &modules = {},
                 const std::optional<std::string> &subRef = std::nullopt);

    utils::error::Result<api::types::v1::RepositoryCacheLayersItem> getCachedItem();

    const package::Reference &getReference() const { return reference; }

    const std::optional<package::LayerDir> &getLayerDir() const { return layerDir; }

    void setExtensionInfo(ExtensionRuntimeLayerInfo info) { extensionOf = info; }

    const std::optional<ExtensionRuntimeLayerInfo> &getExtensionInfo() const { return extensionOf; }

private:
    package::Reference reference;
    std::reference_wrapper<RunContext> runContext;
    std::optional<package::LayerDir> layerDir;
    std::optional<api::types::v1::RepositoryCacheLayersItem> cachedItem;
    bool temporary;
    std::optional<ExtensionRuntimeLayerInfo> extensionOf;
};

struct ResolveOptions
{
    bool depsBinaryOnly{ false };
    std::optional<std::vector<std::string>> appModules;
    std::optional<std::string> baseRef;
    std::optional<std::string> runtimeRef;
    std::optional<std::vector<std::string>> extensionRefs;
};

class RunContext
{
public:
    struct FilesystemPolicy
    {
        bool allowListConfigured{ false };
        std::vector<ocppi::runtime::config::types::Mount> allowList;
        std::vector<ocppi::runtime::config::types::Mount> extra;
    };

    RunContext(repo::OSTreeRepo &r)
        : repo(r)
    {
    }

    ~RunContext();

    utils::error::Result<void> resolve(const linglong::package::Reference &runnable,
                                       const ResolveOptions &opts = ResolveOptions{});

    utils::error::Result<void> resolve(const api::types::v1::BuilderProject &target,
                                       const std::filesystem::path &buildOutput);

    utils::error::Result<void> fillContextCfg(generator::ContainerCfgBuilder &builder);
    api::types::v1::ContainerProcessStateInfo stateInfo();

    repo::OSTreeRepo &getRepo() const { return repo; }

    std::string currentAppId() const;
    const FilesystemPolicy &filesystemPolicy() const;

    const std::string &getContainerId() const { return containerID; }

    const std::optional<RuntimeLayer> &getBaseLayer() const { return baseLayer; }

    const std::optional<RuntimeLayer> &getRuntimeLayer() const { return runtimeLayer; }

    void enableSecurityContext(const std::vector<SecurityContextType> &ctxs);

    const std::optional<RuntimeLayer> &getAppLayer() const { return appLayer; }

    utils::error::Result<std::filesystem::path> getBaseLayerPath() const;
    utils::error::Result<std::filesystem::path> getRuntimeLayerPath() const;

    utils::error::Result<api::types::v1::RepositoryCacheLayersItem> getCachedAppItem();

    bool hasRuntime() const { return !!runtimeLayer; }

private:
    utils::error::Result<void> resolveLayer(bool depsBinaryOnly,
                                            const std::vector<std::string> &appModules);
    utils::error::Result<void> resolveExtension(RuntimeLayer &layer);
    utils::error::Result<void>
    resolveExtension(const std::vector<api::types::v1::ExtensionDefine> &extDefs,
                     std::optional<std::string> channel = std::nullopt,
                     bool skipOnNotFound = false);
    utils::error::Result<void> fillExtraAppMounts(generator::ContainerCfgBuilder &builder);
    void detectDisplaySystem(generator::ContainerCfgBuilder &builder) noexcept;
    utils::error::Result<std::vector<api::types::v1::ExtensionDefine>>
    makeManualExtensionDefine(const std::vector<std::string> &refs);

    repo::OSTreeRepo &repo;
    std::unordered_map<SecurityContextType, std::unique_ptr<SecurityContext>> securityContexts;
    std::optional<RuntimeLayer> baseLayer;
    std::optional<RuntimeLayer> runtimeLayer;
    std::optional<RuntimeLayer> appLayer;
    std::list<RuntimeLayer> extensionLayers;

    std::string targetId;
    std::optional<std::filesystem::path> appOutput;
    std::optional<std::filesystem::path> runtimeOutput;
    std::optional<std::filesystem::path> extensionOutput;

    std::string containerID;
    std::filesystem::path bundle;
    std::map<std::string, std::string> environment;
    mutable std::optional<FilesystemPolicy> filesystemPolicyCache;
};

} // namespace linglong::runtime
