create table users (
    id uuid primary key default gen_random_uuid(),
    wallet varchar(64),
    name varchar(32) not null
);

create table resource (
    id serial primary key,
    name varchar(32) not null unique
);

create table game_constants(
    key text primary key,
    value float4 not null
);

create table store_item(
    id integer primary key,
    name text not null,
    description text not null,
    price integer not null,
    image_name text not null,
    claim_amount integer not null,
    completion_time_in_mins integer not null
);

create table upgrade_item(
    tier integer not null,
    store_item_id integer not null,
    price text not null,
    claim_boost integer not null,
    constraint fk_store_item_upgrade_item foreign key(store_item_id)
        references store_item(id) on delete cascade,
    primary key (store_item_id, tier)
);

create table staking_source_item (
    id integer primary key,
    name text not null,
    description text not null,
    image_name text not null
);

create table staking_source (
    id uuid primary key default gen_random_uuid(),
    name varchar not null,
    description varchar not null,
    image_name text not null,
    user_id uuid not null,
    address varchar(128) not null,
    created_at timestamp with time zone default timezone('utc', now()),
    constraint fk_user_staking_source foreign key(user_id)
        references users(id) on delete cascade
);

create table resource_well (
    id uuid primary key default gen_random_uuid(),
    resource_id integer not null,
    source_id uuid not null,
    supply integer not null,
    constraint fk_sid_resource_well foreign key(source_id)
        references staking_source(id) on delete cascade,
  constraint fk_rid_resource_well foreign key(resource_id)
        references resource(id)
);

create table inventory_item (
    id uuid primary key default gen_random_uuid(),
    user_id uuid not null,
    tier integer not null default 0,
    store_item_id int not null,
    created_at timestamp with time zone default timezone('utc', now()),
    constraint fk_user_inventory_item foreign key(user_id)
        references users(id) on delete cascade,
    constraint fk_store_item_inventory_item foreign key(store_item_id)
        references store_item(id) on delete cascade,
    constraint uk_user_store_item unique (user_id, store_item_id)
);

create table upgrade_event (
    id uuid primary key default gen_random_uuid(),
    inventory_item_id uuid not null,
    created_at timestamp with time zone default timezone('utc', now()),
    constraint fk_iid_upgrade_event foreign key(inventory_item_id)
        references inventory_item(id) on delete cascade
);

create table staking_event (
    id uuid primary key default gen_random_uuid(),
    user_id uuid not null,
    well_id uuid not null,
    inventory_item_id uuid not null,
    duration_in_mins integer not null,
    stake_amount integer not null,
    created_at timestamp with time zone default timezone('utc', now()),
    constraint fk_user_staking_event foreign key(user_id)
        references users(id) on delete cascade,
    constraint fk_wid_staking_event foreign key(well_id)
        references resource_well(id),
    constraint fk_iiid_staking_event foreign key(inventory_item_id)
        references inventory_item(id) on delete cascade
);

create table claim_event (
    id uuid primary key default gen_random_uuid(),
    staking_event_id uuid not null,
    claim_amount integer not null,
    created_at timestamp with time zone default timezone('utc', now()),
    constraint fk_se_claim_event foreign key(staking_event_id)
        references staking_event(id) on delete cascade
);

create table bank_account (
    id uuid primary key default gen_random_uuid(),
    user_id uuid not null,
    balance integer not null default 0 check (balance >= 0),
    constraint fk_user_bank_account foreign key(user_id)
        references users(id) on delete cascade
);

create table resource_account (
    id uuid primary key default gen_random_uuid(),
    resource_id integer not null,
    user_id uuid not null,
    balance integer not null default 0 check (balance >= 0),
    constraint fk_user_resource_account foreign key(user_id)
        references users(id) on delete cascade,
    constraint fk_rid_resource_account foreign key(resource_id)
        references resource(id)
);
