/*
	@BLOG
*/
DROP TABLE IF EXISTS blog;
CREATE TABLE blog (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`author_id` INTEGER NOT NULL,
	`title` TEXT NOT NULL,
	`slug` TEXT NOT NULL UNIQUE,
	`content` TEXT NOT NULL,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_blog_author_id ON blog (`author_id`);
CREATE INDEX idx_blog_slug ON blog (`slug`);

/*
	@CHARS
*/
DROP TABLE IF EXISTS characters;
CREATE TABLE characters (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`user_id` INTEGER NOT NULL,
	`name` TEXT NOT NULL UNIQUE,
	`title_id` INTEGER NOT NULL DEFAULT 1,
	`level` INTEGER NOT NULL DEFAULT 1,
	`xp` INTEGER NOT NULL DEFAULT 0,
	`xp_to_level` INTEGER NOT NULL DEFAULT 100,
	`hp` INTEGER NOT NULL DEFAULT 20,
	`m_hp` INTEGER NOT NULL DEFAULT 20,
	`mp` INTEGER NOT NULL DEFAULT 10,
	`m_mp` INTEGER NOT NULL DEFAULT 10,
	`tp` INTEGER NOT NULL DEFAULT 1,
	`m_tp` INTEGER NOT NULL DEFAULT 1,
	`pow` INTEGER NOT NULL DEFAULT 0,  -- Power
	`acc` INTEGER NOT NULL DEFAULT 0,  -- Accuracy
	`pen` INTEGER NOT NULL DEFAULT 0,  -- Penetration
	`foc` INTEGER NOT NULL DEFAULT 0,  -- Focus
	`tou` INTEGER NOT NULL DEFAULT 0,  -- Toughness
	`arm` INTEGER NOT NULL DEFAULT 0,  -- Armor
	`res` INTEGER NOT NULL DEFAULT 0,  -- Resist
	`pre` INTEGER NOT NULL DEFAULT 0,  -- Precision
	`fer` INTEGER NOT NULL DEFAULT 0,  -- Ferocity
	`luck` INTEGER NOT NULL DEFAULT 0, -- Luck
	`inv_slots` INTEGER NOT NULL DEFAULT 10,
	`att_points` INTEGER NOT NULL DEFAULT 0,
	`bio` TEXT DEFAULT ''
);
CREATE INDEX idx_characters_user_id ON characters (`user_id`);

/*
	@CHARGEAR
*/
DROP TABLE IF EXISTS char_gear;
CREATE TABLE char_gear (
	`char_id` INTEGER NOT NULL,
	`head` INTEGER NOT NULL DEFAULT 0,
	`chest` INTEGER NOT NULL DEFAULT 0,
	`boots` INTEGER NOT NULL DEFAULT 0,
	`hands` INTEGER NOT NULL DEFAULT 0,
	`m_hand` INTEGER NOT NULL DEFAULT 0,
	`o_hand` INTEGER NOT NULL DEFAULT 0,
	`rune` INTEGER NOT NULL DEFAULT 0,
	`ring` INTEGER NOT NULL DEFAULT 0,
	`amulet` INTEGER NOT NULL DEFAULT 0,
	`pow` INTEGER NOT NULL DEFAULT 0,  -- Power
	`acc` INTEGER NOT NULL DEFAULT 0,  -- Accuracy
	`pen` INTEGER NOT NULL DEFAULT 0,  -- Penetration
	`foc` INTEGER NOT NULL DEFAULT 0,  -- Focus
	`tou` INTEGER NOT NULL DEFAULT 0,  -- Toughness
	`arm` INTEGER NOT NULL DEFAULT 0,  -- Armor
	`res` INTEGER NOT NULL DEFAULT 0,  -- Resist
	`pre` INTEGER NOT NULL DEFAULT 0,  -- Precision
	`fer` INTEGER NOT NULL DEFAULT 0,  -- Ferocity
	`luck` INTEGER NOT NULL DEFAULT 0, -- Luck
	`max_hp` INTEGER NOT NULL DEFAULT 0,
	`max_mp` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_char_gear_char_id ON char_gear (`char_id`);

/*
	@CHARINV
*/
DROP TABLE IF EXISTS char_inventory;
CREATE TABLE char_inventory (
	`char_id` INTEGER NOT NULL,
	`item_id` INTEGER NOT NULL
);
CREATE INDEX idx_inventory_char_id ON char_inventory (`char_id`);

/*
	@WALLETS
*/
DROP TABLE IF EXISTS wallets;
CREATE TABLE wallets (
	`user_id` INTEGER NOT NULL,
	`silver` INTEGER NOT NULL DEFAULT 10,
	`stargem` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_wallets_user_id ON wallets (`user_id`);

/*
	@CHARBANK
*/
DROP TABLE IF EXISTS char_bank;
CREATE TABLE char_bank (
	`char_id` INTEGER NOT NULL,
	`slots` INTEGER NOT NULL DEFAULT 5,
	`silver` INTEGER NOT NULL DEFAULT 0,
	`tier` INTEGER NOT NULL DEFAULT 0,
	`can_collect` INTEGER NOT NULL DEFAULT 1,
	`last_collect` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_bank_char_id ON char_bank (`char_id`);

/*
	@CHARBANKITEMS
*/
DROP TABLE IF EXISTS char_banked_items;
CREATE TABLE char_banked_items (
	`char_id` INTEGER NOT NULL,
	`item_id` INTEGER NOT NULL
);
CREATE INDEX idx_banked_items_char_id ON char_banked_items (`char_id`);
CREATE INDEX idx_banked_items_item_id ON char_banked_items (`item_id`);

/*
	@TOWNS
*/
DROP TABLE IF EXISTS towns;
CREATE TABLE towns (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`x` INTEGER NOT NULL,
	`y` INTEGER NOT NULL,
	`type` INTEGER NOT NULL,
	`lore` TEXT NOT NULL,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_towns_location ON towns (`x`, `y`);

/*
	@SHOPS
*/
DROP TABLE IF EXISTS shops;
CREATE TABLE shops (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`type` INTEGER NOT NULL,
	`lore` TEXT NOT NULL,
	`x` INTEGER NOT NULL,
	`y` INTEGER NOT NULL,
	`items` TEXT NOT NULL,
	`gear` TEXT NOT NULL,
	`materials` TEXT NOT NULL,
	`b_mod` INTEGER NOT NULL DEFAULT 100,
	`s_mod` INTEGER NOT NULL DEFAULT 100,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_shops_location ON shops (`x`, `y`);

/*
	@INNS
*/
DROP TABLE IF EXISTS inns;
CREATE TABLE inns (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`type` INTEGER NOT NULL,
	`lore` TEXT NOT NULL,
	`x` INTEGER NOT NULL,
	`y` INTEGER NOT NULL,
	`cost` INTEGER NOT NULL,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_inns_location ON inns (`x`, `y`);

/*
	@GUILDS
*/
DROP TABLE IF EXISTS guilds;
CREATE TABLE guilds (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`lore` TEXT NOT NULL DEFAULT "",
	`leader_id` INTEGER NOT NULL,
	`silver` INTEGER NOT NULL DEFAULT 0,
	`rep` INTEGER NOT NULL DEFAULT 0,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_guilds_leader_id ON guilds (`leader_id`);

/*
	@GUILDRANKS
*/
DROP TABLE IF EXISTS guild_ranks;
CREATE TABLE guild_ranks (
	`guild_id` INTEGER NOT NULL,
	`order` INTEGER NOT NULL,
	`name` TEXT NOT NULL,
	`permissions` TEXT NOT NULL,
	`leader` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_guild_ranks_guild_id ON guild_ranks (`guild_id`);

/*
	@GUILDMEMBERS
*/
DROP TABLE IF EXISTS guild_members;
CREATE TABLE guild_members (
	`guild_id` INTEGER NOT NULL,
	`char_id` INTEGER NOT NULL,
	`rank` INTEGER NOT NULL,
	`rep` INTEGER NOT NULL DEFAULT 0,
	`donated` INTEGER NOT NULL DEFAULT 0,
	`joined` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_guild_members_guild_id ON guild_members (`guild_id`);
CREATE INDEX idx_guild_members_char_id ON guild_members (`char_id`);

/*
	@NPCS
*/
DROP TABLE IF EXISTS npcs;
CREATE TABLE npcs (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`type` INTEGER NOT NULL,
	`lore` TEXT NOT NULL,
	`conversation` TEXT NOT NULL,
	`x` INTEGER NOT NULL,
	`y` INTEGER NOT NULL,
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_npcs_location ON npcs (`x`, `y`);

/*
	@TOWNREP
*/
DROP TABLE IF EXISTS char_town_rep;
CREATE TABLE char_town_rep (
	`char_id` INTEGER NOT NULL,
	`town_id` INTEGER NOT NULL,
	`rep` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_char_town_rep_char_id ON char_town_rep (`char_id`);

/*
	@ITEMS
*/
DROP TABLE IF EXISTS items;
CREATE TABLE items (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL,
	`type` TEXT NOT NULL DEFAULT 0,
	`rarity` INTEGER NOT NULL DEFAULT 0,
	`forged` INTEGER NOT NULL DEFAULT 0,
	`quality` INTEGER NOT NULL DEFAULT 0,
	`value` INTEGER NOT NULL DEFAULT 0,
	`consumable` INTEGER NOT NULL DEFAULT 0,
	`duration` INTEGER NOT NULL DEFAULT 0,
	`durability` INTEGER NOT NULL DEFAULT 0,
	`m_durability` INTEGER NOT NULL DEFAULT 0,
	`pow` INTEGER NOT NULL DEFAULT 0,  -- Power
	`acc` INTEGER NOT NULL DEFAULT 0,  -- Accuracy
	`pen` INTEGER NOT NULL DEFAULT 0,  -- Penetration
	`foc` INTEGER NOT NULL DEFAULT 0,  -- Focus
	`tou` INTEGER NOT NULL DEFAULT 0,  -- Toughness
	`arm` INTEGER NOT NULL DEFAULT 0,  -- Armor
	`res` INTEGER NOT NULL DEFAULT 0,  -- Resist
	`pre` INTEGER NOT NULL DEFAULT 0,  -- Precision
	`fer` INTEGER NOT NULL DEFAULT 0,  -- Ferocity
	`luck` INTEGER NOT NULL DEFAULT 0, -- Luck
	`reqs` TEXT NOT NULL DEFAULT "",
	`traits` TEXT NOT NULL DEFAULT "",
	`lore` TEXT NOT NULL DEFAULT "",
	`created` DATETIME DEFAULT CURRENT_TIMESTAMP,
	`updated` DATETIME DEFAULT CURRENT_TIMESTAMP
);

/*
	@CHARTRAITS
*/
DROP TABLE IF EXISTS char_traits;
CREATE TABLE char_traits (
	`char_id` INTEGER NOT NULL,
	`trait_id` INTEGER NOT NULL,
	`value` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_char_traits_char_id ON char_traits (`char_id`);

/*
	@CHARLOCATIONS
*/
DROP TABLE IF EXISTS char_locations;
CREATE TABLE char_locations (
	`char_id` INTEGER NOT NULL,
	`x` INTEGER NOT NULL,
	`y` INTEGER NOT NULL,
	`currently` INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX idx_char_locations_char_id ON char_locations (`char_id`);
CREATE INDEX idx_char_locations_location ON char_locations (`x`, `y`);

/*
	@TITLES
*/
DROP TABLE IF EXISTS titles;
CREATE TABLE titles (
	`id` INTEGER PRIMARY KEY AUTOINCREMENT,
	`name` TEXT NOT NULL DEFAULT 'Title',
	`lore` TEXT
);

/*
	@OWNEDTITLES
*/
DROP TABLE IF EXISTS owned_titles;
CREATE TABLE owned_titles (
	`title_id` INTEGER NOT NULL,
	`char_id` INTEGER NOT NULL,
	`awarded` DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_owned_titles_char_id ON owned_titles (`char_id`);
CREATE INDEX idx_owned_titles_owned ON owned_titles (`char_id`, `title_id`);
