create table if not exists lottery_rounds (
  id text primary key,
  status text not null,
  pool_minor bigint not null default 0 check (pool_minor >= 0),
  winner_user_id text references users(id),
  winner_account_id text references accounts(id),
  draw_at timestamptz not null,
  created_at timestamptz not null,
  completed_at timestamptz
);

create table if not exists lottery_entries (
  id text primary key,
  round_id text not null references lottery_rounds(id) on delete cascade,
  user_id text not null references users(id) on delete cascade,
  account_id text not null references accounts(id) on delete cascade,
  amount_minor bigint not null check (amount_minor > 0),
  created_at timestamptz not null
);

create index if not exists idx_lottery_rounds_status_created_at on lottery_rounds(status, created_at desc);
create index if not exists idx_lottery_entries_round_id on lottery_entries(round_id, created_at asc);
create index if not exists idx_lottery_entries_user_id on lottery_entries(user_id, created_at desc);
