/*******************************************************************************
 * 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>

#pragma once

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


namespace authdb{
    struct Record;
    class File;

    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 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 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;
        };

        void setPos(size_t pos);
        size_t getPos();

        size_t getRevesion();
        void   newRevesion();

        size_t end();

        void vacuum();

        void purge();

        void write(const unsigned char *dest,size_t destsize);
        void read(char unsigned *src,size_t srcsize);

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

        bool clusterDataExists() const;

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

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