CREATE TYPE public.cluster_status AS ENUM ('active', 'disabled', 'error');

CREATE TABLE public.clusters (
	id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
	name text NOT NULL,
	api_endpoint text NOT NULL,
	tls_fingerprint text,
	encrypted_token bytea NOT NULL,
	token_id text NOT NULL,
	status public.cluster_status NOT NULL DEFAULT 'active',
	created_at timestamptz NOT NULL DEFAULT now(),
	updated_at timestamptz NOT NULL DEFAULT now(),
	CONSTRAINT clusters_name_not_blank_check CHECK (btrim(name) <> ''),
	CONSTRAINT clusters_api_endpoint_not_blank_check CHECK (btrim(api_endpoint) <> ''),
	CONSTRAINT clusters_token_id_not_blank_check CHECK (btrim(token_id) <> ''),
	CONSTRAINT clusters_api_endpoint_protocol_check CHECK (api_endpoint ~ '^https://')
);

CREATE UNIQUE INDEX clusters_api_endpoint_key ON public.clusters (api_endpoint);
CREATE INDEX clusters_status_idx ON public.clusters (status);
