-- public.chat_messages definition

-- Drop table

-- DROP TABLE public.chat_messages;

CREATE TABLE public.chat_messages (
	id serial4 NOT NULL,
	thread_id varchar(255) NOT NULL,
	checkpoint_id varchar(255) NOT NULL,
	message_index int4 NOT NULL,
	"role" varchar(20) NOT NULL,
	"content" text NOT NULL,
	injected_content text NULL,
	has_files bool DEFAULT false NULL,
	metadata jsonb NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	"name" varchar(255) NULL,
	CONSTRAINT chat_messages_pkey PRIMARY KEY (id),
	CONSTRAINT uk_checkpoint_message UNIQUE (checkpoint_id, message_index)
);
CREATE INDEX idx_chat_messages_checkpoint_id ON public.chat_messages USING btree (checkpoint_id);
CREATE INDEX idx_chat_messages_content_search ON public.chat_messages USING gin (to_tsvector('simple'::regconfig, content));
CREATE INDEX idx_chat_messages_has_files ON public.chat_messages USING btree (has_files);
CREATE INDEX idx_chat_messages_metadata ON public.chat_messages USING gin (metadata);
CREATE INDEX idx_chat_messages_role ON public.chat_messages USING btree (role);
CREATE INDEX idx_chat_messages_thread_created ON public.chat_messages USING btree (thread_id, created_at DESC);
CREATE INDEX idx_chat_messages_thread_id ON public.chat_messages USING btree (thread_id);


-- public.checkpoint_blobs definition

-- Drop table

-- DROP TABLE public.checkpoint_blobs;

CREATE TABLE public.checkpoint_blobs (
	thread_id text NOT NULL,
	checkpoint_ns text DEFAULT ''::text NOT NULL,
	channel text NOT NULL,
	"version" text NOT NULL,
	"type" text NOT NULL,
	"blob" bytea NULL,
	CONSTRAINT checkpoint_blobs_pkey PRIMARY KEY (thread_id, checkpoint_ns, channel, version)
);
CREATE INDEX checkpoint_blobs_thread_id_idx ON public.checkpoint_blobs USING btree (thread_id);


-- public.checkpoint_migrations definition

-- Drop table

-- DROP TABLE public.checkpoint_migrations;

CREATE TABLE public.checkpoint_migrations (
	v int4 NOT NULL,
	CONSTRAINT checkpoint_migrations_pkey PRIMARY KEY (v)
);


-- public.checkpoint_writes definition

-- Drop table

-- DROP TABLE public.checkpoint_writes;

CREATE TABLE public.checkpoint_writes (
	thread_id text NOT NULL,
	checkpoint_ns text DEFAULT ''::text NOT NULL,
	checkpoint_id text NOT NULL,
	task_id text NOT NULL,
	idx int4 NOT NULL,
	channel text NOT NULL,
	"type" text NULL,
	"blob" bytea NOT NULL,
	task_path text DEFAULT ''::text NOT NULL,
	CONSTRAINT checkpoint_writes_pkey PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id, task_id, idx)
);
CREATE INDEX checkpoint_writes_thread_id_idx ON public.checkpoint_writes USING btree (thread_id);


-- public.checkpoints definition

-- Drop table

-- DROP TABLE public.checkpoints;

CREATE TABLE public.checkpoints (
	thread_id text NOT NULL,
	checkpoint_ns text DEFAULT ''::text NOT NULL,
	checkpoint_id text NOT NULL,
	parent_checkpoint_id text NULL,
	"type" text NULL,
	"checkpoint" jsonb NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	CONSTRAINT checkpoints_pkey PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id)
);
CREATE INDEX checkpoints_thread_id_idx ON public.checkpoints USING btree (thread_id);


-- public.enterprise definition

-- Drop table

-- DROP TABLE public.enterprise;

CREATE TABLE public.enterprise (
	id serial4 NOT NULL,
	"name" varchar(255) NOT NULL,
	code varchar(64) NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
	ai_display_name varchar(128) DEFAULT '智能助手 AI'::character varying NOT NULL,
	CONSTRAINT enterprise_code_key UNIQUE (code),
	CONSTRAINT enterprise_pkey PRIMARY KEY (id)
);


-- public.chat_message_file definition

-- Drop table

-- DROP TABLE public.chat_message_file;

CREATE TABLE public.chat_message_file (
	id serial4 NOT NULL,
	thread_id varchar(255) NOT NULL,
	checkpoint_id varchar(255) NOT NULL,
	message_index int4 NOT NULL,
	file_id int4 NOT NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	CONSTRAINT chat_message_file_pkey PRIMARY KEY (id),
	CONSTRAINT uk_message_file UNIQUE (checkpoint_id, message_index, file_id)
);
CREATE INDEX idx_chat_message_file_checkpoint ON public.chat_message_file USING btree (checkpoint_id, message_index);
CREATE INDEX idx_chat_message_file_file_id ON public.chat_message_file USING btree (file_id);
CREATE INDEX idx_chat_message_file_thread_checkpoint ON public.chat_message_file USING btree (thread_id, checkpoint_id);
CREATE INDEX idx_chat_message_file_thread_id ON public.chat_message_file USING btree (thread_id);


-- public.chat_thread_chunk definition

-- Drop table

-- DROP TABLE public.chat_thread_chunk;

CREATE TABLE public.chat_thread_chunk (
	id serial4 NOT NULL,
	file_id int4 NOT NULL,
	thread_id varchar(255) NOT NULL,
	chunk_index int4 NOT NULL,
	"content" text NOT NULL,
	metadata jsonb NULL,
	vector_id varchar(255) NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	summary text NULL,
	CONSTRAINT chat_thread_chunk_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_chat_thread_chunk_created_at ON public.chat_thread_chunk USING btree (created_at);
CREATE INDEX idx_chat_thread_chunk_file_id ON public.chat_thread_chunk USING btree (file_id);
CREATE INDEX idx_chat_thread_chunk_file_thread ON public.chat_thread_chunk USING btree (file_id, thread_id);
CREATE INDEX idx_chat_thread_chunk_thread_id ON public.chat_thread_chunk USING btree (thread_id);
CREATE INDEX idx_chat_thread_chunk_vector_id ON public.chat_thread_chunk USING btree (vector_id);


-- public.chat_thread_file definition

-- Drop table

-- DROP TABLE public.chat_thread_file;

CREATE TABLE public.chat_thread_file (
	id serial4 NOT NULL,
	thread_id varchar(255) NOT NULL,
	user_id int4 NOT NULL,
	file_name varchar(255) NOT NULL,
	file_path varchar(500) NOT NULL,
	file_size int4 DEFAULT 0 NULL,
	file_type varchar(50) DEFAULT 'pdf'::character varying NULL,
	status varchar(20) DEFAULT 'processing'::character varying NULL,
	chunk_count int4 DEFAULT 0 NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	is_deleted bool DEFAULT false NULL,
	deleted_at timestamptz NULL,
	CONSTRAINT chat_thread_file_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_chat_thread_file_created_at ON public.chat_thread_file USING btree (created_at);
CREATE INDEX idx_chat_thread_file_is_deleted ON public.chat_thread_file USING btree (is_deleted);
CREATE INDEX idx_chat_thread_file_status ON public.chat_thread_file USING btree (status);
CREATE INDEX idx_chat_thread_file_thread_deleted ON public.chat_thread_file USING btree (thread_id, is_deleted);
CREATE INDEX idx_chat_thread_file_thread_id ON public.chat_thread_file USING btree (thread_id);
CREATE INDEX idx_chat_thread_file_thread_user ON public.chat_thread_file USING btree (thread_id, user_id);
CREATE INDEX idx_chat_thread_file_user_id ON public.chat_thread_file USING btree (user_id);
CREATE UNIQUE INDEX uk_chat_thread_file_thread_name_active ON public.chat_thread_file USING btree (thread_id, file_name) WHERE (is_deleted = false);


-- public.chat_threads definition

-- Drop table

-- DROP TABLE public.chat_threads;

CREATE TABLE public.chat_threads (
	id serial4 NOT NULL,
	thread_id varchar(255) NOT NULL,
	user_id int4 NOT NULL,
	title varchar(50) NOT NULL,
	first_query text NOT NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	message_count int4 DEFAULT 1 NULL,
	is_deleted bool DEFAULT false NULL,
	knowledge_base_id int4 NULL,
	novel_graph_id int4 NULL,
	knowledge_graph_id int4 NULL,
	ip varchar(128) NULL,
	CONSTRAINT chat_threads_pkey PRIMARY KEY (id),
	CONSTRAINT uk_thread_id UNIQUE (thread_id)
);
CREATE INDEX idx_chat_threads_created_at ON public.chat_threads USING btree (created_at DESC);
CREATE INDEX idx_chat_threads_knowledge_graph_id ON public.chat_threads USING btree (knowledge_graph_id);
CREATE INDEX idx_chat_threads_novel_graph_id ON public.chat_threads USING btree (novel_graph_id);
CREATE INDEX idx_chat_threads_user_created ON public.chat_threads USING btree (user_id, created_at DESC);
CREATE INDEX idx_chat_threads_user_id ON public.chat_threads USING btree (user_id);


-- public.department definition

-- Drop table

-- DROP TABLE public.department;

CREATE TABLE public.department (
	id serial4 NOT NULL,
	enterprise_id int4 NOT NULL,
	"name" varchar(255) NOT NULL,
	parent_id int4 NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
	leader_user_id int4 NULL,
	CONSTRAINT department_pkey PRIMARY KEY (id),
	CONSTRAINT uq_department_enterprise_name UNIQUE (enterprise_id, name)
);
CREATE INDEX idx_department_enterprise_id ON public.department USING btree (enterprise_id);
CREATE INDEX idx_department_leader_user_id ON public.department USING btree (leader_user_id);


-- public.graphs definition

-- Drop table

-- DROP TABLE public.graphs;

CREATE TABLE public.graphs (
	id serial4 NOT NULL,
	user_id int4 NOT NULL,
	"name" varchar(255) NOT NULL,
	description text NULL,
	csv_file_name varchar(255) NULL,
	node_count int4 DEFAULT 0 NULL,
	edge_count int4 DEFAULT 0 NULL,
	neo4j_graph_id varchar(100) NOT NULL,
	graph_type varchar(20) DEFAULT 'knowledge'::character varying NOT NULL,
	build_status varchar(20) NULL,
	build_error text NULL,
	rag_chunk_count int4 DEFAULT 0 NOT NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	enterprise_id int4 NOT NULL,
	department_id int4 NULL,
	creator_id int4 NOT NULL,
	visibility varchar(32) DEFAULT 'private'::character varying NOT NULL,
	CONSTRAINT ck_graphs_visibility CHECK (((visibility)::text = ANY ((ARRAY['private'::character varying, 'department'::character varying, 'enterprise'::character varying])::text[]))),
	CONSTRAINT graphs_neo4j_graph_id_key UNIQUE (neo4j_graph_id),
	CONSTRAINT graphs_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_graphs_created_at ON public.graphs USING btree (created_at DESC);
CREATE INDEX idx_graphs_creator ON public.graphs USING btree (creator_id);
CREATE INDEX idx_graphs_ent_vis ON public.graphs USING btree (enterprise_id, visibility);
CREATE INDEX idx_graphs_enterprise ON public.graphs USING btree (enterprise_id);
CREATE INDEX idx_graphs_graph_type ON public.graphs USING btree (user_id, graph_type);
CREATE INDEX idx_graphs_neo4j_id ON public.graphs USING btree (neo4j_graph_id);
CREATE INDEX idx_graphs_user_id ON public.graphs USING btree (user_id);


-- public.kb_audit_log definition

-- Drop table

-- DROP TABLE public.kb_audit_log;

CREATE TABLE public.kb_audit_log (
	id serial4 NOT NULL,
	enterprise_id int4 NOT NULL,
	actor_id int4 NOT NULL,
	target_user_id int4 NULL,
	department_id int4 NULL,
	kb_id int4 NULL,
	file_id int4 NULL,
	"action" varchar(50) NOT NULL,
	ip varchar(128) NULL,
	user_agent text NULL,
	metadata jsonb NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
	CONSTRAINT kb_audit_log_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_kb_audit_log_action ON public.kb_audit_log USING btree (action);
CREATE INDEX idx_kb_audit_log_actor_id ON public.kb_audit_log USING btree (actor_id);
CREATE INDEX idx_kb_audit_log_created_at ON public.kb_audit_log USING btree (created_at DESC);
CREATE INDEX idx_kb_audit_log_department_id ON public.kb_audit_log USING btree (department_id);
CREATE INDEX idx_kb_audit_log_ent_dept_created ON public.kb_audit_log USING btree (enterprise_id, department_id, created_at DESC);
CREATE INDEX idx_kb_audit_log_enterprise_id ON public.kb_audit_log USING btree (enterprise_id);
CREATE INDEX idx_kb_audit_log_kb_id ON public.kb_audit_log USING btree (kb_id);
CREATE INDEX idx_kb_audit_log_target_user_id ON public.kb_audit_log USING btree (target_user_id);


-- public.knowledge_base definition

-- Drop table

-- DROP TABLE public.knowledge_base;

CREATE TABLE public.knowledge_base (
	id serial4 NOT NULL,
	user_id int4 NOT NULL,
	"name" varchar(255) NOT NULL,
	description text NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	is_deleted bool DEFAULT false NULL,
	deleted_at timestamptz NULL,
	enterprise_id int4 DEFAULT 1 NOT NULL,
	department_id int4 NULL,
	creator_id int4 NULL,
	visibility varchar(32) DEFAULT 'private'::character varying NOT NULL,
	CONSTRAINT ck_knowledge_base_visibility CHECK (((visibility)::text = ANY ((ARRAY['private'::character varying, 'department'::character varying, 'enterprise'::character varying])::text[]))),
	CONSTRAINT knowledge_base_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_knowledge_base_created_at ON public.knowledge_base USING btree (created_at);
CREATE INDEX idx_knowledge_base_creator ON public.knowledge_base USING btree (creator_id);
CREATE INDEX idx_knowledge_base_ent_vis ON public.knowledge_base USING btree (enterprise_id, visibility) WHERE (is_deleted = false);
CREATE INDEX idx_knowledge_base_enterprise ON public.knowledge_base USING btree (enterprise_id);
CREATE INDEX idx_knowledge_base_is_deleted ON public.knowledge_base USING btree (is_deleted);
CREATE INDEX idx_knowledge_base_user_deleted ON public.knowledge_base USING btree (user_id, is_deleted);
CREATE INDEX idx_knowledge_base_user_id ON public.knowledge_base USING btree (user_id);
CREATE INDEX idx_knowledge_base_user_name ON public.knowledge_base USING btree (user_id, name);
CREATE UNIQUE INDEX uk_user_knowledge_base_name_active ON public.knowledge_base USING btree (user_id, name) WHERE (is_deleted = false);


-- public.knowledge_base_chunk definition

-- Drop table

-- DROP TABLE public.knowledge_base_chunk;

CREATE TABLE public.knowledge_base_chunk (
	id serial4 NOT NULL,
	file_id int4 NOT NULL,
	knowledge_base_id int4 NOT NULL,
	chunk_index int4 NOT NULL,
	"content" text NOT NULL,
	metadata jsonb NULL,
	vector_id varchar(255) NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	summary text NULL,
	CONSTRAINT knowledge_base_chunk_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_kb_chunk_file_id ON public.knowledge_base_chunk USING btree (file_id);
CREATE INDEX idx_kb_chunk_kb_id ON public.knowledge_base_chunk USING btree (knowledge_base_id);
CREATE INDEX idx_kb_chunk_metadata ON public.knowledge_base_chunk USING gin (metadata);
CREATE INDEX idx_kb_chunk_vector_id ON public.knowledge_base_chunk USING btree (vector_id);


-- public.knowledge_base_file definition

-- Drop table

-- DROP TABLE public.knowledge_base_file;

CREATE TABLE public.knowledge_base_file (
	id serial4 NOT NULL,
	knowledge_base_id int4 NOT NULL,
	user_id int4 NOT NULL,
	file_name varchar(255) NOT NULL,
	file_path varchar(500) NOT NULL,
	file_size int8 NOT NULL,
	file_type varchar(50) DEFAULT 'pdf'::character varying NOT NULL,
	status varchar(20) DEFAULT 'processing'::character varying NOT NULL,
	chunk_count int4 DEFAULT 0 NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	is_deleted bool DEFAULT false NULL,
	deleted_at timestamptz NULL,
	CONSTRAINT knowledge_base_file_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_kb_file_created_at ON public.knowledge_base_file USING btree (created_at);
CREATE INDEX idx_kb_file_kb_id ON public.knowledge_base_file USING btree (knowledge_base_id);
CREATE INDEX idx_kb_file_status ON public.knowledge_base_file USING btree (status);
CREATE UNIQUE INDEX idx_kb_file_unique_active ON public.knowledge_base_file USING btree (knowledge_base_id, file_name) WHERE (is_deleted = false);
CREATE INDEX idx_kb_file_user_id ON public.knowledge_base_file USING btree (user_id);


-- public.knowledge_processing_task definition

-- Drop table

-- DROP TABLE public.knowledge_processing_task;

CREATE TABLE public.knowledge_processing_task (
	id serial4 NOT NULL,
	user_id int4 NOT NULL,
	knowledge_base_id int4 NOT NULL,
	task_name varchar(255) NOT NULL,
	instruction text NOT NULL,
	file_ids _int4 NOT NULL,
	task_type varchar(50) NOT NULL,
	status varchar(20) DEFAULT 'pending'::character varying NULL,
	"result" text NULL,
	result_file_url text NULL,
	error_message text NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	started_at timestamptz NULL,
	completed_at timestamptz NULL,
	CONSTRAINT knowledge_processing_task_pkey PRIMARY KEY (id)
);
CREATE INDEX idx_kb_processing_created_at ON public.knowledge_processing_task USING btree (created_at DESC);
CREATE INDEX idx_kb_processing_kb_id ON public.knowledge_processing_task USING btree (knowledge_base_id);
CREATE INDEX idx_kb_processing_status ON public.knowledge_processing_task USING btree (status);
CREATE INDEX idx_kb_processing_user_id ON public.knowledge_processing_task USING btree (user_id);
CREATE INDEX idx_kb_processing_user_status ON public.knowledge_processing_task USING btree (user_id, status);


-- public.user_list definition

-- Drop table

-- DROP TABLE public.user_list;

CREATE TABLE public.user_list (
	id serial4 NOT NULL,
	username varchar(50) NOT NULL,
	email varchar(255) NOT NULL,
	phone varchar(255) NOT NULL,
	github_id varchar(100) NULL,
	github_username varchar(100) NULL,
	github_avatar_url text NULL,
	github_access_token text NULL,
	github_token_expires_at timestamptz NULL,
	display_name varchar(100) NULL,
	avatar_url text NULL,
	bio text NULL,
	is_active bool DEFAULT true NULL,
	email_verified bool DEFAULT false NULL,
	created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
	last_login_at timestamptz NULL,
	hashed_password varchar(255) NULL,
	is_search bool DEFAULT false NULL,
	is_reasoner bool DEFAULT false NULL,
	enterprise_id int4 DEFAULT 1 NOT NULL,
	department_id int4 NULL,
	"role" varchar(32) DEFAULT 'employee'::character varying NOT NULL,
	is_first_login bool DEFAULT true NOT NULL,
	allow_kb_upload bool DEFAULT true NOT NULL,
	CONSTRAINT unique_email UNIQUE (email),
	CONSTRAINT unique_github_id UNIQUE (github_id),
	CONSTRAINT user_list_phone_key UNIQUE (phone),
	CONSTRAINT user_list_pkey PRIMARY KEY (id),
	CONSTRAINT user_list_username_key UNIQUE (username)
);
CREATE INDEX idx_user_list_created_at ON public.user_list USING btree (created_at);
CREATE INDEX idx_user_list_department_id ON public.user_list USING btree (department_id);
CREATE INDEX idx_user_list_email ON public.user_list USING btree (email);
CREATE INDEX idx_user_list_enterprise_id ON public.user_list USING btree (enterprise_id);
CREATE INDEX idx_user_list_github_id ON public.user_list USING btree (github_id);
CREATE INDEX idx_user_list_role ON public.user_list USING btree (role);
CREATE INDEX idx_user_list_username ON public.user_list USING btree (username);


-- public.chat_message_file foreign keys

ALTER TABLE public.chat_message_file ADD CONSTRAINT fk_chat_message_file_file FOREIGN KEY (file_id) REFERENCES public.chat_thread_file(id) ON DELETE CASCADE;


-- public.chat_thread_chunk foreign keys

ALTER TABLE public.chat_thread_chunk ADD CONSTRAINT fk_chat_thread_chunk_file FOREIGN KEY (file_id) REFERENCES public.chat_thread_file(id) ON DELETE CASCADE;


-- public.chat_thread_file foreign keys

ALTER TABLE public.chat_thread_file ADD CONSTRAINT fk_chat_thread_file_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


-- public.chat_threads foreign keys

ALTER TABLE public.chat_threads ADD CONSTRAINT fk_chat_threads_knowledge_graph FOREIGN KEY (knowledge_graph_id) REFERENCES public.graphs(id) ON DELETE SET NULL;
ALTER TABLE public.chat_threads ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


-- public.department foreign keys

ALTER TABLE public.department ADD CONSTRAINT department_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id) ON DELETE CASCADE;
ALTER TABLE public.department ADD CONSTRAINT department_leader_user_id_fkey FOREIGN KEY (leader_user_id) REFERENCES public.user_list(id) ON DELETE SET NULL;
ALTER TABLE public.department ADD CONSTRAINT department_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES public.department(id) ON DELETE SET NULL;


-- public.graphs foreign keys

ALTER TABLE public.graphs ADD CONSTRAINT fk_graphs_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;
ALTER TABLE public.graphs ADD CONSTRAINT graphs_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.user_list(id) ON DELETE SET NULL;
ALTER TABLE public.graphs ADD CONSTRAINT graphs_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;
ALTER TABLE public.graphs ADD CONSTRAINT graphs_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);


-- public.kb_audit_log foreign keys

ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_actor_id_fkey FOREIGN KEY (actor_id) REFERENCES public.user_list(id) ON DELETE CASCADE;
ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;
ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id) ON DELETE CASCADE;
ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_file_id_fkey FOREIGN KEY (file_id) REFERENCES public.knowledge_base_file(id) ON DELETE SET NULL;
ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_kb_id_fkey FOREIGN KEY (kb_id) REFERENCES public.knowledge_base(id) ON DELETE SET NULL;
ALTER TABLE public.kb_audit_log ADD CONSTRAINT kb_audit_log_target_user_id_fkey FOREIGN KEY (target_user_id) REFERENCES public.user_list(id) ON DELETE SET NULL;


-- public.knowledge_base foreign keys

ALTER TABLE public.knowledge_base ADD CONSTRAINT knowledge_base_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES public.user_list(id) ON DELETE CASCADE;
ALTER TABLE public.knowledge_base ADD CONSTRAINT knowledge_base_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;
ALTER TABLE public.knowledge_base ADD CONSTRAINT knowledge_base_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);


-- public.knowledge_base_chunk foreign keys

ALTER TABLE public.knowledge_base_chunk ADD CONSTRAINT fk_kb FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;
ALTER TABLE public.knowledge_base_chunk ADD CONSTRAINT fk_kb_file FOREIGN KEY (file_id) REFERENCES public.knowledge_base_file(id) ON DELETE CASCADE;


-- public.knowledge_base_file foreign keys

ALTER TABLE public.knowledge_base_file ADD CONSTRAINT fk_knowledge_base FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;


-- public.knowledge_processing_task foreign keys

ALTER TABLE public.knowledge_processing_task ADD CONSTRAINT fk_kb_processing_kb FOREIGN KEY (knowledge_base_id) REFERENCES public.knowledge_base(id) ON DELETE CASCADE;
ALTER TABLE public.knowledge_processing_task ADD CONSTRAINT fk_kb_processing_user FOREIGN KEY (user_id) REFERENCES public.user_list(id) ON DELETE CASCADE;


-- public.user_list foreign keys

ALTER TABLE public.user_list ADD CONSTRAINT user_list_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.department(id) ON DELETE SET NULL;
ALTER TABLE public.user_list ADD CONSTRAINT user_list_enterprise_id_fkey FOREIGN KEY (enterprise_id) REFERENCES public.enterprise(id);