CREATE TABLE IF NOT EXISTS `sets` (
    `set_code` TEXT PRIMARY KEY,
    `name` TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS `rarities` (
    `rarity` TEXT PRIMARY KEY,
    `rarity_ord` INTEGER NOT NULL
);

DELETE FROM `rarities`;
INSERT INTO `rarities` (`rarity`, `rarity_ord`) VALUES
('common', 1),
('uncommon', 2),
('rare', 3),
('special', 4),
('mythic', 5),
('bonus', 6);

CREATE TABLE IF NOT EXISTS `cards` (
    `scryfall_id` TEXT PRIMARY KEY,
    `oracle_id` TEXT NOT NULL,
    `name` TEXT NOT NULL,
    `set_code` TEXT NOT NULL,
    `collector_number` TEXT NOT NULL,
    `release_date` TEXT NOT NULL,
    `rarity` TEXT NOT NULL,
    `color_identity` TEXT NOT NULL,
    `cmc` TEXT NOT NULL, -- Decimal value
    `type_line` TEXT NOT NULL,
    `foil` INTEGER NOT NULL DEFAULT 0,
    `nonfoil` INTEGER NOT NULL DEFAULT 1,
    `variation` INTEGER NOT NULL DEFAULT 0,
    `edhrec_rank` INTEGER,
    `oracle_text` TEXT,
    FOREIGN KEY (`set_code`) REFERENCES `sets` (`set_code`),
    FOREIGN KEY (`rarity`) REFERENCES `rarities` (`rarity`)
);

CREATE INDEX IF NOT EXISTS `cards_name` ON `cards`(`name` COLLATE nocase);
CREATE INDEX IF NOT EXISTS `cards_rarity` ON `cards`(`rarity`);
CREATE INDEX IF NOT EXISTS `cards_color_identity` ON `cards`(`color_identity`);
CREATE INDEX IF NOT EXISTS `cards_oracle_id` ON `cards` (`oracle_id`);
CREATE INDEX IF NOT EXISTS `cards_name` ON `cards` (`name`);

CREATE TABLE IF NOT EXISTS `copies` (
    `id` INTEGER PRIMARY KEY AUTOINCREMENT,
    `collection` TEXT NOT NULL DEFAULT 'Default',
    `scryfall_id` TEXT,
    `isFoil` INTEGER NOT NULL DEFAULT 0,
    `language` TEXT,
    `condition` TEXT,
    FOREIGN KEY (`scryfall_id`) REFERENCES `cards`(`scryfall_id`)
);

CREATE TABLE IF NOT EXISTS `card_prices` (
    `scryfall_id` TEXT,
    `date` TEXT,
    `usd` TEXT, -- Decimal value
    `usd_foil` TEXT, -- Decimal value
    `eur` TEXT, -- Decimal value
    `eur_foil` TEXT, -- Decimal value
    `tix` TEXT, -- Decimal value
    PRIMARY KEY (`scryfall_id`, `date`),
    FOREIGN KEY (`scryfall_id`) REFERENCES `cards`(`scryfall_id`)
);

CREATE TABLE IF NOT EXISTS `legalities` (
    `scryfall_id` TEXT NOT NULL,
    `format` TEXT NOT NULL,
    `legality` TEXT NOT NULL,
    PRIMARY KEY (`scryfall_id`, `format`),
    FOREIGN KEY (`scryfall_id`) REFERENCES `cards`(`scryfall_id`)
);

CREATE TABLE IF NOT EXISTS `games` (
    `scryfall_id` TEXT NOT NULL,
    `game` TEXT NOT NULL, -- 'paper', 'arena', or 'mtgo'
    PRIMARY KEY (`scryfall_id`, `game`),
    FOREIGN KEY (`scryfall_id`) REFERENCES `cards`(`scryfall_id`)
);

CREATE TABLE IF NOT EXISTS `decks` (
    `deck_id` INTEGER PRIMARY KEY AUTOINCREMENT,
    `name` TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS `deck_cards` (
    `deck_id` INTEGER NOT NULL,
    `oracle_id` TEXT NOT NULL,
    `quantity` INTEGER NOT NULL DEFAULT 1,
    PRIMARY KEY (`deck_id`, `oracle_id`),
    FOREIGN KEY (`deck_id`) REFERENCES `decks`(`deck_id`)
);

CREATE TABLE IF NOT EXISTS `vars` (
    `key` TEXT PRIMARY KEY,
    `value` TEXT
);
