CREATE TABLE IF NOT EXISTS "channel_integrations" (
	"id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "channel_integrations_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
	"channel_id" integer NOT NULL,
	"provider" text NOT NULL,
	"label" text,
	"external_url" text,
	"added_by" integer NOT NULL,
	"added_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "scheduled_statuses" (
	"id" integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (sequence name "scheduled_statuses_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
	"workspace_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"text" text NOT NULL,
	"emoji" text DEFAULT '💬' NOT NULL,
	"starts_at" timestamp with time zone NOT NULL,
	"ends_at" timestamp with time zone NOT NULL,
	"pause_notifications" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "availability_mode" text DEFAULT 'auto' NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "dnd_until" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "auto_status_huddle" boolean DEFAULT true NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "auto_status_focus" boolean DEFAULT true NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "auto_status_outside_hours" boolean DEFAULT true NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "focus_mode_enabled" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "in_huddle" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "work_hours_start" text DEFAULT '09:00' NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "work_hours_end" text DEFAULT '17:00' NOT NULL;--> statement-breakpoint
ALTER TABLE "workspace_members" ADD COLUMN "working_days" jsonb DEFAULT '[1,2,3,4,5]'::jsonb NOT NULL;--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "channel_integrations" ADD CONSTRAINT "channel_integrations_channel_id_channels_id_fk" FOREIGN KEY ("channel_id") REFERENCES "public"."channels"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "channel_integrations" ADD CONSTRAINT "channel_integrations_added_by_users_id_fk" FOREIGN KEY ("added_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "scheduled_statuses" ADD CONSTRAINT "scheduled_statuses_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "scheduled_statuses" ADD CONSTRAINT "scheduled_statuses_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "channel_integrations_channel_provider_idx" ON "channel_integrations" USING btree ("channel_id","provider");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "scheduled_statuses_user_start_idx" ON "scheduled_statuses" USING btree ("workspace_id","user_id","starts_at");