/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#pragma once

#include <ox/claw/read.hpp>
#include <ox/fs/fs.hpp>

#include "validation.hpp"

namespace keel {

constexpr auto K1HdrSz = 40;

ox::Result<ox::UUID> readUuidHeader(ox::BufferView const &buff) noexcept;

ox::Result<ox::UUID> regenerateUuidHeader(ox::Buffer &buff) noexcept;

constexpr ox::Error writeUuidHeader(ox::Writer_c auto &writer, ox::UUID const &uuid) noexcept {
	OX_RETURN_ERROR(write(writer, "K1;"));
	OX_RETURN_ERROR(uuid.toString(writer));
	return writer.put(';');
}

template<typename T>
constexpr ox::Result<T> readAsset(ox::BufferView buff) noexcept {
	auto const err = readUuidHeader(buff).error;
	if (!err) {
		buff += K1HdrSz; // the size of K1 headers
	}
	auto out = ox::readClaw<T>(buff);
	OX_RETURN_ERROR(out);
	OX_RETURN_ERROR(ensureValid(out.value));
	return out;
}

ox::Result<ox::ModelObject> readAsset(ox::TypeStore &ts, ox::BufferView const &buff) noexcept;

struct AssetHdr {
	ox::UUID uuid;
	ox::ClawHeader clawHdr;
};

ox::Result<ox::StringView> readAssetTypeId(ox::BufferView const &buff) noexcept;

ox::Result<AssetHdr> readAssetHeader(ox::BufferView const &buff) noexcept;

}
