CREATE TABLE "users" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "slug" TEXT NOT NULL,
    "display_name" TEXT NOT NULL,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "users_slug_key" ON "users"("slug");

INSERT INTO "users" (
    "id",
    "slug",
    "display_name",
    "is_default",
    "created_at",
    "updated_at"
)
VALUES (
    gen_random_uuid(),
    'default-owner',
    'Default Owner',
    true,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
)
ON CONFLICT ("slug") DO NOTHING;

ALTER TABLE "artwork_assets"
    ALTER COLUMN "id" SET DEFAULT gen_random_uuid();

ALTER TABLE "audio_assets"
    ALTER COLUMN "id" SET DEFAULT gen_random_uuid(),
    ADD COLUMN "user_id" UUID;

ALTER TABLE "device_sync_cursors"
    ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP;

ALTER TABLE "devices"
    ALTER COLUMN "id" SET DEFAULT gen_random_uuid(),
    ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP,
    ADD COLUMN "user_id" UUID;

ALTER TABLE "library_events"
    ADD COLUMN "user_id" UUID;

ALTER TABLE "tracks"
    ALTER COLUMN "id" SET DEFAULT gen_random_uuid(),
    ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP,
    ADD COLUMN "user_id" UUID;

ALTER TABLE "upload_sessions"
    ALTER COLUMN "id" SET DEFAULT gen_random_uuid(),
    ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP,
    ADD COLUMN "user_id" UUID,
    ADD COLUMN "track_id" UUID,
    ADD COLUMN "audio_asset_id" UUID,
    ADD COLUMN "original_filename" TEXT,
    ADD COLUMN "completed_at" TIMESTAMP(3),
    ADD COLUMN "finalized_at" TIMESTAMP(3);

UPDATE "devices"
SET "user_id" = (SELECT "id" FROM "users" WHERE "slug" = 'default-owner')
WHERE "user_id" IS NULL;

UPDATE "tracks"
SET "user_id" = (SELECT "id" FROM "users" WHERE "slug" = 'default-owner')
WHERE "user_id" IS NULL;

UPDATE "audio_assets"
SET "user_id" = (SELECT "id" FROM "users" WHERE "slug" = 'default-owner')
WHERE "user_id" IS NULL;

UPDATE "upload_sessions"
SET
    "user_id" = (SELECT "id" FROM "users" WHERE "slug" = 'default-owner'),
    "original_filename" = COALESCE("original_filename", "expected_sha256" || '.mp3')
WHERE "user_id" IS NULL
   OR "original_filename" IS NULL;

UPDATE "library_events"
SET "user_id" = (SELECT "id" FROM "users" WHERE "slug" = 'default-owner')
WHERE "user_id" IS NULL;

ALTER TABLE "audio_assets"
    ALTER COLUMN "user_id" SET NOT NULL;

ALTER TABLE "devices"
    ALTER COLUMN "user_id" SET NOT NULL;

ALTER TABLE "library_events"
    ALTER COLUMN "user_id" SET NOT NULL;

ALTER TABLE "tracks"
    ALTER COLUMN "user_id" SET NOT NULL;

ALTER TABLE "upload_sessions"
    ALTER COLUMN "user_id" SET NOT NULL,
    ALTER COLUMN "original_filename" SET NOT NULL;

DROP INDEX "audio_assets_sha256_key";

CREATE UNIQUE INDEX "audio_assets_user_id_sha256_key" ON "audio_assets"("user_id", "sha256");
CREATE INDEX "audio_assets_user_id_idx" ON "audio_assets"("user_id");
CREATE INDEX "devices_user_id_idx" ON "devices"("user_id");
CREATE INDEX "library_events_user_id_idx" ON "library_events"("user_id");
CREATE INDEX "tracks_user_id_idx" ON "tracks"("user_id");
CREATE INDEX "upload_sessions_user_id_idx" ON "upload_sessions"("user_id");

ALTER TABLE "audio_assets"
    ADD CONSTRAINT "audio_assets_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "devices"
    ADD CONSTRAINT "devices_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "library_events"
    ADD CONSTRAINT "library_events_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "tracks"
    ADD CONSTRAINT "tracks_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "upload_sessions"
    ADD CONSTRAINT "upload_sessions_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;
