#include "seed.hpp"
#include <cstddef>
#include <cstdint>
#include <format>
#include <span>
#include <string>
#include <vector>
#include <Magick++.h>
namespace overseer::test
{
namespace
{
namespace inv = ::overseer::inventory;
// Build a 4x4 solid-color PNG so `InventoryRepo::putAttachment` has
// real bytes to run through ImageMagick.
std::vector<unsigned char> tinyPng(const std::string& color)
{
Magick::Image img(Magick::Geometry(4, 4), Magick::Color(color));
img.magick("PNG");
Magick::Blob blob;
img.write(&blob);
const auto* p = static_cast<const unsigned char*>(blob.data());
return std::vector<unsigned char>(p, p + blob.length());
}
} // namespace
SeededFamily seedFamily(inv::InventoryRepo& repo)
{
SeededFamily out;
inv::Storage apt;
apt.name = "Apartment";
out.apartment_id = *repo.createStorage(apt);
inv::Storage room;
room.name = "Room";
room.parent_id = out.apartment_id;
out.room_id = *repo.createStorage(room);
inv::Storage shelf;
shelf.name = "Shelf";
shelf.parent_id = out.room_id;
out.shelf_id = *repo.createStorage(shelf);
// 5 attachments.
const std::array<const char*, 5> colors{
"red", "green", "blue", "yellow", "gray"};
for(size_t i = 0; i < colors.size(); ++i)
{
auto bytes = tinyPng(colors[i]);
std::span<const unsigned char> sp(bytes);
out.attachment_ids[i] = *repo.putAttachment(sp, "image/png");
}
// 20 stuffs spread across the three storages. First five get an
// attachment.
const std::array<int64_t, 3> storage_cycle{
out.apartment_id, out.room_id, out.shelf_id};
out.stuff_ids.reserve(20);
for(int i = 0; i < 20; ++i)
{
inv::Stuff s;
s.name = std::format("Stuff {:02d}", i);
s.storage_id = storage_cycle[static_cast<size_t>(i) % 3];
if(i < 5)
{
s.attachment_id = out.attachment_ids[static_cast<size_t>(i)];
}
out.stuff_ids.push_back(*repo.createStuff(s));
}
return out;
}
} // namespace overseer::test