<?php

namespace Shm\ShmSocial;

use Shm\ShmTypes\StructureType;

class ShmSocialsMigrate
{
    public static function migrate(StructureType $structure, string $socialArrayField)
    {

        $items = $structure->find([
            $socialArrayField => ['$exists' => true]
        ]);

        foreach ($items as $item) {
            $socials = $item[$socialArrayField];
            foreach ($socials as $social) {
                if (!$social['id']) {

                    echo "Social not found: " . $social['id'] . " for item: " . $item['_id'] . "\n";
                    continue;
                }
                ShmSocials::structure()->_updateOne([
                    'id' => $social['id']
                ], [
                    '$set' => [
                        ...$social,
                        'owner' => $item['_id'],
                        'ownerCollection' => $structure->collection,
                    ],
                ], [
                    'upsert' => true
                ]);
                echo "Migrated social: " . $social['id'] . " for item: " . $item['_id'] . "\n";
            }
        }
    }
}