CREATE TABLE "devices" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"name" varchar(255),
	"is_camera" boolean DEFAULT false NOT NULL,
	"last_seen_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"event_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"sent_at" timestamp with time zone DEFAULT now() NOT NULL,
	"channel" varchar(32) NOT NULL,
	"status" varchar(32) DEFAULT 'queued' NOT NULL,
	"is_read" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
ALTER TABLE "events" RENAME COLUMN "creator_id" TO "user_id";--> statement-breakpoint
ALTER TABLE "events" DROP CONSTRAINT "events_creator_id_users_id_fk";
--> statement-breakpoint
ALTER TABLE "events" ALTER COLUMN "title" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "events" ALTER COLUMN "video_url" SET DATA TYPE varchar(1024);--> statement-breakpoint
ALTER TABLE "events" ALTER COLUMN "video_url" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "device_id" uuid;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "triggered_by" varchar(64) DEFAULT 'motion';--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "status" varchar(32) DEFAULT 'recording' NOT NULL;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "started_at" timestamp with time zone DEFAULT now();--> statement-breakpoint
UPDATE "events" SET "started_at" = now() WHERE "started_at" IS NULL;--> statement-breakpoint
ALTER TABLE "events" ALTER COLUMN "started_at" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "events" ALTER COLUMN "started_at" DROP DEFAULT;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "ended_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "notified_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "events" ADD COLUMN "updated_at" timestamp with time zone DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "videos" ADD COLUMN "event_id" uuid;--> statement-breakpoint
ALTER TABLE "videos" ADD COLUMN "device_id" uuid NOT NULL;--> statement-breakpoint
ALTER TABLE "devices" ADD CONSTRAINT "devices_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_event_id_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."events"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "events" ADD CONSTRAINT "events_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "events" ADD CONSTRAINT "events_device_id_devices_id_fk" FOREIGN KEY ("device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "videos" ADD CONSTRAINT "videos_event_id_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."events"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "videos" ADD CONSTRAINT "videos_device_id_devices_id_fk" FOREIGN KEY ("device_id") REFERENCES "public"."devices"("id") ON DELETE no action ON UPDATE no action;
