-- CreateEnum для типов алертов безопасности
CREATE TYPE "SecurityAlertType" AS ENUM ('EXCESSIVE_ACCESS', 'UNAUTHORIZED_ATTEMPT', 'DATA_LEAK_RISK', 'SUSPICIOUS_PATTERN', 'BULK_EXPORT_DETECTED');

-- CreateEnum для уровней серьезности алертов
CREATE TYPE "SecurityAlertSeverity" AS ENUM ('LOW', 'MEDIUM', 'HIGH', 'CRITICAL');

-- CreateTable для журнала аудита
CREATE TABLE "audit_logs" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "organizationType" "OrganizationType" NOT NULL,
    "action" TEXT NOT NULL,
    "resourceType" TEXT NOT NULL,
    "resourceId" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable для алертов безопасности
CREATE TABLE "security_alerts" (
    "id" TEXT NOT NULL,
    "type" "SecurityAlertType" NOT NULL,
    "severity" "SecurityAlertSeverity" NOT NULL,
    "userId" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "resolved" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateIndex для оптимизации запросов по audit_logs
CREATE INDEX "audit_logs_userId_idx" ON "audit_logs"("userId");
CREATE INDEX "audit_logs_timestamp_idx" ON "audit_logs"("timestamp");
CREATE INDEX "audit_logs_action_idx" ON "audit_logs"("action");
CREATE INDEX "audit_logs_resourceType_idx" ON "audit_logs"("resourceType");

-- CreateIndex для оптимизации запросов по security_alerts
CREATE INDEX "security_alerts_userId_idx" ON "security_alerts"("userId");
CREATE INDEX "security_alerts_timestamp_idx" ON "security_alerts"("timestamp");
CREATE INDEX "security_alerts_resolved_idx" ON "security_alerts"("resolved");
CREATE INDEX "security_alerts_severity_idx" ON "security_alerts"("severity");