/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#pragma once

#include <unordered_map>
#include <shared_mutex>
#include <string>
#include <vector>
#include <cstring>
#include <uuidp.h>

namespace authdb {

    class AuthBackend;

    struct IndexEntry {
        size_t     offset;
        uuid::uuid uid;
        int        type;
    };

    class RecordIndex {
    public:
        RecordIndex();

        void build(AuthBackend &backend);

        void addEntry(const uuid::uuid &uid, int type, const char *name, size_t offset);
        void removeEntry(const uuid::uuid &uid, int type);

        bool findByName(int type, const char *fieldname, const std::string &name, uuid::uuid &out_uid) const;
        bool findByUuid(int type, const uuid::uuid &uid) const;
        size_t findOffset(const uuid::uuid &uid, int type) const;
        void listByType(int type, std::vector<uuid::uuid> &out) const;

        void invalidate();
        bool valid() const;

        size_t revision() const;

    private:
        struct NameKey {
            int         type;
            std::string name;

            bool operator==(const NameKey &o) const {
                return type == o.type && name == o.name;
            }
        };

        struct NameKeyHash {
            size_t operator()(const NameKey &k) const noexcept {
                size_t h = std::hash<int>()(k.type);
                h ^= std::hash<std::string>()(k.name) + 0x9e3779b9 + (h << 6) + (h >> 2);
                return h;
            }
        };

        struct UuidTypeKey {
            uuid::uuid uid;
            int        type;

            bool operator==(const UuidTypeKey &o) const {
                return type == o.type && memcmp(uid.value, o.uid.value, 16) == 0;
            }
        };

        struct UuidTypeKeyHash {
            size_t operator()(const UuidTypeKey &k) const noexcept {
                size_t h = 0;
                for (int i = 0; i < 16; ++i)
                    h = h * 131 + k.uid.value[i];
                h ^= std::hash<int>()(k.type) + 0x9e3779b9 + (h << 6) + (h >> 2);
                return h;
            }
        };

        mutable std::shared_mutex _mutex;
        std::unordered_map<NameKey, uuid::uuid, NameKeyHash>       _nameIndex;
        std::unordered_map<UuidTypeKey, size_t, UuidTypeKeyHash>   _offsetIndex;
        size_t _revision;
        bool   _valid;
    };

    RecordIndex &getIndex(const std::string &path);
};
