CREATE TABLE "crumb_reactions" (
	"trail_id" uuid NOT NULL,
	"crumb_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"emoji" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "crumb_reactions_trail_id_crumb_id_user_id_pk" PRIMARY KEY("trail_id","crumb_id","user_id")
);
--> statement-breakpoint
CREATE TABLE "trail_share_links" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"trail_id" uuid NOT NULL,
	"token" text NOT NULL,
	"created_by" uuid,
	"revoked_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "trail_user_states" (
	"trail_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"last_viewed_at" timestamp with time zone,
	"pinned_at" timestamp with time zone,
	CONSTRAINT "trail_user_states_trail_id_user_id_pk" PRIMARY KEY("trail_id","user_id")
);
--> statement-breakpoint
ALTER TABLE "crumb_reactions" ADD CONSTRAINT "crumb_reactions_trail_id_trails_id_fk" FOREIGN KEY ("trail_id") REFERENCES "public"."trails"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "crumb_reactions" ADD CONSTRAINT "crumb_reactions_crumb_id_crumbs_id_fk" FOREIGN KEY ("crumb_id") REFERENCES "public"."crumbs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "crumb_reactions" ADD CONSTRAINT "crumb_reactions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "trail_share_links" ADD CONSTRAINT "trail_share_links_trail_id_trails_id_fk" FOREIGN KEY ("trail_id") REFERENCES "public"."trails"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "trail_share_links" ADD CONSTRAINT "trail_share_links_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "trail_user_states" ADD CONSTRAINT "trail_user_states_trail_id_trails_id_fk" FOREIGN KEY ("trail_id") REFERENCES "public"."trails"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "trail_user_states" ADD CONSTRAINT "trail_user_states_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "crumb_reactions_crumb_id_idx" ON "crumb_reactions" USING btree ("crumb_id");--> statement-breakpoint
CREATE UNIQUE INDEX "trail_share_links_token_idx" ON "trail_share_links" USING btree ("token");--> statement-breakpoint
CREATE INDEX "trail_share_links_trail_id_idx" ON "trail_share_links" USING btree ("trail_id");--> statement-breakpoint
CREATE INDEX "trail_user_states_user_id_idx" ON "trail_user_states" USING btree ("user_id");