<?php

namespace Shm\ShmUtils;

use Shm\Shm;
use Shm\ShmAdmin\SchemaCollections\ShmExportCollection;
use Shm\ShmCmd\Cmd;
use Shm\ShmDB\mDB;
use Shm\ShmTypes\StructureType;

class DisplayValuePrepare
{

    /**
     * Prepare display values for a list of items by ids
     * @param StructureType $structure
     * @param array $_ids
     * @return array
     * [
     *     '123' => 'Display Value',
     *     '456' => 'Display Value',
     * ]
     */
    public static function prepareByIdsKeyValues(StructureType $structure, array $_ids)
    {
        $displayValues = self::prepareByIds($structure, $_ids);
        $result = [];
        foreach ($displayValues as $item) {
            $result[$item['_id']] = $item['displayValue'];
        }
        return $result;
    }

    /**
     * Prepare display values for a list of items by ids
     * @param StructureType $structure
     * @param array $_ids
     * @return array
     * [
     *     [
     *        '_id' => '123',
     *        'displayValue' => 'Display Value',
     *    ],
     *    [
     *        '_id' => '456',
     *        'displayValue' => 'Display Value',
     *    ],
     * ]
     */
    public static function prepareByIds(StructureType $structure, array $_ids)
    {

        $items = mDB::collection($structure->collection)->find(['_id' => ['$in' => array_map(fn($id) => mDB::id($id), $_ids)]]);

        if (!$items) {
            return null;
        }

        return self::prepare($structure, $items);
    }


    public static function prepareById(StructureType $structure, $_id)
    {

        $item = mDB::collection($structure->collection)->findOne(['_id' => mDB::id($_id)]);

        if (!$item) {
            return null;
        }

        return self::prepare($structure, [$item])[0] ?? null;
    }

    /**
     * Prepare display values for a list of items
     * @param StructureType $structure
     * @param array $data
     * @return array
     * [
     *     '123' => 'Display Value',
     *     '456' => 'Display Value',
     * ]
     */
    public static function prepareKeyValues(StructureType $structure, array $data = [])
    {

        $displayValues = self::prepare($structure, $data);
        $result = [];
        foreach ($displayValues as $item) {
            $result[$item['_id']] = $item['displayValue'];
        }
        return $result;
    }
    /**
     * Prepare display values for a list of items
     * @param StructureType $structure
     * @param array $data
     * @return array
     * [
     *    [
     *        '_id' => '123',
     *        'displayValue' => 'Display Value',
     *    ],
     *    [
     *        '_id' => '456',
     *        'displayValue' => 'Display Value',
     *    ],
     * ]
     */
    public static function prepare(StructureType $structure, $data = [])
    {







        $bulkUpdate = [];

        $result = [];

        foreach ($data as $item) {

            $_displayValue = $item['_displayValue'] ?? null;
            $_displayValueUpdated_at = ShmInit::isDev() ? null : $item['_displayValueUpdated_at'] ?? null;


            $OneDay =  60 * 60 * 24;

            if ($_displayValue  && $_displayValueUpdated_at && $_displayValueUpdated_at > (time() - $OneDay)) {

                $result[] = [
                    '_id' => (string)$item['_id'],
                    'displayValue' => $item['_displayValue'],
                ];
                continue;
            }

            $displayValues = $structure->displayValues($item);

            if ($displayValues && is_string($displayValues)) {
                $displayValues = [$displayValues];
            }

            $value = null;
            if (is_array($displayValues) && count($displayValues) > 0) {
                $value = implode(', ', $displayValues);
            } else {


                $result[] = [
                    '_id' => (string)$item['_id'],
                    'displayValue' => (string)$item['_id'],
                ];

                continue;



                $displayValues = $structure->fallbackDisplayValues($item);


                if (is_array($displayValues) && count($displayValues) > 0) {
                    $value =  implode(', ', $displayValues);
                }
            }

            if ($value) {
                $bulkUpdate[] = [
                    'updateOne' => [
                        [
                            '_id' => $item['_id'],
                        ],
                        [
                            '$set' => [
                                '_displayValue' => $value,
                                '_displayValueUpdated_at' => time(),
                            ],
                        ],
                    ],
                ];
            }

            $result[] = [
                '_id' => (string)$item['_id'],
                'displayValue' => $value,
            ];
        }


        if (count($bulkUpdate) > 0) {
            mDB::_collection($structure->collection)->bulkWrite($bulkUpdate);
        }

        return $result;
    }
}
