CREATE TABLE player (
    player_id INTEGER PRIMARY KEY,
    player_name VARCHAR(256) NOT NULL,
    player_password VARCHAR(256) NOT NULL
);

CREATE TABLE base (
    base_id INTEGER PRIMARY KEY,
    player_id INTEGER NOT NULL REFERENCES player(player_id),
    "level" INTEGER NOT NULL
);

CREATE TABLE currency (
    currency_id INTEGER PRIMARY KEY,
    currency_name VARCHAR(128) NOT NULL,
    premium BOOLEAN NOT NULL DEFAULT false
);

CREATE TABLE "resource" (
    resource_id INTEGER PRIMARY KEY,
    resource_name VARCHAR(128) NOT NULL
);

CREATE TABLE building (
    building_id INTEGER PRIMARY KEY,
    building_name VARCHAR(256) NOT NULL,
    building_description VARCHAR(512) NOT NULL,
    resource_generated INTEGER NOT NULL REFERENCES "resource"(resource_id)
);

CREATE TABLE buildingcost (
    building_id INTEGER PRIMARY KEY REFERENCES building(building_id),
    resource_id INTEGER NOT NULL REFERENCES "resource"(resource_id),
    amount INTEGER NOT NULL,
    build_time INTERVAL NOT NULL
);

CREATE TABLE basebuilding (
    building_id INTEGER NOT NULL REFERENCES building(building_id),
    base_id INTEGER REFERENCES base(base_id)
);

CREATE TABLE constructionqueue (
    construction_id INTEGER PRIMARY KEY,
    building_id INTEGER NOT NULL REFERENCES building(building_id),
    base_id INTEGER NOT NULL REFERENCES base(base_id),
    "start_date" DATE NOT NULL
);

CREATE TABLE playercurrency (
    player_id INTEGER REFERENCES player(player_id),
    currency_id INTEGER NOT NULL REFERENCES currency(currency_id),
    amount INTEGER NOT NULL
);

CREATE TABLE playerresource (
    player_id INTEGER REFERENCES player(player_id),
    resource_id INTEGER NOT NULL REFERENCES "resource"(resource_id),
    amount INTEGER NOT NULL
);


INSERT INTO player VALUES (1, 'HarrySmith', 'password1'), (2, 'SophieBrown', 'password2'), (3, 'OliverTaylor', 'password3'), (4, 'FatimaAhmed', 'password4'), (5, 'MohammedAli', 'password5'), (6, 'AishaKhan', 'password6');
INSERT INTO base VALUES (1, 1, 2), (2, 2, 1), (3, 3, 3), (4, 4, 1), (5, 5, 2), (6, 6, 1);
INSERT INTO currency VALUES (1, 'Gold', 'false'), (2, 'Ethereal Silver', 'false'), (3, 'Diamonds', 'true');
INSERT INTO "resource" VALUES (1, 'Food'), (2, 'Wood'), (3, 'Stone'), (4, 'Gold');
INSERT INTO building VALUES (1, 'Farm', 'Produces food for the kingdom', 1), (2, 'Lumber Mill', 'Produces wood for the kingdom', 2), (3, 'Stone Quarry', 'Produces stone for the kingdom', 3), (4, 'Gold Mine', 'Produces gold for the kingdom', 4), (5, 'Marketplace', 'Allows for the exchange of resources between kingdoms', 4);
INSERT INTO buildingcost VALUES (1, 4, 100, '3 days'), (2, 4, 75, '2 days'), (3, 4, 150, '5 days'), (4, 4, 200, '7 days'), (5, 4, 350, '14 days');
INSERT INTO basebuilding VALUES (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (1, 4), (2, 4), (3, 4), (1, 5), (2, 5), (1, 6), (2, 6), (3, 6), (4, 6);
INSERT INTO constructionqueue VALUES (1, 4, 2, '2023-04-09'), (2, 5, 2, '2023-04-08'), (3, 3, 5, '2023-04-06'), (4, 4, 5, '2023-04-07');
INSERT INTO playercurrency VALUES (1, 1, 100), (1, 2, 50), (1, 3, 10), (2, 1, 200), (2, 2, 100), (2, 3, 20), (3, 1, 50), (3, 2, 20), (3, 3, 5), (4, 1, 150), (4, 2, 75), (5, 1, 75), (5, 2, 30), (5, 3, 8), (6, 1, 300), (6, 2, 150), (6, 3, 50);
INSERT INTO playerresource VALUES (1, 1, 5000), (1, 2, 2500), (1, 3, 1000), (1, 4, 100), (2, 1, 4500), (2, 2, 2000), (2, 3, 500), (2, 4, 50), (3, 1, 6000), (3, 2, 3000), (3, 3, 1500), (3, 4, 200), (4, 1, 4000), (4, 2, 1500), (4, 3, 2000), (4, 4, 300), (5, 1, 5500), (5, 2, 3500), (5, 3, 800), (5, 4, 150), (6, 1, 7000), (6, 2, 4000), (6, 3, 2000), (6, 4, 500);


CREATE VIEW building_info AS
    SELECT building.building_name AS "Name",
           building.building_description AS "Description",
           "resource".resource_name AS "Resources generated",
           buildingcost.amount AS "Required gold",
           buildingcost.build_time AS "Build time"
    FROM building JOIN "resource" ON "resource".resource_id = building.resource_generated
                  JOIN buildingcost ON buildingcost.building_id = building.building_id;

CREATE VIEW player_buildings AS
    SELECT player.player_name AS "Name",
           basebuilding.base_id AS "Base ID",
           building.building_name AS "Building",
           base.level AS "Level"
    FROM basebuilding JOIN base ON basebuilding.base_id = base.base_id
                      JOIN building ON basebuilding.building_id = building.building_id
                      JOIN player ON base.base_id = player.player_id;

CREATE VIEW player_resources AS
    SELECT player.player_name as "Name",
           (SELECT amount FROM playerresource AS pr WHERE pr.player_id = player.player_id AND pr.resource_id=(SELECT resource_id FROM "resource" WHERE "resource".resource_name = 'Wood')) AS "Wood amount",
           (SELECT amount FROM playerresource AS pr WHERE pr.player_id = player.player_id AND pr.resource_id=(SELECT resource_id FROM "resource" WHERE "resource".resource_name = 'Food')) AS "Food amount",
           (SELECT amount FROM playerresource AS pr WHERE pr.player_id = player.player_id AND pr.resource_id=(SELECT resource_id FROM "resource" WHERE "resource".resource_name = 'Stone')) AS "Stone amount"
    FROM player;

CREATE VIEW player_currencies AS
    SELECT player.player_name as "Name",
           (SELECT amount FROM playercurrency AS pc WHERE pc.player_id = player.player_id AND pc.currency_id=(SELECT currency_id FROM currency WHERE currency.currency_name = 'Gold')) AS "Gold amount",
           (SELECT amount FROM playercurrency AS pc WHERE pc.player_id = player.player_id AND pc.currency_id=(SELECT currency_id FROM currency WHERE currency.currency_name = 'Ethereal Silver')) AS "Ethereal silver amount",
           (SELECT amount FROM playercurrency AS pc WHERE pc.player_id = player.player_id AND pc.currency_id=(SELECT currency_id FROM currency WHERE currency.currency_name = 'Diamonds')) AS "Diamdons amount"
    FROM player;
