CREATE TABLE admin (
	AdminID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	Name CHAR(50) NOT NULL,
	Password CHAR(50) NOT NULL,
	Email CHAR(50) NOT NULL,
	AccountType INT UNSIGNED NOT NULL,
	ActivateStatus INT UNSIGNED NOT NULL
);

CREATE TABLE maestro (
	MaestroID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	Name CHAR(50) NOT NULL,
	Password CHAR(50) NOT NULL,
	Email CHAR(50) NOT NULL,
	AccountType INT UNSIGNED NOT NULL,
	ActivateStatus INT UNSIGNED NOT NULL,
	AvailableActivateDateTime DATETIME NOT NULL,
	PlayerCount INT UNSIGNED NOT NULL,
	AcceptClausesDateTime DATETIME NOT NULL,
	AllowEditEnterCode INT UNSIGNED NOT NULL,
	MaestroTestID INT UNSIGNED
);

CREATE TABLE maestro_upgrade (
	MaestroUpgradeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	RegisteredAccountType INT UNSIGNED NOT NULL,
	RequestedAccountType INT UNSIGNED NOT NULL,
	RequestedDateTime DATETIME NOT NULL,
	Status INT UNSIGNED NOT NULL,
	MaestroID INT UNSIGNED NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID)
);

CREATE TABLE maestro_extension (
	MaestroExtensionID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	AccountType INT UNSIGNED NOT NULL,
	RequestedDateTime DATETIME NOT NULL,
	Status INT UNSIGNED NOT NULL,
	MaestroID INT UNSIGNED NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID)
);

CREATE TABLE player (
	PlayerID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	Name CHAR(50) NOT NULL,
	EnterCode CHAR(8) NOT NULL,
	AccountType INT UNSIGNED NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID)
);



CREATE TABLE app (
	AppID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	AppName CHAR(40) NOT NULL,
	KoreanName CHAR(40) NOT NULL,
	AppType INT UNSIGNED NOT NULL,
	Status INT UNSIGNED NOT NULL,
	HowToPlay TEXT NOT NULL
);

CREATE TABLE active_app (
	ActiveAppID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	AppID INT UNSIGNED NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (AppID) REFERENCES app(AppID)
);



CREATE TABLE best_record (
	BestRecordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	AppID INT UNSIGNED NOT NULL,
	PlayerID INT UNSIGNED NOT NULL,
	BestRecord FLOAT NOT NULL,
	RecordDateTime DATETIME NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (AppID) REFERENCES app(AppID),
	FOREIGN KEY (PlayerID) REFERENCES player(PlayerID)
);

CREATE TABLE app_highest_record (
	AppHighestRecordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	AppID INT UNSIGNED NOT NULL,
	PlayerID INT UNSIGNED NOT NULL,
	HighestRecord FLOAT NOT NULL,
	RecordDateTime DATETIME NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (AppID) REFERENCES app(AppID),
	FOREIGN KEY (PlayerID) REFERENCES player(PlayerID)
);

CREATE TABLE ranking (
	RankingID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	AppID INT UNSIGNED NOT NULL,
	RankingType INT UNSIGNED NOT NULL,
	Rank INT UNSIGNED NOT NULL,
	PlayerID INT UNSIGNED NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (AppID) REFERENCES app(AppID),
	FOREIGN KEY (PlayerID) REFERENCES player(PlayerID)
);

CREATE TABLE banned_word (
	BannedWordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	Word CHAR(50) NOT NULL
);

CREATE TABLE maestro_log (
	MaestroLogID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	Type CHAR(50) NOT NULL,
	LogDateTime DATETIME NOT NULL,
	MaestroID INT UNSIGNED NOT NULL,
	Remark CHAR(100) NOT NULL
);


-- Typing Examination

CREATE TABLE ads_client (
	AdsClientID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	ClientName CHAR(50) NOT NULL
);
INSERT INTO `ads_client` (`AdsClientID`, `ClientName`) VALUES
(1, 'Chocomae');


CREATE TABLE writing (
	WritingID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	ActivateStatus INT UNSIGNED NOT NULL,
	Language CHAR(10) NOT NULL,
	Name CHAR(50) NOT NULL,
	Filename CHAR(50) NOT NULL,
	Writer CHAR(50) NOT NULL,
	WriterID INT UNSIGNED,
	UpdateDate DATETIME NOT NULL,
	LetterCount INT UNSIGNED NOT NULL
);
INSERT INTO `writing` (`WritingID`, `ActivateStatus`, `Language`, `Name`, `Filename`, `Writer`, `WriterID`, `UpdateDate`, `LetterCount`) VALUES
(1, 1, 'korean', '봄2', 'korean_spring.txt', 'system', NULL, '2019-07-04 00:00:00', 155),
(2, 1, 'korean', '애국가', 'korean_national_anthem.txt', 'system', NULL, '2019-07-04 00:00:00', 208),
(3, 1, 'korean', '냉면', 'korean_cold_noodle.txt', 'system', NULL, '2019-07-04 00:00:00', 2099),
(4, 0, 'korean', '허생전', 'korean_heoseng.txt', 'system', NULL, '2019-07-04 00:00:00', 8837),
(5, 1, 'english', 'Time is', 'english_time_is.txt', 'system', NULL, '2019-07-11 00:00:00', 186),
(6, 1, 'english', 'Beautiful dreamer', 'english_beautiful_dreamer.txt', 'system', NULL, '2019-07-11 00:00:00', 729),
(7, 0, 'english', 'The wolf and the seven little kids', 'english_wolf_7kids.txt', 'system', NULL, '2019-07-11 00:00:00', 5765),
(8, 0, 'english', 'The happy prince', 'english_happy_prince.txt', 'system', NULL, '2019-07-11 00:00:00', 18355),
(9, 0, 'korean', '서시', 'korean_seosi.txt', 'system', NULL, '2019-07-04 00:00:00', 133),
(10, 1, 'english', 'The North Wind and The Sun', 'english_north_wind.txt', 'system', NULL, '2019-07-11 00:00:00', 1145),
(11, 1, 'english', 'The three little pigs', 'english_3_little_pigs.txt', 'system', NULL, '2019-07-11 00:00:00', 2118),
(12, 0, 'korean', '약손', 'korean_drug_hand.txt', 'system', NULL, '2019-07-11 00:00:00', 959),
(13, 1, 'korean', '은혜 갚은 까치', 'korean_magpie.txt', 'system', NULL, '2019-09-11 00:00:00', 953),
(14, 1, 'korean', '꼬리로 낚시하는 호랑이', 'korean_fishing_tiger.txt', 'system', NULL, '2019-09-11 00:00:00', 686);


CREATE TABLE typing_exam_record (
	TypingExamRecordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	PlayerID INT UNSIGNED NOT NULL,
	WritingID INT UNSIGNED NOT NULL,
	Record FLOAT NOT NULL,
	RecordDateTime DATETIME NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (PlayerID) REFERENCES player(PlayerID),
	FOREIGN KEY (WritingID) REFERENCES writing(WritingID)
);

CREATE TABLE typing_exam_highest_record (
	TypingExamHighestRecordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	MaestroID INT UNSIGNED NOT NULL,
	PlayerID INT UNSIGNED NOT NULL,
	WritingID INT UNSIGNED NOT NULL,
	HighestRecord FLOAT NOT NULL,
	RecordDateTime DATETIME NOT NULL,

	FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
	FOREIGN KEY (PlayerID) REFERENCES player(PlayerID),
	FOREIGN KEY (WritingID) REFERENCES writing(WritingID)
);

CREATE TABLE typing_exam_ads (
	TypingExamAdsID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	AdsClientID INT UNSIGNED NOT NULL,
	WritingID INT UNSIGNED NOT NULL,
	FromDateTime DATETIME NOT NULL,
	ToDateTime DATETIME NOT NULL,
	Status INT UNSIGNED NOT NULL,
	Cost FLOAT NOT NULL,

	FOREIGN KEY (AdsClientID) REFERENCES ads_client(AdsClientID),
	FOREIGN KEY (WritingID) REFERENCES writing(WritingID)
);