-- CreateEnum
CREATE TYPE "platform_internal_apps"."InternalAppActorRole" AS ENUM ('OWNER', 'ADMIN', 'SYSTEM');
-- CreateEnum
CREATE TYPE "platform_internal_apps"."InternalAppEventOutcome" AS ENUM ('OK', 'FAIL');
-- CreateTable
CREATE TABLE "platform_internal_apps"."internal_app_events" (
    "id" UUID NOT NULL,
    "app_id" UUID,
    "employee_slug" VARCHAR(20),
    "actor_id" UUID,
    "actor_role" "platform_internal_apps"."InternalAppActorRole" NOT NULL,
    "event_type" VARCHAR(48) NOT NULL,
    "outcome" "platform_internal_apps"."InternalAppEventOutcome" NOT NULL DEFAULT 'OK',
    "error_code" VARCHAR(64),
    "duration_ms" INTEGER,
    "payload" JSONB NOT NULL DEFAULT '{}',
    "request_id" VARCHAR(64),
    "ip_addr" VARCHAR(64),
    "user_agent" VARCHAR(256),
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "internal_app_events_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "internal_app_events_app_id_created_at_idx" ON "platform_internal_apps"."internal_app_events"("app_id", "created_at" DESC);
-- CreateIndex
CREATE INDEX "internal_app_events_employee_slug_created_at_idx" ON "platform_internal_apps"."internal_app_events"("employee_slug", "created_at" DESC);
-- CreateIndex
CREATE INDEX "internal_app_events_event_type_created_at_idx" ON "platform_internal_apps"."internal_app_events"("event_type", "created_at" DESC);
-- CreateIndex
CREATE INDEX "internal_app_events_actor_role_created_at_idx" ON "platform_internal_apps"."internal_app_events"("actor_role", "created_at" DESC);
-- CreateIndex
CREATE INDEX "internal_app_events_organization_id_created_at_idx" ON "platform_internal_apps"."internal_app_events"("organization_id", "created_at" DESC);
-- AddForeignKey
ALTER TABLE "platform_internal_apps"."internal_app_events" ADD CONSTRAINT "internal_app_events_app_id_fkey" FOREIGN KEY ("app_id") REFERENCES "platform_internal_apps"."internal_apps"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "platform_internal_apps"."internal_app_events" ADD CONSTRAINT "internal_app_events_actor_id_fkey" FOREIGN KEY ("actor_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
