CREATE TABLE IF NOT EXISTS business_terms_v015 (
 id varchar(36) PRIMARY KEY, name varchar(180) UNIQUE NOT NULL, definition text NOT NULL,
 synonyms json NOT NULL DEFAULT '[]', domain varchar(100) NOT NULL DEFAULT 'corporate', classification varchar(40) NOT NULL DEFAULT 'internal',
 owner_id varchar(36) NOT NULL REFERENCES users(id), steward_id varchar(36) REFERENCES users(id), status varchar(30) NOT NULL DEFAULT 'draft',
 examples json NOT NULL DEFAULT '[]', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS semantic_models_v015 (
 id varchar(36) PRIMARY KEY, name varchar(180) UNIQUE NOT NULL, description text NOT NULL DEFAULT '', domain varchar(100) NOT NULL DEFAULT 'corporate',
 dataset_id varchar(36) REFERENCES datasets(id) ON DELETE SET NULL, business_object varchar(120) NOT NULL, source_mapping json NOT NULL DEFAULT '{}',
 default_filters json NOT NULL DEFAULT '{}', status varchar(30) NOT NULL DEFAULT 'draft', version integer NOT NULL DEFAULT 1,
 owner_id varchar(36) NOT NULL REFERENCES users(id), approved_by varchar(36) REFERENCES users(id), approved_at timestamptz,
 created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS semantic_metrics_v015 (
 id varchar(36) PRIMARY KEY, model_id varchar(36) NOT NULL REFERENCES semantic_models_v015(id) ON DELETE CASCADE,
 name varchar(180) NOT NULL, label varchar(180) NOT NULL, description text NOT NULL DEFAULT '', expression text NOT NULL,
 aggregation varchar(30) NOT NULL DEFAULT 'sum', data_type varchar(30) NOT NULL DEFAULT 'number', format varchar(80) NOT NULL DEFAULT '',
 certified boolean NOT NULL DEFAULT false, owner_id varchar(36) NOT NULL REFERENCES users(id), created_at timestamptz NOT NULL DEFAULT now(), UNIQUE(model_id,name));
CREATE TABLE IF NOT EXISTS semantic_dimensions_v015 (
 id varchar(36) PRIMARY KEY, model_id varchar(36) NOT NULL REFERENCES semantic_models_v015(id) ON DELETE CASCADE,
 name varchar(180) NOT NULL, label varchar(180) NOT NULL, expression text NOT NULL, data_type varchar(30) NOT NULL DEFAULT 'string',
 hierarchy json NOT NULL DEFAULT '[]', hidden boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE(model_id,name));
CREATE TABLE IF NOT EXISTS knowledge_graph_nodes_v015 (
 id varchar(36) PRIMARY KEY, node_type varchar(80) NOT NULL, external_id varchar(180) NOT NULL, label varchar(250) NOT NULL,
 properties json NOT NULL DEFAULT '{}', domain varchar(100) NOT NULL DEFAULT 'corporate', created_at timestamptz NOT NULL DEFAULT now(), UNIQUE(node_type,external_id));
CREATE TABLE IF NOT EXISTS knowledge_graph_edges_v015 (
 id varchar(36) PRIMARY KEY, source_node_id varchar(36) NOT NULL REFERENCES knowledge_graph_nodes_v015(id) ON DELETE CASCADE,
 target_node_id varchar(36) NOT NULL REFERENCES knowledge_graph_nodes_v015(id) ON DELETE CASCADE, relation_type varchar(100) NOT NULL,
 properties json NOT NULL DEFAULT '{}', created_at timestamptz NOT NULL DEFAULT now(), UNIQUE(source_node_id,target_node_id,relation_type));
CREATE TABLE IF NOT EXISTS semantic_permissions_v015 (
 id varchar(36) PRIMARY KEY, model_id varchar(36) NOT NULL REFERENCES semantic_models_v015(id) ON DELETE CASCADE,
 subject_type varchar(30) NOT NULL DEFAULT 'role', subject_value varchar(180) NOT NULL, object_type varchar(30) NOT NULL DEFAULT 'model',
 object_name varchar(180) NOT NULL DEFAULT '*', action varchar(30) NOT NULL DEFAULT 'read', row_filter json NOT NULL DEFAULT '{}',
 masking json NOT NULL DEFAULT '{}', effect varchar(10) NOT NULL DEFAULT 'allow', created_by varchar(36) NOT NULL REFERENCES users(id), created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS semantic_query_audit_v015 (
 id varchar(36) PRIMARY KEY, user_id varchar(36) NOT NULL REFERENCES users(id), model_id varchar(36) REFERENCES semantic_models_v015(id) ON DELETE SET NULL,
 query json NOT NULL DEFAULT '{}', resolved_plan json NOT NULL DEFAULT '{}', status varchar(30) NOT NULL DEFAULT 'success', error text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now());
CREATE INDEX IF NOT EXISTS ix_business_terms_domain_v015 ON business_terms_v015(domain);
CREATE INDEX IF NOT EXISTS ix_semantic_models_domain_v015 ON semantic_models_v015(domain);
CREATE INDEX IF NOT EXISTS ix_kg_nodes_label_v015 ON knowledge_graph_nodes_v015(label);
CREATE INDEX IF NOT EXISTS ix_semantic_query_audit_created_v015 ON semantic_query_audit_v015(created_at DESC);
