CREATE TABLE "comments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"conversation_id" uuid NOT NULL,
	"social_object_id" uuid,
	"channel" text NOT NULL,
	"external_comment_id" text NOT NULL,
	"parent_external_comment_id" text,
	"author_external_id" text,
	"author_name" text,
	"author_handle" text,
	"contact_id" uuid,
	"direction" text NOT NULL,
	"body" text NOT NULL,
	"edit_history" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"media" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"is_hidden" boolean DEFAULT false NOT NULL,
	"hidden_reason" text,
	"hidden_by" text,
	"is_deleted" boolean DEFAULT false NOT NULL,
	"liked_by_page" boolean DEFAULT false NOT NULL,
	"like_count" integer DEFAULT 0 NOT NULL,
	"sentiment" text,
	"intent" text,
	"language" text,
	"toxicity" real,
	"external_created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"sent_by_user_id" uuid,
	"sent_by_ai" boolean DEFAULT false NOT NULL,
	"sent_by_rule_id" uuid,
	"provider_status" text DEFAULT 'received' NOT NULL,
	"provider_error" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "engagement_metrics_daily" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"date" date NOT NULL,
	"social_account_id" uuid,
	"channel" text NOT NULL,
	"user_id" uuid,
	"inbound_count" integer DEFAULT 0 NOT NULL,
	"replied_count" integer DEFAULT 0 NOT NULL,
	"median_first_response_s" integer,
	"p90_first_response_s" integer,
	"resolved_count" integer DEFAULT 0 NOT NULL,
	"hidden_count" integer DEFAULT 0 NOT NULL,
	"negative_count" integer DEFAULT 0 NOT NULL,
	"leads_created" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE "engagement_rules" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"name" text NOT NULL,
	"type" text NOT NULL,
	"channels" text[] DEFAULT '{}'::text[] NOT NULL,
	"social_account_ids" uuid[] DEFAULT '{}'::uuid[] NOT NULL,
	"social_object_ids" uuid[] DEFAULT '{}'::uuid[] NOT NULL,
	"trigger" text DEFAULT 'new_comment' NOT NULL,
	"conditions" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"actions" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"priority" integer DEFAULT 100 NOT NULL,
	"stop_processing" boolean DEFAULT false NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"schedule" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"rate_limit" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"stats" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "knowledge_entries" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"question" text NOT NULL,
	"answer" text NOT NULL,
	"scope" text,
	"social_account_id" uuid,
	"language" text DEFAULT 'en' NOT NULL,
	"tags" text[] DEFAULT '{}'::text[] NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "messaging_windows" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"contact_id" uuid NOT NULL,
	"channel" text NOT NULL,
	"social_account_id" uuid,
	"last_inbound_at" timestamp with time zone NOT NULL,
	"window_expires_at" timestamp with time zone NOT NULL,
	"private_reply_available_until" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "moderation_events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"comment_id" uuid NOT NULL,
	"action" text NOT NULL,
	"reason" text,
	"actor_type" text NOT NULL,
	"actor_id" text,
	"actor_name" text,
	"undone_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "reviews" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"conversation_id" uuid NOT NULL,
	"social_account_id" uuid NOT NULL,
	"external_review_id" text NOT NULL,
	"reviewer_name" text,
	"rating" integer NOT NULL,
	"body" text,
	"language" text,
	"external_created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"reply_body" text,
	"reply_status" text DEFAULT 'none' NOT NULL,
	"replied_at" timestamp with time zone,
	"replied_by_user_id" uuid,
	"replied_by_ai" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "rule_firings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"rule_id" uuid NOT NULL,
	"conversation_id" uuid,
	"author_external_id" text,
	"social_object_id" uuid,
	"actions" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "social_accounts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"integration_id" uuid,
	"channel_family" text NOT NULL,
	"platform" text NOT NULL,
	"external_account_id" text NOT NULL,
	"name" text NOT NULL,
	"avatar_url" text,
	"status" text DEFAULT 'inactive' NOT NULL,
	"capabilities" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"webhook_subscribed" boolean DEFAULT false NOT NULL,
	"last_event_at" timestamp with time zone,
	"last_polled_at" timestamp with time zone,
	"poll_cursor" text,
	"backfill" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"last_error" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "social_blocklist" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"platform" text NOT NULL,
	"author_external_id" text NOT NULL,
	"author_name" text,
	"reason" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
CREATE TABLE "social_objects" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"workspace_id" uuid NOT NULL,
	"social_account_id" uuid NOT NULL,
	"type" text NOT NULL,
	"external_id" text NOT NULL,
	"permalink" text,
	"thumbnail_url" text,
	"caption_excerpt" text,
	"ad_id" uuid,
	"external_ad_id" text,
	"campaign_id" uuid,
	"campaign_name" text,
	"published_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid
);
--> statement-breakpoint
ALTER TABLE "canned_replies" ADD COLUMN "title" text;--> statement-breakpoint
ALTER TABLE "canned_replies" ADD COLUMN "variables" text[] DEFAULT '{}'::text[] NOT NULL;--> statement-breakpoint
ALTER TABLE "canned_replies" ADD COLUMN "channels" text[] DEFAULT '{}'::text[] NOT NULL;--> statement-breakpoint
ALTER TABLE "canned_replies" ADD COLUMN "tags" text[] DEFAULT '{}'::text[] NOT NULL;--> statement-breakpoint
ALTER TABLE "canned_replies" ADD COLUMN "language" text DEFAULT 'en' NOT NULL;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "kind" text DEFAULT 'dm' NOT NULL;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "social_account_id" uuid;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "social_object_id" uuid;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "external_thread_id" text;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "priority" text DEFAULT 'normal' NOT NULL;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "sentiment" text;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "intent" text;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "tags" text[] DEFAULT '{}'::text[] NOT NULL;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "sla_due_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "sla_breached_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "first_response_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "resolved_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "last_inbound_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "snoozed_until" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "locked_by_user_id" uuid;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "locked_until" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "conversations" ADD COLUMN "handled_by" text;--> statement-breakpoint
ALTER TABLE "memberships" ADD COLUMN "team" text;--> statement-breakpoint
ALTER TABLE "memberships" ADD COLUMN "availability" text DEFAULT 'online' NOT NULL;--> statement-breakpoint
ALTER TABLE "memberships" ADD COLUMN "social_account_ids" uuid[] DEFAULT '{}'::uuid[] NOT NULL;--> statement-breakpoint
ALTER TABLE "comments" ADD CONSTRAINT "comments_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "comments" ADD CONSTRAINT "comments_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "engagement_metrics_daily" ADD CONSTRAINT "engagement_metrics_daily_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "engagement_rules" ADD CONSTRAINT "engagement_rules_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "knowledge_entries" ADD CONSTRAINT "knowledge_entries_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "messaging_windows" ADD CONSTRAINT "messaging_windows_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "messaging_windows" ADD CONSTRAINT "messaging_windows_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "moderation_events" ADD CONSTRAINT "moderation_events_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "moderation_events" ADD CONSTRAINT "moderation_events_comment_id_comments_id_fk" FOREIGN KEY ("comment_id") REFERENCES "public"."comments"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reviews" ADD CONSTRAINT "reviews_social_account_id_social_accounts_id_fk" FOREIGN KEY ("social_account_id") REFERENCES "public"."social_accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rule_firings" ADD CONSTRAINT "rule_firings_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rule_firings" ADD CONSTRAINT "rule_firings_rule_id_engagement_rules_id_fk" FOREIGN KEY ("rule_id") REFERENCES "public"."engagement_rules"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "social_accounts" ADD CONSTRAINT "social_accounts_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "social_blocklist" ADD CONSTRAINT "social_blocklist_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "social_objects" ADD CONSTRAINT "social_objects_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "social_objects" ADD CONSTRAINT "social_objects_social_account_id_social_accounts_id_fk" FOREIGN KEY ("social_account_id") REFERENCES "public"."social_accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "comments_ext_uq" ON "comments" USING btree ("workspace_id","channel","external_comment_id");--> statement-breakpoint
CREATE INDEX "comments_conv_idx" ON "comments" USING btree ("conversation_id","external_created_at");--> statement-breakpoint
CREATE INDEX "eng_metrics_idx" ON "engagement_metrics_daily" USING btree ("workspace_id","date");--> statement-breakpoint
CREATE INDEX "engagement_rules_ws_idx" ON "engagement_rules" USING btree ("workspace_id","priority");--> statement-breakpoint
CREATE UNIQUE INDEX "msg_windows_uq" ON "messaging_windows" USING btree ("workspace_id","contact_id","channel");--> statement-breakpoint
CREATE INDEX "moderation_events_ws_idx" ON "moderation_events" USING btree ("workspace_id","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "reviews_ext_uq" ON "reviews" USING btree ("social_account_id","external_review_id");--> statement-breakpoint
CREATE INDEX "rule_firings_idx" ON "rule_firings" USING btree ("rule_id","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "social_accounts_uq" ON "social_accounts" USING btree ("workspace_id","platform","external_account_id");--> statement-breakpoint
CREATE INDEX "social_accounts_ext_idx" ON "social_accounts" USING btree ("external_account_id");--> statement-breakpoint
CREATE UNIQUE INDEX "social_blocklist_uq" ON "social_blocklist" USING btree ("workspace_id","platform","author_external_id");--> statement-breakpoint
CREATE UNIQUE INDEX "social_objects_uq" ON "social_objects" USING btree ("social_account_id","external_id");--> statement-breakpoint
CREATE UNIQUE INDEX "conv_ext_thread_uq" ON "conversations" USING btree ("workspace_id","channel","external_thread_id");