USE DATABASE sevenkeys;

CREATE TABLE IF NOT EXISTS Gamepiece (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	ArenaId INT NULL,
	ScryfallId VARCHAR(36) NOT NULL,
	OracleId VARCHAR(36) NOT NULL,
	Name VARCHAR(141) NOT NULL,
	Layout ENUM("normal",
		"split",
		"flip",
		"transform",
		"modal_dfc",
		"meld",
		"leveler",
		"class",
		"case",
		"saga",
		"adventure",
		"mutate",
		"prototype",
		"battle",
		"planar",
		"scheme",
		"vanguard",
		"token",
		"double_faced_token",
		"emblem",
		"augment",
		"host",
		"art_series",
		"reversible_card") NOT NULL,
	ManaCost VARCHAR(8) NOT NULL,
	ManaValue FLOAT(7,2) NOT NULL,
	TypeLine VARCHAR(50) NOT NULL,
	OracleText VARCHAR(800) NOT NULL,
	Power VARCHAR(5) NULL,
	Toughness VARCHAR(5) NULL,
	ReserveList BOOLEAN NOT NULL,
);

CREATE TABLE IF NOT EXISTS Color (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(5) NOT NULL,
	Abbreviation VARCHAR(1) NOT NULL
);
INSERT INTO Color (Name, Abbreviation) VALUES ("White", "W");
INSERT INTO Color (Name, Abbreviation) VALUES ("Blue", "U");
INSERT INTO Color (Name, Abbreviation) VALUES ("Black", "B");
INSERT INTO Color (Name, Abbreviation) VALUES ("Red", "R");
INSERT INTO Color (Name, Abbreviation) VALUES ("Green", "G");

CREATE TABLE IF NOT EXISTS GamepieceColor (
	ColorId INT,
	GamepieceId INT,
	PRIMARY KEY (ColorId, GamepieceId),
	FOREIGN KEY (ColorId) REFERENCES Color(Id),
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);

CREATE TABLE IF NOT EXISTS GamepieceColorIdentity (
	ColorId INT,
	GamepieceId INT,
	PRIMARY KEY (ColorId, GamepieceId),
	FOREIGN KEY (ColorId) REFERENCES Color(Id),
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);

CREATE TABLE IF NOT EXISTS GamepieceColorIndicator (
	ColorId INT,
	GamepieceId INT,
	PRIMARY KEY (ColorId, GamepieceId),
	FOREIGN KEY (ColorId) REFERENCES Color(Id),
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id)
);

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

CREATE TABLE IF NOT EXISTS GamepieceKeyword (
	GamepieceId INT NOT NULL,
	KeywordId INT NOT NULL,
	PRIMARY KEY (GamepieceId, KeywordId),
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
	FOREIGN KEY (KeywordId) REFERENCES Keyword(Id)
);

CREATE TABLE IF NOT EXISTS Format (
	Id INT AUTO_INCREMENT PRIMARY KEY,
	Name VARCHAR(50)
);

CREATE TABLE IF NOT EXISTS GamepieceFormatLegality (
	GamepieceId INT NOT NULL,
	FormatId INT NOT NULL,
	PRIMARY KEY (GamepieceId, FormatId),
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
	FOREIGN KEY (FormatId) REFERENCES Format(Id),
	Legality ENUM("legal", "not_legal", "restricted", "banned") NOT NULL
);

CREATE TABLE IF NOT EXISTS ExpansionSet (
	Id INT AUTO_INCREMENT NOT NULL,
	SetCode VARCHAR(4) NOT NULL,
	Name VARCHAR(40) NOT NULL,
	Digital BOOLEAN NOT NULL,
);

CREATE TABLE IF NOT EXISTS Artist (
	Id INT AUTO_INCREMENT NOT NULL,
	ScryfallId VARCHAR(36) NOT NULL,
	Name VARCHAR(100) NOT NULL
);

CREATE TABLE IF NOT EXISTS CardPrinting (
	Id INT AUTO_INCREMENT NOT NULL,
	GamepieceId INT,
	FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
	Language VARCHAR(3) NOT NULL,
	ReleasedAt DATETIME NOT NULL,
	Reprint BOOLEAN NOT NULL,
	ExpansionSetId INT NOT NULL,
	FOREIGN KEY (SetId) REFERENCES ExpansionSet(Id),
	CollectorNumber VARCHAR(10) NOT NULL,
	Rarity ENUM("common", "uncommon", "rare", "special", "mythic", "bonus") NOT NULL,
	ArtistId INT NULL,
	FOREIGN KEY (ArtistId) REFERENCES Artist(Id),
	BorderColor ENUM("black", "white", "borderless", "silver", "gold") NOT NULL,
	Frame ENUM("1993", "1997", "2003", "2015", "future") NOT NULL,
	FullArt BOOLEAN NOT NULL,
	Textless BOOLEAN NOT NULL,
	StorySpotlight BOOLEAN NOT NULL,
	FlavorName VARCHAR(40) NULL,
	FlavorText VARCHAR(300) NULL,
	DigitalOnly BOOLEAN NOT NULL,
	Variation BOOLEAN NOT NULL,
	VariationId VARCHAR(36) NULL,
	SecurityStamp ENUM("oval", "triangle", "acorn", "circle", "arena", "heart") NULL,
);
