BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "user" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL UNIQUE,
	"display_name"	TEXT NOT NULL UNIQUE,
	"reference"	TEXT NOT NULL UNIQUE,
	"motto"	TEXT NOT NULL DEFAULT "",
	"password_hash"	TEXT NOT NULL,
	"is_admin" INTEGER NOT NULL DEFAULT 0,
	"is_live" INTEGER NOT NULL DEFAULT 1,
	PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "gift" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL,
	"recipient_id"	INTEGER NOT NULL,
	"claimant_id"	INTEGER,
	"creator_id"	INTEGER NOT NULL,
	"sent"	INTEGER NOT NULL DEFAULT 0,
	PRIMARY KEY("id" AUTOINCREMENT),
	FOREIGN KEY("recipient_id") REFERENCES "user"("id"),
	FOREIGN KEY("creator_id") REFERENCES "user"("id"),
	FOREIGN KEY("claimant_id") REFERENCES "user"("id")
);
CREATE TABLE IF NOT EXISTS "group" (
	"id"	INTEGER NOT NULL UNIQUE,
	"name"	TEXT NOT NULL UNIQUE,
	"reference"	TEXT NOT NULL UNIQUE,
	PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "group_member" (
	"group_id"	INTEGER NOT NULL,
	"user_id"	INTEGER NOT NULL,
	UNIQUE("user_id","group_id"),
	FOREIGN KEY("group_id") REFERENCES "group"("id"),
	FOREIGN KEY("user_id") REFERENCES "user"("id")
);
CREATE TABLE IF NOT EXISTS "session" (
	"id"	INTEGER NOT NULL UNIQUE,
	"value"	TEXT NOT NULL,
	PRIMARY KEY("id" AUTOINCREMENT)
);

DROP VIEW IF EXISTS "v_user";
CREATE VIEW "v_user"
AS
SELECT * FROM user WHERE user.is_live = 1;

-- DROP VIEW IF EXISTS "v_wish";
-- CREATE VIEW "v_wish"
-- AS
-- SELECT gift.id, gift.name, gift.sent FROM gift JOIN user AS recipient;

COMMIT;
