USE "Gwent - The Witcher Card Game"

CREATE TABLE Factions(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    totalMatches INT NOT NULL DEFAULT 0,
    totalWins INT NOT NULL DEFAULT 0,
    totalLosses INT NOT NULL DEFAULT 0,
    totalDraws INT NOT NULL DEFAULT 0,
    totalCards INT NOT NULL DEFAULT 0,
)

CREATE TABLE CardSet(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    image VARCHAR(255) NOT NULL,
    releaseDate DATE NOT NULL,
)

CREATE TABLE Cards(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    faction INT NOT NULL references Factions(hmy),
    description VARCHAR(1000) NOT NULL,
    action VARCHAR(1000) NOT NULL,
    image VARCHAR(255) NOT NULL,
    cost INT NOT NULL,
    type VARCHAR(255) NOT NULL,
    power INT,
    color VARCHAR(255) NOT NULL,
    cardset INT NOT NULL references CardSet(hmy),
    CHECK(type in ('unit', 'special', 'artifact', 'stratagem')),
    CHECK(color in ('bronze', 'gold')),
)

CREATE TABLE Players(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    totalMatches INT NOT NULL DEFAULT 0,
    totalWins INT NOT NULL DEFAULT 0,
    totalLosses INT NOT NULL DEFAULT 0,
    totalDraws INT NOT NULL DEFAULT 0,
    reward_points INT NOT NULL DEFAULT 0,
    scraps INT NOT NULL DEFAULT 0,
    ores INT NOT NULL DEFAULT 0,
    meteorite_powder INT NOT NULL DEFAULT 0,
    rank INT NOT NULL DEFAULT 30,
)

CREATE TABLE CardsOwnership(
    hCard INT NOT NULL references Cards(hmy),
    hPlayer INT NOT NULL references Players(hmy),
    nrOfCards INT NOT NULL,
    nrOfPremiumCards INT NOT NULL,
    CHECK(nrOfCards in (0,1,2)),
    CHECK(nrOfPremiumCards in (0,1,2)),
    CHECK(nrOfCards + nrOfPremiumCards <= 2 AND nrOfCards + nrOfPremiumCards >= 0),
    PRIMARY KEY(hCard, hPlayer)
)

CREATE TABLE Matches(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    hPlayer1 INT NOT NULL references Players(hmy),
    hPlayer1Faction INT NOT NULL references Factions(hmy),
    hPlayer2 INT NOT NULL references Players(hmy),
    hPlayer2Faction INT NOT NULL references Factions(hmy),
    result INT NOT NULL,
    CHECK(result in (1,2,3)),
    CHECK(hPlayer1 != hPlayer2)
)

CREATE TABLE Cosmetics(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    image VARCHAR(255) NOT NULL,
    type VARCHAR(255) NOT NULL,
    description VARCHAR(255) NOT NULL,
    CHECK(type in ('avatar', 'title', 'border', 'leader', 'cardback', 'coin'))
)

CREATE TABLE CosmeticsOwnership(
    hCosmetic INT NOT NULL references Cosmetics(hmy),
    hPlayer INT NOT NULL references Players(hmy),
    PRIMARY KEY(hCosmetic, hPlayer)
)

CREATE TABLE Journeys(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    storyPanel1 VARCHAR(8000) NOT NULL,
    storyPanel2 VARCHAR(8000) NOT NULL,
    storyPanel3 VARCHAR(8000) NOT NULL,
    storyPanel4 VARCHAR(8000) NOT NULL,
    storyPanel5 VARCHAR(8000) NOT NULL,
    storyPanel6 VARCHAR(8000) NOT NULL,
    storyPanel7 VARCHAR(8000) NOT NULL,
    storyPanel8 VARCHAR(8000) NOT NULL,
    storyPanel9 VARCHAR(8000) NOT NULL,
    storyPanel10 VARCHAR(8000) NOT NULL,
    storyPanel11 VARCHAR(8000) NOT NULL,
    storyPanel12 VARCHAR(8000) NOT NULL,
    level6Reward INT NOT NULL references Cosmetics(hmy),
    level12Reward INT NOT NULL references Cosmetics(hmy),
    level18Reward INT NOT NULL references Cosmetics(hmy),
    level24Reward INT NOT NULL references Cosmetics(hmy),
    level30Reward INT NOT NULL references Cosmetics(hmy),
    level36Reward INT NOT NULL references Cosmetics(hmy),
    level42Reward INT NOT NULL references Cosmetics(hmy),
    level48Reward INT NOT NULL references Cosmetics(hmy),
    level54Reward INT NOT NULL references Cosmetics(hmy),
    level60Reward INT NOT NULL references Cosmetics(hmy),
    level66Reward INT NOT NULL references Cosmetics(hmy),
    level72Reward INT NOT NULL references Cosmetics(hmy),
    level78Reward INT NOT NULL references Cosmetics(hmy),
    level84Reward INT NOT NULL references Cosmetics(hmy),
    level90Reward INT NOT NULL references Cosmetics(hmy),
    level96Reward INT NOT NULL references Cosmetics(hmy),
    level100Reward INT NOT NULL references Cosmetics(hmy),
)

CREATE TABLE JourneyProgress(
    hJourney INT NOT NULL references Journeys(hmy),
    hPlayer INT NOT NULL references Players(hmy),
    level INT NOT NULL DEFAULT 1,
    PRIMARY KEY(hJourney, hPlayer)
)

CREATE TABLE Decks(
    hmy INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    faction INT NOT NULL references Factions(hmy),
    strategem INT NOT NULL references Cards(hmy),
    nrOfCards INT NOT NULL,
    nrOfNeutralCards INT NOT NULL,
    owner INT NOT NULL references Players(hmy),
)

CREATE TABLE DecksCards(
    hDeck INT NOT NULL references Decks(hmy),
    hCard INT NOT NULL references Cards(hmy),
    nrOfCards INT NOT NULL,
    CHECK(nrOfCards in (1,2)),
    PRIMARY KEY(hDeck, hCard)
)


