create sequence if not exists accounts_low_seq as integer start with 1 minvalue 1;

create table if not exists accounts (
    account_high        integer     not null,
    account_low         integer     not null,
    pass_token          text        not null,
    created_at          bigint      not null,
    last_seen_at        bigint      not null,
    session_count       integer     not null default 0,
    play_time_seconds   integer     not null default 0,
    banned              boolean     not null default false,
    primary key (account_high, account_low)
);

create table if not exists players (
    account_high        integer     not null,
    account_low         integer     not null,
    name                text        not null,
    name_set_by_user    boolean     not null default false,
    exp_level           integer     not null,
    exp_points          integer     not null,
    gold                integer     not null,
    diamonds            integer     not null,
    free_diamonds       integer     not null,
    trophies            integer     not null,
    arena_table         integer     not null,
    arena_instance      integer     not null,
    arena_name          text,
    gold_resource_table integer     not null,
    gold_resource_instance integer  not null,
    gold_resource_name  text,
    chest_slot_count    integer     not null,
    battle_count        integer     not null default 0,
    win_count           integer     not null default 0,
    lose_count          integer     not null default 0,
    npc_win_count       integer     not null default 0,
    npc_lose_count      integer     not null default 0,
    three_crown_wins    integer     not null default 0,
    tutorials_finished  boolean     not null default false,
    chest_id_counter    integer     not null default 0,
    free_chest_collect_count integer not null default 1,
    crowns_towards_crown_chest integer not null default 0,
    updated_at          bigint      not null default 0,
    primary key (account_high, account_low),
    foreign key (account_high, account_low) references accounts (account_high, account_low) on delete cascade
);

create table if not exists player_cards (
    account_high        integer     not null,
    account_low         integer     not null,
    holder              smallint    not null,
    position            integer     not null,
    card_table          integer     not null,
    card_instance       integer     not null,
    card_name           text,
    level_index         integer     not null default 0,
    count               integer     not null default 0,
    primary key (account_high, account_low, holder, position),
    foreign key (account_high, account_low) references players (account_high, account_low) on delete cascade
);

create index if not exists player_cards_account_idx
    on player_cards (account_high, account_low);

create table if not exists player_chests (
    account_high        integer     not null,
    account_low         integer     not null,
    slot_index          integer     not null,
    chest_table         integer     not null,
    chest_instance      integer     not null,
    chest_name          text,
    chest_id            integer     not null,
    source              integer     not null default 0,
    unlocked            boolean     not null default false,
    claimed             boolean     not null default false,
    remaining_ticks     integer     not null default 0,
    total_ticks         integer     not null default 0,
    end_timestamp       integer     not null default -1,
    primary key (account_high, account_low, slot_index),
    foreign key (account_high, account_low) references players (account_high, account_low) on delete cascade
);

create table if not exists shop_purchases (
    id                  bigserial   primary key,
    account_high        integer     not null,
    account_low         integer     not null,
    offer_id            integer     not null,
    cost_table          integer     not null,
    cost_instance       integer     not null,
    cost_count          integer     not null,
    give_table          integer     not null,
    give_instance       integer     not null,
    give_count          integer     not null,
    purchased_at        bigint      not null
);

create index if not exists shop_purchases_account_idx
    on shop_purchases (account_high, account_low, purchased_at desc);
