// resource_pack.hpp
// Resource pack file format and management for Projecte 2026

#pragma once

#include <array>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>

namespace Resource {

    // Entry metadata for each resource in the pack
    struct ResourceEntry {
        std::string filename;  // Relative path within pack
        uint64_t offset{0};    // Byte offset in data block
        uint64_t size{0};      // Size in bytes
        uint32_t checksum{0};  // CRC32 checksum for verification
    };

    // Resource pack file format
    // Header: "P26R" (4 bytes) + Version (4 bytes)
    // Metadata: Count + array of ResourceEntry
    // Data: Encrypted data block
    class Pack {
       public:
        Pack() = default;
        ~Pack() = default;

        Pack(const Pack&) = delete;  // Deleted copy/move constructors
        auto operator=(const Pack&) -> Pack& = delete;
        Pack(Pack&&) = delete;
        auto operator=(Pack&&) -> Pack& = delete;

        auto addFile(const std::string& filepath, const std::string& pack_name) -> bool;  // Building packs
        auto addDirectory(const std::string& dir_path, const std::string& base_path = "") -> bool;

        auto savePack(const std::string& pack_file) -> bool;  // Pack I/O
        auto loadPack(const std::string& pack_file) -> bool;

        auto getResource(const std::string& filename) -> std::vector<uint8_t>;  // Resource access
        [[nodiscard]] auto hasResource(const std::string& filename) const -> bool;
        [[nodiscard]] auto getResourceList() const -> std::vector<std::string>;

        [[nodiscard]] auto isLoaded() const -> bool { return loaded_; }  // Status queries
        [[nodiscard]] auto getResourceCount() const -> size_t { return resources_.size(); }
        [[nodiscard]] auto getDataSize() const -> size_t { return data_.size(); }
        [[nodiscard]] auto calculatePackChecksum() const -> uint32_t;  // Validation

       private:
        static constexpr std::array<char, 4> MAGIC_HEADER = {'P', '2', '6', 'R'};  // Pack format constants
        static constexpr uint32_t VERSION = 2;
        static constexpr const char* DEFAULT_ENCRYPT_KEY = "P26_RESOURCES_2026";

        static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t;  // Utility methods

        static void encryptData(std::vector<uint8_t>& data, const std::string& key);  // Encryption/decryption
        static void decryptData(std::vector<uint8_t>& data, const std::string& key);

        static auto readFile(const std::string& filepath) -> std::vector<uint8_t>;  // File I/O

        std::unordered_map<std::string, ResourceEntry> resources_;  // Member variables
        std::vector<uint8_t> data_;                                 // Encrypted data block
        bool loaded_{false};
    };

}  // namespace Resource
