-- AiConsentLog (audit RGPD) + consentement persistant sur UserAISettings

CREATE TABLE IF NOT EXISTS "AiConsentLog" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "consent" BOOLEAN NOT NULL DEFAULT true,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'AiConsentLog_userId_fkey'
  ) THEN
    ALTER TABLE "AiConsentLog"
      ADD CONSTRAINT "AiConsentLog_userId_fkey"
      FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
  END IF;
END $$;

ALTER TABLE "UserAISettings"
  ADD COLUMN IF NOT EXISTS "aiProcessingConsent" BOOLEAN NOT NULL DEFAULT false;
