CREATE TABLE card_set(
set_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
set_name text UNIQUE,
set_code text UNIQUE,
set_series text,
set_total_cards int,
set_release_date date,
set_logo_url text,
set_symbol_url text
);

CREATE TABLE energy_type(
energy_type_id int generated always as identity PRIMARY KEY,
energy_type_name text UNIQUE,
energy_type_icon_url text
);

CREATE TABLE pokemon_sub_type(
sub_type_id int generated always as identity PRIMARY KEY,
sub_type_name text UNIQUE,
sub_type_default_prize_count int DEFAULT 1,
sub_type_global_rule_text text
);

CREATE TABLE ability_type(
ability_type_id int generated always as identity PRIMARY KEY,
ability_type_name text UNIQUE
);

CREATE TABLE attack_name(
attack_name_id int generated always as identity PRIMARY KEY,
attack_name_title text UNIQUE
);

CREATE TABLE trainer_printed_type(
  printed_type_id int generated always as identity PRIMARY KEY,
  printed_type_name text UNIQUE
);

CREATE TABLE game_mechanic(
mechanic_id int PRIMARY KEY,
mechanic_name text UNIQUE
);

CREATE TABLE pokemon_species(
pokemon_pokedex_number int PRIMARY KEY,
species_eng_name text UNIQUE,
species_generation int,
species_height_m NUMERIC CHECK (species_height_m > 0.0),
species_weight_kg NUMERIC CHECK (species_weight_kg > 0.0)
);

CREATE TABLE pokedex_entry(
entry_id int generated always as identity PRIMARY KEY,
entry_pokedex_number int REFERENCES pokemon_species(pokemon_pokedex_number),
entry_source_game text,
entry_description text
);

CREATE TABLE card(
card_id int generated always as identity PRIMARY KEY,
card_number text NOT NULL,
card_set_id int REFERENCES card_set(set_id) NOT NULL,
card_name text,
card_rarity text,
card_type text CHECK (card_type IN ('Pokemon', 'Trainer', 'Energy')) NOT NULL,
card_image_url text,
unique(card_set_id, card_number)
);

CREATE TABLE pokemon_card(
pokemon_card_id int PRIMARY KEY REFERENCES card(card_id),
pokemon_pokedex_number int not null REFERENCES pokemon_species(pokemon_pokedex_number),
pokemon_pokedex_entry_id int REFERENCES pokedex_entry(entry_id),
pokemon_hp int CHECK (pokemon_hp % 10 = 0),
pokemon_stage text NOT NULL,
pokemon_evolves_from text,
pokemon_retreat_cost int,
pokemon_sub_type_id int REFERENCES pokemon_sub_type(sub_type_id),
pokemon_category text,
pokemon_printed_height text,
pokemon_printed_weight text
);

CREATE TABLE trainer_card (
trainer_card_id int PRIMARY KEY REFERENCES card(card_id),
trainer_printed_type_id int REFERENCES trainer_printed_type(printed_type_id),
trainer_effect_text text,
trainer_is_ace_spec bool
);

CREATE TABLE energy_card (
energy_card_id int PRIMARY KEY REFERENCES card(card_id),
energy_is_special bool,
energy_main_type_id int REFERENCES energy_type(energy_type_id),
energy_effect_text text
);

CREATE TABLE pokemon_ability(
ability_pokemon_card_id int PRIMARY KEY REFERENCES pokemon_card(pokemon_card_id),
ability_type_id int REFERENCES ability_type(ability_type_id),
ability_name text,
ability_effect_text text
);

CREATE TABLE pokemon_attack_link(
link_id int generated always as identity PRIMARY KEY,
link_pokemon_card_id int REFERENCES pokemon_card(pokemon_card_id),
link_attack_name_id int REFERENCES attack_name(attack_name_id),
link_damage_value text,
link_effect_text text,
unique(link_pokemon_card_id, link_attack_name_id)
);

CREATE TABLE attack_energy_cost(
cost_id int generated always as identity PRIMARY KEY,
cost_link_id int REFERENCES pokemon_attack_link(link_id),
cost_energy_type_id int REFERENCES energy_type(energy_type_id),
cost_quantity int DEFAULT 1
);

CREATE TABLE pokemon_attribute(
attribute_id int generated always as identity PRIMARY KEY,
attribute_pokemon_card_id int REFERENCES pokemon_card(pokemon_card_id),
attribute_energy_type_id int REFERENCES energy_type(energy_type_id),
attribute_kind text,
attribute_modifier_value text,
unique(attribute_pokemon_card_id, attribute_energy_type_id, attribute_kind)
);

CREATE TABLE trainer_mechanic_link(
mechanic_link_trainer_card_id int REFERENCES trainer_card(trainer_card_id),
mechanic_link_mechanic_id int REFERENCES game_mechanic(mechanic_id),
primary key(mechanic_link_trainer_card_id, mechanic_link_mechanic_id)
);

insert into card_set (set_name, set_code, set_series, set_total_cards, set_release_date) values
	('XY Базовый набор', 'XY1', 'XY', 146, '2014-02-05'),
	('XY Огненная вспышка', 'XY2', 'XY', 106, '2014-05-07'),
	('XY Яростный кулак', 'XY3', 'XY', 111, '2014-08-13'),
	('XY Призрачные силы', 'XY4', 'XY', 119, '2014-11-05'),
	('XY Первобытная дуэль', 'XY5', 'XY', 160, '2015-02-04'),
	('XY Грохочущие небеса', 'XY6', 'XY', 108, '2015-05-06'),
	('XY Древние истоки', 'XY7', 'XY', 98, '2015-08-12'),
	('XY Прорыв', 'XY8', 'XY', 162, '2015-11-04'),
	('XY Тайное превосходство', 'XY9', 'XY', 122, '2016-02-03'),
	('XY Столкновение судеб', 'XY10', 'XY', 124, '2016-05-02');

CREATE TABLE staging_furious_fists (
    card_id        INTEGER,
    card_number    TEXT,
    card_name      TEXT,
    card_rarity    TEXT,
    card_type      TEXT,
    card_image_url TEXT
);

INSERT INTO card (
    card_number, card_set_id, card_name,
    card_rarity, card_type, card_image_url)
SELECT
    s.card_number,
    (SELECT set_id FROM card_set WHERE set_code = 'XY3'),
    s.card_name,
    s.card_rarity,
    s.card_type,
    s.card_image_url
FROM staging_furious_fists s
WHERE s.card_type IN ('Pokemon', 'Trainer', 'Energy');

DROP TABLE staging_furious_fists;

INSERT INTO energy_type (energy_type_name, energy_type_icon_url) VALUES
    ('Grass',     NULL),
    ('Fire',      NULL),
    ('Water',     NULL),
    ('Lightning', NULL),
    ('Psychic',   NULL),
    ('Fighting',  NULL),
    ('Darkness',  NULL),
    ('Metal',     NULL),
    ('Fairy',     NULL),
    ('Dragon',    NULL),
    ('Colorless', NULL);

INSERT INTO pokemon_sub_type (
    sub_type_name, sub_type_default_prize_count, sub_type_global_rule_text
) VALUES
    ('Normal', 1, NULL),
    ('EX',     2, 'When a Pokémon-EX is Knocked Out, your opponent takes 2 Prize cards.'),
    ('MEGA',   2, 'When a MEGA Evolution Pokémon is Knocked Out, your opponent takes 2 Prize cards.'),
    ('BREAK',  1, NULL);

INSERT INTO ability_type (ability_type_name) VALUES
    ('Ability'),
    ('Poké-Power'),
    ('Poké-Body');

INSERT INTO trainer_printed_type (printed_type_name) VALUES
    ('Item'),
    ('Supporter'),
    ('Stadium'),
    ('Pokémon Tool');

INSERT INTO attack_name (attack_name_title) VALUES
    ('Nuzzle'),
    ('Quick Attack'),
    ('Circle Circuit'),
    ('Thunder Shock');

INSERT INTO pokemon_species (
    pokemon_pokedex_number, species_eng_name,
    species_generation, species_height_m, species_weight_kg
) VALUES
    (25,  'Pikachu',  1, 0.4, 6.0),
    (26,  'Raichu',   1, 0.8, 30.0),
    (448, 'Lucario',  4, 1.2, 54.0);

INSERT INTO pokemon_card (
    pokemon_card_id,
    pokemon_pokedex_number,
    pokemon_pokedex_entry_id,
    pokemon_hp,
    pokemon_stage,
    pokemon_evolves_from,
    pokemon_retreat_cost,
    pokemon_sub_type_id,
    pokemon_category,
    pokemon_printed_height,
    pokemon_printed_weight
) VALUES
    (28, 25, NULL, 60, 'Basic', NULL, 1, 1, 'Мышонок', '0.4 м', '6.0 кг');

INSERT INTO pokemon_attack_link (
    link_pokemon_card_id, link_attack_name_id,
    link_damage_value, link_effect_text
) VALUES
    (28, 1, '0', 'Your opponent''s Active Pokémon is now Paralyzed.');

INSERT INTO pokemon_attack_link (
    link_pokemon_card_id, link_attack_name_id,
    link_damage_value, link_effect_text
) VALUES
    (28, 2, '20', 'Flip a coin. If heads, this attack does 20 more damage.');

INSERT INTO attack_energy_cost (cost_link_id, cost_energy_type_id, cost_quantity)
VALUES
    (1, 4, 1);

INSERT INTO attack_energy_cost (cost_link_id, cost_energy_type_id, cost_quantity)
VALUES
    (2, 4, 1),
    (2, 11, 1);

INSERT INTO pokemon_attribute (
    attribute_pokemon_card_id, attribute_energy_type_id,
    attribute_kind, attribute_modifier_value
) VALUES
    (28, 6, 'Weakness', 'x2');

INSERT INTO pokemon_attribute (
    attribute_pokemon_card_id, attribute_energy_type_id,
    attribute_kind, attribute_modifier_value
) VALUES
    (28, 8, 'Resistance', '-20');

INSERT INTO trainer_card (
    trainer_card_id, trainer_printed_type_id,
    trainer_effect_text, trainer_is_ace_spec
) VALUES
    (88, 2, 'Switch your Active Pokémon with 1 of your Benched Pokémon.', FALSE);

INSERT INTO energy_card (
    energy_card_id, energy_is_special,
    energy_main_type_id, energy_effect_text
) VALUES
    (103, TRUE, NULL, 'This card provides 1 Fighting energy. When attached, ...');

SELECT c.card_name, pc.pokemon_hp, pc.pokemon_stage
FROM card c
INNER JOIN pokemon_card pc ON c.card_id = pc.pokemon_card_id
ORDER BY pc.pokemon_hp DESC, c.card_name;

SELECT c.card_name, c.card_type, an.attack_name_title, pal.link_damage_value
FROM card c
LEFT JOIN pokemon_attack_link pal ON c.card_id = pal.link_pokemon_card_id
LEFT JOIN attack_name an ON pal.link_attack_name_id = an.attack_name_id
ORDER BY c.card_type, c.card_name, an.attack_name_title;

SELECT c.card_name, an.attack_name_title, pal.link_damage_value, et.energy_type_name, cost_quantity
FROM card c
JOIN pokemon_attack_link pal ON c.card_id = pal.link_pokemon_card_id
JOIN attack_name an ON pal.link_attack_name_id = an.attack_name_id
JOIN attack_energy_cost aec ON aec.cost_link_id = pal.link_id
JOIN energy_type et ON et.energy_type_id = aec.cost_energy_type_id
ORDER BY card_name, attack_name_title, energy_type_name;

SELECT c.card_name, c.card_type, pc.pokemon_hp
FROM card c
LEFT JOIN pokemon_card pc ON pc.pokemon_card_id = c.card_id
AND pc.pokemon_hp > 80;

SELECT c.card_name, c.card_type
FROM card c
LEFT JOIN pokemon_card pc ON c.card_id = pc.pokemon_card_id
LEFT JOIN trainer_card tc ON c.card_id = tc.trainer_card_id
LEFT JOIN energy_card ec ON c.card_id = ec.energy_card_id
WHERE pc.pokemon_card_id IS NULL
  AND tc.trainer_card_id IS NULL
  AND ec.energy_card_id IS NULL;

SELECT c.card_name, cs.set_name
FROM card c
FULL OUTER JOIN card_set cs ON c.card_set_id = cs.set_id;