/*
  Warnings:

  - You are about to drop the `personalities` table. If the table is not empty, all the data it contains will be lost.
  - Added the required column `display_text` to the `knowledge_links` table without a default value. This is not possible if the table is not empty.
  - Added the required column `position_end` to the `knowledge_links` table without a default value. This is not possible if the table is not empty.
  - Added the required column `position_start` to the `knowledge_links` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE "personalities" DROP CONSTRAINT "personalities_workspace_id_fkey";

-- DropIndex
DROP INDEX "knowledge_links_source_id_target_id_key";

-- AlterTable: Add new columns with temporary defaults for existing records
ALTER TABLE "knowledge_links" 
ADD COLUMN     "display_text" TEXT DEFAULT '',
ADD COLUMN     "position_end" INTEGER DEFAULT 0,
ADD COLUMN     "position_start" INTEGER DEFAULT 0,
ADD COLUMN     "resolved" BOOLEAN NOT NULL DEFAULT false,
ALTER COLUMN "target_id" DROP NOT NULL;

-- Update existing records: set display_text to link_text and resolved to true if target exists
UPDATE "knowledge_links" SET "display_text" = "link_text" WHERE "display_text" = '';
UPDATE "knowledge_links" SET "resolved" = true WHERE "target_id" IS NOT NULL;

-- Remove defaults for new records
ALTER TABLE "knowledge_links" 
ALTER COLUMN "display_text" DROP DEFAULT,
ALTER COLUMN "position_end" DROP DEFAULT,
ALTER COLUMN "position_start" DROP DEFAULT;

-- DropTable
DROP TABLE "personalities";

-- DropEnum
DROP TYPE "FormalityLevel";

-- CreateIndex
CREATE INDEX "knowledge_links_source_id_resolved_idx" ON "knowledge_links"("source_id", "resolved");
