/*
 * Copyright (C) 2014-2021 CZ.NIC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include <QMap>
#include <QPair>
#include <QString>
#include <QVector>

#include "src/io/tag_db_tables.h"

namespace TagTbl {
	const QString tabName("tag");

	const QVector< QPair<QString, enum EntryType> > knownAttrs = {
	{"id", DB_INTEGER}, /* NOT NULL */
	{"tag_name", DB_TEXT}, /* NOT NULL */
	{"tag_color", DB_TEXT},
	/*
	 * PRIMARY KEY (id),
	 * UNIQUE (tag_name)
	 */
	};

	const QMap<QString, QString> colConstraints = {
	    {"id", "NOT NULL"},
	    {"tag_name", "NOT NULL"}
	};

	const QString &tblConstraint(
	    ",\n"
	    "        PRIMARY KEY (id),\n"
	    "        UNIQUE (tag_name)"
	);

	const QMap<QString, SQLiteTbl::AttrProp> attrProps = {
	{"id",       {DB_INTEGER, ""}},
	{"tag_name", {DB_TEXT, ""}},
	{"tag_color", {DB_TEXT, ""}}
	};
} /* namespace TagTbl */
SQLiteTbl tagTbl(TagTbl::tabName, TagTbl::knownAttrs,
    TagTbl::attrProps, TagTbl::colConstraints,
    TagTbl::tblConstraint);

namespace MsgTagsTbl {
	const QString tabName("message_tags");

	const QVector< QPair<QString, enum EntryType> > knownAttrs = {
	{"id", DB_INTEGER}, /* NOT NULL */
	{"user_name", DB_TEXT}, /* NOT NULL */
	{"message_id", DB_INTEGER}, /* NOT NULL */
	{"tag_id", DB_INTEGER} /* NOT NULL */
	/*
	 * PRIMARY KEY (id),
	 * FOREIGN KEY(tag_id) REFERENCES tag ("id")
	 */
	};

	const QMap<QString, QString> colConstraints = {
	    {"id", "NOT NULL"},
	    {"user_name", "NOT NULL"},
	    {"message_id", "NOT NULL"},
	    {"tag_id", "NOT NULL"}
	};

	const QString &tblConstraint(
	    ",\n"
	    "        PRIMARY KEY (id),\n"
	    "        FOREIGN KEY(tag_id) REFERENCES tag (id)"
	);

	const QMap<QString, SQLiteTbl::AttrProp> attrProps = {
	{"id",           {DB_INTEGER, ""}},
	{"user_name",     {DB_TEXT, ""}},
	{"message_id",   {DB_INTEGER, ""}},
	{"tag_id",       {DB_INTEGER, ""}}
	};
} /* namespace MsgTagsTbl */
SQLiteTbl msgtagsTbl(MsgTagsTbl::tabName, MsgTagsTbl::knownAttrs,
    MsgTagsTbl::attrProps, MsgTagsTbl::colConstraints,
    MsgTagsTbl::tblConstraint);
