-- Topology tables for demo cloud + marker for loaded profile.

CREATE TABLE IF NOT EXISTS os_availability_zones (
    name text PRIMARY KEY,
    zone_state jsonb NOT NULL DEFAULT '{"available": true}'::jsonb
);

CREATE TABLE IF NOT EXISTS os_hypervisors (
    id integer PRIMARY KEY,
    hypervisor_hostname text NOT NULL UNIQUE,
    state text NOT NULL DEFAULT 'up',
    status text NOT NULL DEFAULT 'enabled',
    hypervisor_type text NOT NULL DEFAULT 'QEMU',
    hypervisor_version integer NOT NULL DEFAULT 201000,
    host_ip text,
    vcpus integer NOT NULL DEFAULT 64,
    vcpus_used integer NOT NULL DEFAULT 0,
    memory_mb integer NOT NULL DEFAULT 262144,
    memory_mb_used integer NOT NULL DEFAULT 0,
    local_gb integer NOT NULL DEFAULT 2000,
    local_gb_used integer NOT NULL DEFAULT 0,
    running_vms integer NOT NULL DEFAULT 0,
    service_host text,
    availability_zone text REFERENCES os_availability_zones(name),
    created_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS os_aggregates (
    id integer PRIMARY KEY,
    name text NOT NULL UNIQUE,
    availability_zone text,
    hosts jsonb NOT NULL DEFAULT '[]'::jsonb,
    metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
    created_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS os_compute_services (
    id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "binary" text NOT NULL,
    host text NOT NULL,
    zone text,
    status text NOT NULL DEFAULT 'enabled',
    state text NOT NULL DEFAULT 'up',
    updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS os_demo_meta (
    key text PRIMARY KEY,
    value text NOT NULL,
    updated_at timestamptz NOT NULL DEFAULT now()
);

-- Extra columns for richer server/volume demo metadata (idempotent).
ALTER TABLE os_servers ADD COLUMN IF NOT EXISTS availability_zone text;
ALTER TABLE os_servers ADD COLUMN IF NOT EXISTS host text;
ALTER TABLE os_volumes ADD COLUMN IF NOT EXISTS description text NOT NULL DEFAULT '';
