/*
 * Copyright (c) 2019 Clementine Computing LLC.
 *
 * This file is part of PopuFare.
 *
 * PopuFare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PopuFare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#ifndef PASSDB_SLIM_RULEPARAM_DB_H
#define PASSDB_SLIM_RULEPARAM_DB_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

#include "../common/common_defs.h"
#include "../commhub/commhub.h"
#include "../commhub/client_utils.h"


#define RULEPARAM_DB_FILE DATABASE_FILE_PATH "ruleparam.db"


#define RULEPARAM_DB_NOT_FOUND  (-1)
#define RULEPARAM_DB_FULL       (-2)
#define RULEPARAM_DB_EMPTY      (-3)
#define RULEPARAM_DB_DUP        (-4)
#define RULEPARAM_DB_SANITY     (-5)

#define RULEPARAM_DB_MAX ( 2048 )

typedef struct passdb_slim_ruleparam_t {
  int id;
  int reference_count;
  char name[RULENAME_LEN];
  char param[PARAM_LEN];
  struct passdb_slim_ruleparam_t *next;
} passdb_slim_ruleparam;


typedef struct passdb_slim_ruleparam_db_context_t {
  int n;
  passdb_slim_ruleparam *head;
  char *db_filename;

  int sync_counter;
  int sync_every_n;

  int n_filename_history;     // We want to store some historic ruleparams, which we 
  int pos_filename_history;   // rotate through.
  unsigned long long seq;
} ruleparam_db_ctx;


passdb_slim_ruleparam *_alloc_ruleparam_db_nod(int id, char *name, char *param);


int ruleparam_db_init( ruleparam_db_ctx * );
ruleparam_db_ctx * ruleparam_db_alloc( );
int ruleparam_db_load( ruleparam_db_ctx **, char * );
int ruleparam_db_free( ruleparam_db_ctx * );

int ruleparam_db_debug_dump( ruleparam_db_ctx *);

//int ruleparam_db_save( ruleparam_db_ctx *, char * );
int ruleparam_db_save( ruleparam_db_ctx * );
//int ruleparam_db_rate_limited_sync( ruleparam_db_ctx *, char * );
int ruleparam_db_rate_limited_sync( ruleparam_db_ctx * );

int ruleparam_db_add( ruleparam_db_ctx *, char *, char *);
int ruleparam_db_remove( ruleparam_db_ctx *, int );
int ruleparam_db_find( ruleparam_db_ctx *, char *, char *);
int ruleparam_db_reference_count( ruleparam_db_ctx *, char *, char *);

int ruleparam_db_get( char *, char *, ruleparam_db_ctx *, int );

int ruleparam_db_find_free_id( ruleparam_db_ctx * );

int ruleparam_db_update( ruleparam_db_ctx *ctx, char *name, char *param, int delta_ref_count );

int ruleparam_db_clean( ruleparam_db_ctx *ctx );


int ruleparam_db_consistency_check( ruleparam_db_ctx *ctx );

passdb_slim_ruleparam *ruleparam_db_get_node( ruleparam_db_ctx *ctx, int id );

int format_new_ruleparamdb(char *);

void ruleparam_db_dump(ruleparam_db_ctx *ctx);

#endif

