/*******************************************************************************
 * Copyright (c) 2024, 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.
 *******************************************************************************/

#include <stdexcept>
#include <mutex>
#include <shared_mutex>
#include <atomic>

#pragma once

#ifdef Windows
#define VISIBILITY __declspec(dllexport)
#else
#define VISIBILITY __attribute__((visibility("default")))
#endif


namespace uuid { class uuid; }

namespace authdb{
    struct Record;
    class File;
    class AuthData;
    class User;
    class Client;
    class Gpo;
    class Group;
    class Domain;
    class ServiceManager;
    class RecordIndex;
    class DomainBackend;
    class Export;
    class Import;

    enum AuthBackendType{
        File=0,
        Ceph=1,
        ClusterStore=2
    };

    class VISIBILITY AuthBackendApi {
    public:
        virtual void write(const unsigned char *dest,size_t destsize)=0;
        virtual void read(unsigned char *src,size_t srcsize)=0;
        virtual void prefetch(){}
        virtual void lock()=0;
        virtual void unlock()=0;
        virtual void setPos(size_t pos)=0;
        virtual size_t getPos()=0;
        virtual size_t end()=0;
        virtual size_t getRevesion()=0;
        virtual void newRevesion()=0;
        virtual void vacuum()=0;
        virtual void purge(){};
        virtual void flushSync(){}
        virtual bool clusterDataExists() const { return false; }
    };

    class VISIBILITY AuthBackend {
    public:
        AuthBackend(int type,const std::string &options,const std::string &domain);
        AuthBackend(const AuthBackend &backend);
        ~AuthBackend();

        enum LockMode { Exclusive, Shared };

        class Guard {
        public:
            Guard(AuthBackend &backend, LockMode mode = Exclusive);
            ~Guard();
            Guard(const Guard &) = delete;
            Guard &operator=(const Guard &) = delete;
        private:
            AuthBackend &_Backend;
            LockMode     _Mode;
            bool         _Owns;
            bool         _Upgraded;
        };

        size_t getRevesion();
        void   newRevesion();

        size_t end();

        void vacuum();
        void vacuum(size_t &sizeBefore, size_t &sizeAfter);

        void purge();
        void flushSync();

        void lock();
        void unlock();
        void lock_shared();
        void unlock_shared();

        const std::string &getDomain() const { return _Domain; }
        const std::string &getOptions() const { return _Options; }
        int getType() const { return _Type; }

        bool clusterDataExists() const;

        RecordIndex &getIndex();

        void createRecord(AuthData &rec, int type);
        bool getRecord(AuthData &rec, int type);
        bool editRecord(AuthData &rec, int type);
        void delRecord(const uuid::uuid &uid, int type);
        int  searchValue(const char *fieldname, const char *value);

    private:
        void setPos(size_t pos);
        size_t getPos();
        void write(const unsigned char *dest,size_t destsize);
        void read(char unsigned *src,size_t srcsize);

        friend class RecordIndex;
        friend class DomainBackend;
        friend class Export;
        friend class Import;

    private:
        int                            _Type;
        AuthBackendApi                *_Api;
        const std::string              _Options;
        const std::string              _Domain;
        std::shared_mutex              _Lock;
    };
};
