-- Build a Zoo web: accounts and zoos
-- Run once against your PostgreSQL (e.g. psql -f server/schema.sql)

CREATE TABLE IF NOT EXISTS accounts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  public_key TEXT UNIQUE NOT NULL,
  pseudo TEXT UNIQUE NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  last_seen_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS zoos (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  account_id UUID REFERENCES accounts(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  x NUMERIC NOT NULL,
  y NUMERIC NOT NULL,
  is_bot BOOLEAN NOT NULL DEFAULT false,
  animal_weights JSONB NOT NULL DEFAULT '{}',
  game_state JSONB,
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  UNIQUE(account_id)  -- only one zoo per account; bots have account_id NULL
);

CREATE INDEX IF NOT EXISTS zoos_account_id ON zoos(account_id);
CREATE INDEX IF NOT EXISTS zoos_is_bot ON zoos(is_bot);

-- One row: map dimensions and min zoos for density
CREATE TABLE IF NOT EXISTS map_config (
  key TEXT PRIMARY KEY,
  value JSONB NOT NULL
);

INSERT INTO map_config (key, value) VALUES
  ('params', '{"mapWidth": 100, "mapHeight": 100, "minZoos": 5}')
ON CONFLICT (key) DO NOTHING;
