-- DRY-RUN ONLY. NOT APPLIED IN STEP_044B.
CREATE TABLE public.peer_egress_leases (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    peer_id uuid NOT NULL REFERENCES public.peers(id) ON DELETE CASCADE,
    tunnel_ip inet NOT NULL,
    egress_class text NOT NULL CHECK (egress_class IN ('cs1','cs2','cs3','cs4','cs5')),
    assigned_at timestamptz NOT NULL DEFAULT now(),
    last_activity_at timestamptz,
    last_rx_packets bigint NOT NULL DEFAULT 0,
    last_tx_packets bigint NOT NULL DEFAULT 0,
    last_rx_bytes bigint NOT NULL DEFAULT 0,
    last_tx_bytes bigint NOT NULL DEFAULT 0,
    state text NOT NULL DEFAULT 'idle' CHECK (state IN ('active','idle','expired')),
    reason text NOT NULL DEFAULT 'bootstrap',
    cooldown_until timestamptz,
    updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX peer_egress_leases_peer_id_uq
ON public.peer_egress_leases(peer_id);

CREATE INDEX peer_egress_leases_class_state_idx
ON public.peer_egress_leases(egress_class, state);

CREATE INDEX peer_egress_leases_tunnel_ip_idx
ON public.peer_egress_leases(tunnel_ip);

