-- Add admin role and impersonation fields to User
ALTER TABLE "User" ADD COLUMN "role" TEXT NOT NULL DEFAULT 'dealer';
ALTER TABLE "User" ADD COLUMN "impersonatedById" TEXT;
ALTER TABLE "User" ADD COLUMN "impersonatedAt" TIMESTAMP(3);
ALTER TABLE "User" ADD COLUMN "impersonationExpiry" TIMESTAMP(3);

-- Create index on User role for admin lookups
CREATE INDEX "User_role_idx" ON "User"("role");

-- CreateTable: PageView for analytics
CREATE TABLE "PageView" (
    "id" TEXT NOT NULL,
    "dealerId" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "referrer" TEXT,
    "userAgent" TEXT,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "PageView_pkey" PRIMARY KEY ("id")
);

-- CreateTable: LinkClick for analytics
CREATE TABLE "LinkClick" (
    "id" TEXT NOT NULL,
    "dealerId" TEXT NOT NULL,
    "sourcePath" TEXT NOT NULL,
    "targetHref" TEXT NOT NULL,
    "clickType" TEXT NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "LinkClick_pkey" PRIMARY KEY ("id")
);

-- CreateTable: Lead for contact form submissions
CREATE TABLE "Lead" (
    "id" TEXT NOT NULL,
    "dealerId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "message" TEXT,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "Lead_pkey" PRIMARY KEY ("id")
);

-- CreateTable: AdminAction for audit logging
CREATE TABLE "AdminAction" (
    "id" TEXT NOT NULL,
    "adminUserId" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "targetType" TEXT NOT NULL,
    "targetId" TEXT NOT NULL,
    "details" JSONB,
    "reason" TEXT,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "AdminAction_pkey" PRIMARY KEY ("id")
);

-- CreateIndex: PageView indexes
CREATE INDEX "PageView_dealerId_timestamp_idx" ON "PageView"("dealerId", "timestamp");
CREATE INDEX "PageView_dealerId_path_timestamp_idx" ON "PageView"("dealerId", "path", "timestamp");

-- CreateIndex: LinkClick indexes
CREATE INDEX "LinkClick_dealerId_timestamp_idx" ON "LinkClick"("dealerId", "timestamp");
CREATE INDEX "LinkClick_dealerId_clickType_timestamp_idx" ON "LinkClick"("dealerId", "clickType", "timestamp");

-- CreateIndex: Lead index
CREATE INDEX "Lead_dealerId_timestamp_idx" ON "Lead"("dealerId", "timestamp");

-- CreateIndex: AdminAction indexes
CREATE INDEX "AdminAction_adminUserId_idx" ON "AdminAction"("adminUserId");
CREATE INDEX "AdminAction_targetType_targetId_idx" ON "AdminAction"("targetType", "targetId");
CREATE INDEX "AdminAction_action_idx" ON "AdminAction"("action");
CREATE INDEX "AdminAction_createdAt_idx" ON "AdminAction"("createdAt");

-- AddForeignKey: PageView -> Dealer
ALTER TABLE "PageView" ADD CONSTRAINT "PageView_dealerId_fkey" FOREIGN KEY ("dealerId") REFERENCES "Dealer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: LinkClick -> Dealer
ALTER TABLE "LinkClick" ADD CONSTRAINT "LinkClick_dealerId_fkey" FOREIGN KEY ("dealerId") REFERENCES "Dealer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: Lead -> Dealer
ALTER TABLE "Lead" ADD CONSTRAINT "Lead_dealerId_fkey" FOREIGN KEY ("dealerId") REFERENCES "Dealer"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: AdminAction -> User
ALTER TABLE "AdminAction" ADD CONSTRAINT "AdminAction_adminUserId_fkey" FOREIGN KEY ("adminUserId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
