CREATE DATABASE IF NOT EXISTS sevenkeys;

USE sevenkeys;

CREATE TABLE IF NOT EXISTS CacheTimestamps (
	CacheType ENUM('BulkCardPrintings') PRIMARY KEY,
	Stamp DATETIME NOT NULL
);

CREATE TABLE IF NOT EXISTS Gamepiece (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(150) NOT NULL
);

CREATE TABLE IF NOT EXISTS CardPrinting (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	GamepieceId INT NOT NULL,
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
	SetCode VARCHAR(6) NOT NULL,
	ImageUrl VARCHAR(2048) NOT NULL
);

CREATE TABLE IF NOT EXISTS TournamentDecklist (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	DeckName VARCHAR(100) NOT NULL,
	DatePublished DATE NOT NULL,
	Source VARCHAR(200) NOT NULL,
	Format VARCHAR(25) NOT NULL,
	AuthorName VARCHAR(100) NOT NULL
);

CREATE TABLE IF NOT EXISTS TournamentDecklistCard (
	TournamentDecklistId INT NOT NULL,
	GamepieceId INT NOT NULL,
	PRIMARY KEY (TournamentDecklistId, GamepieceId),

	FOREIGN KEY TournamentDecklistId REFERENCES TournamentDecklist(Id),
	FOREIGN KEY GamepieceId REFERENCES Gamepiece(Id),

	CardPrintingId INT NULL,
	FOREIGN KEY CardPrintingId REFERENCES CardPrinting(Id),

	Quantity INT NOT NULL
);
