-- =========================================================================
-- Baseline squash migration (issue #213)
-- Generated by `prisma migrate diff --from-empty --to-schema-datamodel prisma/schema`
-- on develop @ HEAD. Replaces 69 历史迁移（移到 _archive/）.
--
-- 不要在已部署环境直接 prisma migrate deploy 这个文件 —— 已部署环境通过
-- _prisma_migrations 表元数据迁移完成（见 docs/todos/migration-history-debt.md）.
-- =========================================================================

-- Required extensions (历史上由 _archive/20260430000000_add_ops_center_m365_dormant 引入；
-- baseline 必须包含，否则 fresh DB replay 时 CITEXT 列建表失败)
CREATE EXTENSION IF NOT EXISTS "citext";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "corp_approval";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "corp_hr";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "corp_request";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "mfg_inventory";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_ai";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_audit";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_automation";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_devtracker";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_feedback";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_flow_diagram";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_form";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_iam";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_knowledge";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_logging";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_meeting_attendance";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_notify";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_ops_center";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_performance";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_site_attendance";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "platform_tickets";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "robot_manager";

-- CreateEnum
CREATE TYPE "corp_approval"."ProcessStatus" AS ENUM ('ACTIVE', 'SUSPENDED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "corp_approval"."VersionStatus" AS ENUM ('DRAFT', 'DEPLOYED', 'SUPERSEDED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "corp_approval"."InstanceStatus" AS ENUM ('RUNNING', 'SUSPENDED', 'APPROVED', 'REJECTED', 'WITHDRAWN', 'TERMINATED', 'FAILED');

-- CreateEnum
CREATE TYPE "corp_approval"."ApprovalAdminExportStatus" AS ENUM ('PENDING', 'PROCESSING', 'SUCCESS', 'FAILED', 'EXPIRED');

-- CreateEnum
CREATE TYPE "corp_approval"."NodeStatus" AS ENUM ('PENDING', 'ACTIVE', 'COMPLETED', 'CANCELLED', 'FAILED', 'SKIPPED');

-- CreateEnum
CREATE TYPE "corp_approval"."ApprovalTaskType" AS ENUM ('APPROVAL', 'COUNTERSIGN', 'OR_SIGN', 'CC', 'NOTIFICATION', 'SERVICE_TASK', 'SCRIPT_TASK', 'USER_TASK', 'RECEIVE_TASK', 'SEQUENTIAL');

-- CreateEnum
CREATE TYPE "corp_approval"."ApprovalTaskStatus" AS ENUM ('CREATED', 'PENDING', 'CLAIMED', 'RESERVED', 'ASSIGNED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'SUSPENDED', 'WITHDRAWN');

-- CreateEnum
CREATE TYPE "corp_approval"."ApprovalTaskAction" AS ENUM ('SUBMIT', 'APPROVE', 'REJECT', 'RETURN', 'FORWARD', 'WITHDRAW', 'APPROVER_WITHDRAW', 'ADD_SIGN', 'REMOVE_SIGN', 'CLAIM', 'UNCLAIM', 'DELEGATE', 'AUTO_APPROVE', 'AUTO_REJECT', 'ESCALATE', 'REMIND', 'ADMIN_APPROVE', 'ADMIN_REJECT', 'ADMIN_TERMINATE', 'ADMIN_REASSIGN', 'COMPLETE', 'CANCEL', 'COMMENT', 'EXECUTE');

-- CreateEnum
CREATE TYPE "corp_approval"."CallbackStatus" AS ENUM ('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED');

-- CreateEnum
CREATE TYPE "corp_approval"."ReminderType" AS ENUM ('TIMEOUT', 'MANUAL', 'BEFORE_TIMEOUT');

-- CreateEnum
CREATE TYPE "corp_approval"."ReminderStatus" AS ENUM ('SCHEDULED', 'EXECUTED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "corp_approval"."DelegationType" AS ENUM ('MANUAL', 'AUTO_ON_LEAVE', 'AUTO_TIME_RANGE', 'AUTO_TIMEOUT');

-- CreateEnum
CREATE TYPE "corp_approval"."DelegationScope" AS ENUM ('ALL', 'SPECIFIC');

-- CreateEnum
CREATE TYPE "corp_request"."ExpenseCategory" AS ENUM ('TRAVEL', 'MEAL', 'OFFICE', 'COMMUNICATION', 'TRAINING', 'OTHER');

-- CreateEnum
CREATE TYPE "corp_request"."ExpenseStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'PAID', 'CANCELLED');

-- CreateEnum
CREATE TYPE "corp_request"."PurchaseStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'ORDERED', 'RECEIVED', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "corp_request"."ContractType" AS ENUM ('SALES', 'PURCHASE', 'SERVICE', 'LEASE', 'EMPLOYMENT', 'OTHER');

-- CreateEnum
CREATE TYPE "corp_request"."ContractStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'SIGNED', 'EXECUTING', 'COMPLETED', 'TERMINATED');

-- CreateEnum
CREATE TYPE "mfg_inventory"."StationStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'MAINTENANCE');

-- CreateEnum
CREATE TYPE "mfg_inventory"."WarehouseStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'MAINTENANCE');

-- CreateEnum
CREATE TYPE "mfg_inventory"."StorageLocationStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'FULL', 'RESERVED');

-- CreateEnum
CREATE TYPE "mfg_inventory"."PartStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'DISCONTINUED', 'OBSOLETE');

-- CreateEnum
CREATE TYPE "mfg_inventory"."LabelStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'VOID');

-- CreateEnum
CREATE TYPE "mfg_inventory"."InventoryOperationType" AS ENUM ('CHECK_IN', 'CHECK_OUT', 'RECEIVING', 'RETURN', 'ADJUSTMENT', 'TRANSFER', 'SCRAP', 'INITIAL');

-- CreateEnum
CREATE TYPE "mfg_inventory"."StockAlertType" AS ENUM ('LOW_STOCK', 'OUT_OF_STOCK', 'OVERSTOCK', 'APPROACHING_MIN');

-- CreateEnum
CREATE TYPE "mfg_inventory"."AlertSeverity" AS ENUM ('LOW', 'MEDIUM', 'HIGH', 'CRITICAL');

-- CreateEnum
CREATE TYPE "mfg_inventory"."AlertStatus" AS ENUM ('PENDING', 'ACKNOWLEDGED', 'RESOLVED', 'IGNORED');

-- CreateEnum
CREATE TYPE "mfg_inventory"."ImportStatus" AS ENUM ('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED', 'PARTIAL');

-- CreateEnum
CREATE TYPE "mfg_inventory"."GroupFieldType" AS ENUM ('TEXT', 'NUMBER', 'DECIMAL', 'BOOLEAN', 'DATE', 'DATETIME', 'SELECT', 'MULTISELECT', 'TEXTAREA', 'URL', 'EMAIL', 'PHONE');

-- CreateEnum
CREATE TYPE "platform_ai"."AIConversationStatus" AS ENUM ('ACTIVE', 'CLOSED', 'ESCALATED');

-- CreateEnum
CREATE TYPE "platform_ai"."AIConversationCategory" AS ENUM ('IT', 'HR', 'ADMIN', 'GENERAL');

-- CreateEnum
CREATE TYPE "platform_ai"."AIMessageRole" AS ENUM ('USER', 'ASSISTANT', 'SYSTEM');

-- CreateEnum
CREATE TYPE "platform_ai"."AIMessageSource" AS ENUM ('USER', 'AI', 'SYSTEM', 'ESCALATION');

-- CreateEnum
CREATE TYPE "platform_ai"."AIFeedbackType" AS ENUM ('LIKE', 'DISLIKE');

-- CreateEnum
CREATE TYPE "platform_ai"."AITicketCategory" AS ENUM ('IT', 'HR', 'ADMIN', 'FINANCE', 'OTHER');

-- CreateEnum
CREATE TYPE "platform_ai"."AIAssigneeTeam" AS ENUM ('IT', 'HR', 'ADMIN', 'FINANCE');

-- CreateEnum
CREATE TYPE "platform_ai"."AITicketPriority" AS ENUM ('LOW', 'MEDIUM', 'HIGH', 'URGENT');

-- CreateEnum
CREATE TYPE "platform_ai"."AITicketStatus" AS ENUM ('OPEN', 'IN_PROGRESS', 'RESOLVED', 'CLOSED');

-- CreateEnum
CREATE TYPE "platform_ai"."AIKnowledgeFixStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "platform_audit"."AuditAction" AS ENUM ('CREATE', 'READ', 'UPDATE', 'DELETE', 'BULK_UPDATE', 'BULK_DELETE', 'APPROVE', 'REJECT', 'RETURN', 'FORWARD', 'WITHDRAW', 'LOGIN', 'LOGOUT', 'LOGIN_FAILED', 'PASSWORD_CHANGE', 'PASSWORD_RESET', 'EMAIL_CHANGE', 'PERMISSION_CHANGE', 'ROLE_CHANGE', 'CONFIG_CHANGE', 'BACKUP', 'RESTORE', 'EXPORT', 'IMPORT', 'DEPLOY', 'ROLLBACK', 'PAYMENT', 'REFUND', 'INVOICE_GENERATE', 'FINANCIAL_CLOSE', 'BUDGET_APPROVE', 'DOWNLOAD', 'UPLOAD', 'SHARE', 'ARCHIVE');

-- CreateEnum
CREATE TYPE "platform_audit"."AuditStatus" AS ENUM ('SUCCESS', 'FAILED', 'PARTIAL', 'PENDING');

-- CreateEnum
CREATE TYPE "platform_audit"."DbOperation" AS ENUM ('INSERT', 'UPDATE', 'DELETE');

-- CreateEnum
CREATE TYPE "platform_audit"."ComplianceLevel" AS ENUM ('HIGH', 'MEDIUM', 'LOW');

-- CreateEnum
CREATE TYPE "platform_audit"."RiskLevel" AS ENUM ('HIGH', 'MEDIUM', 'LOW');

-- CreateEnum
CREATE TYPE "platform_audit"."IntegrityCheckType" AS ENUM ('HASH_CHAIN', 'SIGNATURE', 'COUNT', 'FULL');

-- CreateEnum
CREATE TYPE "platform_audit"."CheckStatus" AS ENUM ('SUCCESS', 'FAILED', 'PARTIAL');

-- CreateEnum
CREATE TYPE "platform_automation"."AutomationTaskType" AS ENUM ('LDAP_SYNC', 'DATA_CLEANUP', 'REPORT_GENERATION', 'NOTIFICATION', 'BACKUP', 'DINGTALK_SYNC', 'ADP_SYNC', 'M365_DORMANT_SYNC', 'CUSTOM');

-- CreateEnum
CREATE TYPE "platform_automation"."AutomationTaskStatus" AS ENUM ('ACTIVE', 'PAUSED', 'DISABLED');

-- CreateEnum
CREATE TYPE "platform_automation"."AutomationExecutionStatus" AS ENUM ('PENDING', 'RUNNING', 'SUCCESS', 'FAILED', 'TIMEOUT', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_devtracker"."DevItemStatus" AS ENUM ('DRAFT', 'REVIEWED', 'IN_DEVELOPMENT', 'IN_TESTING', 'USER_TESTING', 'DONE', 'CANCELLED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_devtracker"."DevItemType" AS ENUM ('MODULE', 'FEATURE', 'BUG');

-- CreateEnum
CREATE TYPE "platform_devtracker"."DevItemPriority" AS ENUM ('P0', 'P1', 'P2', 'P3');

-- CreateEnum
CREATE TYPE "platform_devtracker"."DevItemSeverity" AS ENUM ('S1', 'S2', 'S3', 'S4');

-- CreateEnum
CREATE TYPE "platform_feedback"."FeedbackType" AS ENUM ('BUG', 'FEATURE', 'IMPROVEMENT', 'OTHER');

-- CreateEnum
CREATE TYPE "platform_feedback"."FeedbackStatus" AS ENUM ('PENDING', 'IN_PROGRESS', 'RESOLVED', 'CLOSED');

-- CreateEnum
CREATE TYPE "platform_feedback"."FeedbackPriority" AS ENUM ('LOW', 'MEDIUM', 'HIGH', 'URGENT');

-- CreateEnum
CREATE TYPE "platform_form"."FormStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'DISABLED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_form"."FormVersionStatus" AS ENUM ('DRAFT', 'PENDING_REVIEW', 'PUBLISHED', 'REJECTED', 'DEPRECATED');

-- CreateEnum
CREATE TYPE "platform_form"."FormInstanceStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'PENDING_APPROVAL', 'APPROVED', 'REJECTED', 'WITHDRAWN', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_form"."ReleaseSnapshotStatus" AS ENUM ('DRAFT', 'PENDING', 'REJECTED', 'ACTIVE', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_iam"."DataScopeType" AS ENUM ('SELF', 'DEPARTMENT', 'DEPARTMENT_TREE', 'ORGANIZATION', 'REGION', 'ALL', 'CUSTOM');

-- CreateEnum
CREATE TYPE "platform_iam"."UserStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'SUSPENDED', 'TERMINATED');

-- CreateEnum
CREATE TYPE "platform_iam"."WorkflowRuleType" AS ENUM ('ORGANIZATION_RELATION', 'SYSTEM_ROLE_MAPPING', 'FIXED_USERS', 'DYNAMIC_SCRIPT');

-- CreateEnum
CREATE TYPE "platform_iam"."UserSource" AS ENUM ('LOCAL', 'ENTRA', 'LDAP');

-- CreateEnum
CREATE TYPE "platform_iam"."FieldAccess" AS ENUM ('VISIBLE', 'READONLY', 'HIDDEN', 'DESENSITIZE');

-- CreateEnum
CREATE TYPE "platform_knowledge"."KnowledgeArticleStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_knowledge"."DocAuthorityLevel" AS ENUM ('OFFICIAL', 'EXPERT', 'PUBLISHED', 'DRAFT');

-- CreateEnum
CREATE TYPE "platform_knowledge"."DocLifecycleStatus" AS ENUM ('DRAFT', 'UNDER_REVIEW', 'PUBLISHED', 'ARCHIVED', 'DEPRECATED');

-- CreateEnum
CREATE TYPE "platform_knowledge"."DocType" AS ENUM ('POLICY', 'MANUAL', 'TUTORIAL', 'FAQ', 'GENERAL');

-- CreateEnum
CREATE TYPE "platform_knowledge"."SyncStatus" AS ENUM ('PENDING', 'SYNCING', 'COMPLETED', 'FAILED');

-- CreateEnum
CREATE TYPE "platform_knowledge"."SyncItemStatus" AS ENUM ('PROCESSING', 'COMPLETED');

-- CreateEnum
CREATE TYPE "platform_knowledge"."RagflowSourceType" AS ENUM ('SP_DOCUMENT', 'ARTICLE');

-- CreateEnum
CREATE TYPE "platform_knowledge"."SearchType" AS ENUM ('KEYWORD', 'SEMANTIC', 'HYBRID');

-- CreateEnum
CREATE TYPE "platform_knowledge"."LikeType" AS ENUM ('LIKE', 'DISLIKE');

-- CreateEnum
CREATE TYPE "platform_knowledge"."QAFeedback" AS ENUM ('THUMBS_UP', 'THUMBS_DOWN');

-- CreateEnum
CREATE TYPE "platform_logging"."LogLevel" AS ENUM ('ERROR', 'WARN', 'INFO', 'DEBUG');

-- CreateEnum
CREATE TYPE "platform_logging"."LogAlertType" AS ENUM ('SLOW_REQUEST', 'HIGH_ERROR_RATE', 'DISK_SPACE', 'IP_ANOMALY');

-- CreateEnum
CREATE TYPE "platform_logging"."LogAlertStatus" AS ENUM ('SENT', 'FAILED', 'ACKNOWLEDGED');

-- CreateEnum
CREATE TYPE "platform_logging"."LogAlertSeverity" AS ENUM ('CRITICAL', 'WARNING', 'INFO');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookBootstrapStatus" AS ENUM ('QUEUED', 'RUNNING', 'SUCCEEDED', 'FAILED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."RecurrencePattern" AS ENUM ('DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."MeetingType" AS ENUM ('OFFLINE', 'ONLINE', 'HYBRID');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."MeetingStatus" AS ENUM ('SCHEDULED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."AttendeeRole" AS ENUM ('C_SUITE_ATTENDEE', 'LEVEL_1_REPORT_ATTENDEE', 'LEVEL_2_REPORT_ATTENDEE', 'MEETING_OPERATIONS', 'REGULAR_ATTENDEE', 'OPTIONAL_ATTENDEE');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."AttendanceStatus" AS ENUM ('NOT_CHECKED_IN', 'ON_SITE', 'ONLINE', 'ABSENT', 'PTO', 'BUSINESS_CONFLICT', 'LATE', 'MEETING');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."AttendanceMode" AS ENUM ('ON_SITE', 'ONLINE');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."CheckinType" AS ENUM ('QR_CODE', 'NFC', 'MANUAL', 'ONLINE');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."LeaveType" AS ENUM ('SICK', 'PERSONAL', 'ANNUAL', 'BUSINESS', 'OTHER');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."LeaveStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookMailboxType" AS ENUM ('SHARED', 'PERSONAL');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookSubscriptionStatus" AS ENUM ('ACTIVE', 'EXPIRED', 'ERROR', 'DISABLED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookManageStatus" AS ENUM ('PENDING_SELECTION', 'MANAGED', 'SYNC_ERROR', 'DISABLED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookCancellationSource" AS ENUM ('CANCELLED_BY_ORGANIZER', 'DELETED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookBindingSyncMode" AS ENUM ('AUTO', 'LOCKED_BY_LOCAL_EDIT');

-- CreateEnum
CREATE TYPE "platform_notify"."NotificationType" AS ENUM ('TASK_ASSIGNED', 'TASK_COMPLETED', 'WORKFLOW_APPROVED', 'WORKFLOW_REJECTED', 'WORK_RECORD_REMINDER', 'SYSTEM');

-- CreateEnum
CREATE TYPE "platform_notify"."NotificationStatus" AS ENUM ('UNREAD', 'READ', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_notify"."NotificationChannel" AS ENUM ('EMAIL', 'TEAMS', 'SMS', 'WEBSOCKET', 'IN_APP');

-- CreateEnum
CREATE TYPE "platform_notify"."NotificationSendStatus" AS ENUM ('PENDING', 'SENDING', 'SENT', 'FAILED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_notify"."NotificationPriority" AS ENUM ('HIGH', 'NORMAL', 'LOW');

-- CreateEnum
CREATE TYPE "platform_ops_center"."M365UserActivityChangeField" AS ENUM ('lastSignInDateTime', 'lastNonInteractiveSignInDateTime', 'lastEmailActivity', 'lastOneDriveActivity', 'lastTeamsActivity', 'lastSharePointActivity', 'accountEnabled', 'hasLicense', 'licenses');

-- CreateEnum
CREATE TYPE "platform_performance"."CycleType" AS ENUM ('MONTHLY', 'QUARTERLY', 'SEMI_ANNUAL', 'ANNUAL', 'CUSTOM');

-- CreateEnum
CREATE TYPE "platform_performance"."CycleStatus" AS ENUM ('DRAFT', 'GOAL_SETTING', 'IN_PROGRESS', 'EVALUATING', 'CALIBRATING', 'CONFIRMING', 'COMPLETED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "platform_performance"."KpiAssignmentStatus" AS ENUM ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "platform_performance"."KpiDependencyStatus" AS ENUM ('PENDING', 'CONFIRMED', 'REJECTED');

-- CreateEnum
CREATE TYPE "platform_performance"."ResultConfirmStatus" AS ENUM ('PENDING', 'CONFIRMED', 'APPEALED', 'APPEAL_RESOLVED');

-- CreateEnum
CREATE TYPE "platform_performance"."KpiAssessmentStatus" AS ENUM ('PENDING', 'SELF_EVALUATED', 'MANAGER_EVALUATED', 'CONFIRMED');

-- CreateEnum
CREATE TYPE "platform_performance"."Evaluation360Status" AS ENUM ('DRAFT', 'IN_PROGRESS', 'COLLECTING', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_performance"."EvaluationTaskStatus" AS ENUM ('PENDING', 'SUBMITTED', 'EXPIRED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_performance"."RelationType" AS ENUM ('SELF', 'SUPERVISOR', 'PEER', 'SUBORDINATE', 'CROSS_FUNCTIONAL');

-- CreateEnum
CREATE TYPE "platform_site_attendance"."SiteGeoPolicy" AS ENUM ('STRICT_BLOCK', 'ALLOW_WITH_FLAG', 'SKIP');

-- CreateEnum
CREATE TYPE "platform_site_attendance"."SiteEventType" AS ENUM ('CHECK_IN', 'CHECK_OUT');

-- CreateEnum
CREATE TYPE "platform_site_attendance"."SiteAuthMethod" AS ENUM ('AUTHENTICATED', 'UNAUTHENTICATED');

-- CreateEnum
CREATE TYPE "platform_site_attendance"."SiteGeoStatus" AS ENUM ('VALID', 'OUT_OF_RANGE', 'LOW_ACCURACY', 'PERMISSION_DENIED', 'UNAVAILABLE', 'TIMEOUT', 'SKIPPED');

-- CreateEnum
CREATE TYPE "platform_site_attendance"."SiteCheckpointAccessMode" AS ENUM ('PUBLIC', 'SIGNED');

-- CreateEnum
CREATE TYPE "platform_tickets"."TicketStatus" AS ENUM ('OPEN', 'ASSIGNED', 'IN_PROGRESS', 'PENDING', 'RESOLVED', 'CLOSED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "platform_tickets"."TicketPriority" AS ENUM ('LOW', 'MEDIUM', 'HIGH', 'URGENT');

-- CreateEnum
CREATE TYPE "platform_tickets"."TicketSource" AS ENUM ('WEB', 'MOBILE', 'EMAIL', 'API', 'CHAT', 'PHONE', 'WALK_IN');

-- CreateEnum
CREATE TYPE "platform_tickets"."CommentType" AS ENUM ('COMMENT', 'STATUS_CHANGE', 'ASSIGNMENT', 'SYSTEM', 'AI_CONTEXT');

-- CreateEnum
CREATE TYPE "platform_tickets"."VisibilityScope" AS ENUM ('ALL', 'AGENT_ONLY', 'GROUP_ONLY', 'INTERNAL');

-- CreateEnum
CREATE TYPE "platform_tickets"."ActivityType" AS ENUM ('CREATED', 'STATUS_CHANGED', 'ASSIGNED', 'REASSIGNED', 'PRIORITY_CHANGED', 'COMMENTED', 'ATTACHMENT_ADDED', 'SLA_BREACHED', 'ESCALATED', 'RESOLVED', 'CLOSED', 'REOPENED');

-- CreateEnum
CREATE TYPE "platform_tickets"."AssignmentStrategy" AS ENUM ('ROUND_ROBIN', 'LOAD_BALANCE', 'MANUAL', 'SKILL_BASED');

-- CreateEnum
CREATE TYPE "robot_manager"."RobotStatus" AS ENUM ('ORDERED', 'IN_TRANSIT', 'BONDED', 'IN_STOCK', 'RESERVED', 'SOLD', 'DELIVERED', 'REPAIR', 'REPAIRED', 'CANCELLED');

-- CreateTable
CREATE TABLE "corp_approval"."approval_definitions" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "description" TEXT,
    "latest_version" INTEGER NOT NULL DEFAULT 1,
    "organization_id" UUID,
    "status" "corp_approval"."ProcessStatus" NOT NULL DEFAULT 'ACTIVE',
    "created_by" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_by" TEXT,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_versions" (
    "id" UUID NOT NULL,
    "definition_id" UUID NOT NULL,
    "version" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "process_model" JSONB NOT NULL,
    "settings" JSONB NOT NULL DEFAULT '{}',
    "form_schema" JSONB,
    "rule_config" JSONB NOT NULL DEFAULT '{}',
    "status" "corp_approval"."VersionStatus" NOT NULL DEFAULT 'DRAFT',
    "deployed_at" TIMESTAMPTZ(3),
    "deployed_by" TEXT,
    "change_log" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_instances" (
    "id" UUID NOT NULL,
    "version_id" UUID NOT NULL,
    "business_type" TEXT NOT NULL,
    "business_id" TEXT NOT NULL,
    "business_key" TEXT NOT NULL,
    "title" TEXT,
    "workflow_id" TEXT NOT NULL,
    "workflow_run_id" TEXT NOT NULL,
    "initiator_id" UUID NOT NULL,
    "region_id" TEXT,
    "status" "corp_approval"."InstanceStatus" NOT NULL DEFAULT 'RUNNING',
    "current_node_id" TEXT,
    "current_node" TEXT,
    "variables" JSONB NOT NULL DEFAULT '{}',
    "total_node_executions" INTEGER NOT NULL DEFAULT 0,
    "end_reason" TEXT,
    "end_comment" TEXT,
    "priority" INTEGER NOT NULL DEFAULT 0,
    "due_date" TIMESTAMPTZ(3),
    "start_time" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "end_time" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_node_instances" (
    "id" UUID NOT NULL,
    "instance_id" UUID NOT NULL,
    "node_id" TEXT NOT NULL,
    "node_name" TEXT NOT NULL,
    "node_type" TEXT NOT NULL,
    "status" "corp_approval"."NodeStatus" NOT NULL DEFAULT 'ACTIVE',
    "assignees" TEXT[],
    "approval_mode" TEXT,
    "execution_count" INTEGER NOT NULL DEFAULT 1,
    "result" JSONB,
    "start_time" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "end_time" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_tasks" (
    "id" UUID NOT NULL,
    "instance_id" UUID NOT NULL,
    "node_instance_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "type" "corp_approval"."ApprovalTaskType" NOT NULL DEFAULT 'USER_TASK',
    "assignee" UUID,
    "candidate_users" TEXT[],
    "candidate_groups" TEXT[],
    "owner" UUID,
    "status" "corp_approval"."ApprovalTaskStatus" NOT NULL DEFAULT 'CREATED',
    "version" INTEGER NOT NULL DEFAULT 0,
    "form_data" JSONB,
    "priority" INTEGER NOT NULL DEFAULT 0,
    "due_date" TIMESTAMPTZ(3),
    "reminder_count" INTEGER NOT NULL DEFAULT 0,
    "last_reminder_at" TIMESTAMPTZ(3),
    "auto_approved" BOOLEAN NOT NULL DEFAULT false,
    "auto_approve_reason" TEXT,
    "is_delegated" BOOLEAN NOT NULL DEFAULT false,
    "delegated_from" UUID,
    "delegated_at" TIMESTAMPTZ(3),
    "delegation_reason" TEXT,
    "delegation_type" "corp_approval"."DelegationType",
    "delegation_state" TEXT,
    "create_time" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "claim_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_task_logs" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "instance_id" UUID NOT NULL,
    "action" "corp_approval"."ApprovalTaskAction" NOT NULL,
    "operator_id" UUID NOT NULL,
    "comment" TEXT,
    "target_user_id" UUID,
    "target_node_id" TEXT,
    "add_sign_users" TEXT[],
    "form_data_changes" JSONB,
    "attachments" JSONB,
    "ip_address" TEXT,
    "user_agent" TEXT,
    "request_id" TEXT,
    "risk_level" "platform_audit"."RiskLevel",
    "admin_reason" TEXT,
    "metadata" JSONB,
    "action_time" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "corp_approval"."callback_retry_queue" (
    "id" UUID NOT NULL,
    "instance_id" UUID NOT NULL,
    "callback_type" TEXT NOT NULL,
    "callback_url" TEXT,
    "payload" JSONB NOT NULL,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 10,
    "next_retry_at" TIMESTAMPTZ(3) NOT NULL,
    "last_error" TEXT,
    "status" "corp_approval"."CallbackStatus" NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."reminder_queue" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "instance_id" UUID NOT NULL,
    "reminder_type" "corp_approval"."ReminderType" NOT NULL,
    "channels" TEXT[],
    "scheduled_at" TIMESTAMPTZ(3) NOT NULL,
    "executed_at" TIMESTAMPTZ(3),
    "status" "corp_approval"."ReminderStatus" NOT NULL DEFAULT 'SCHEDULED',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "corp_approval"."user_delegation_settings" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "delegate_user_id" UUID NOT NULL,
    "start_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),
    "scope" "corp_approval"."DelegationScope" NOT NULL DEFAULT 'ALL',
    "process_keys" TEXT[],
    "reason" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_admin_configs" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "value" JSONB NOT NULL,
    "description" TEXT,
    "updated_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_admin_exports" (
    "id" UUID NOT NULL,
    "status" "corp_approval"."ApprovalAdminExportStatus" NOT NULL DEFAULT 'PENDING',
    "format" VARCHAR(10) NOT NULL DEFAULT 'xlsx',
    "requested_by" UUID NOT NULL,
    "filters" JSONB NOT NULL DEFAULT '{}',
    "file_key" TEXT,
    "file_name" TEXT,
    "file_size" BIGINT,
    "content_type" TEXT,
    "total_rows" INTEGER,
    "error_message" TEXT,
    "started_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "expires_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_hr"."organizations" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "display_name" TEXT,
    "name_en" TEXT,
    "name_zh" TEXT,
    "legal_name" TEXT,
    "legal_representative" TEXT,
    "registration_number" TEXT,
    "tax_id" TEXT,
    "address" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "website" TEXT,
    "primary_region_id" UUID,
    "settings" JSONB NOT NULL DEFAULT '{}',
    "financial_config" JSONB NOT NULL DEFAULT '{}',
    "compliance_config" JSONB NOT NULL DEFAULT '{}',
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "order" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "established_at" DATE,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_hr"."organization_regions" (
    "id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "region_id" UUID NOT NULL,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "corp_hr"."regions" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "name_en" TEXT,
    "name_zh" TEXT,
    "timezone" TEXT,
    "currency" TEXT,
    "locale" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "order" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_hr"."departments" (
    "id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "parent_id" UUID,
    "head_id" UUID,
    "description" TEXT,
    "order" INTEGER NOT NULL DEFAULT 0,
    "tenant_id" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_hr"."positions" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "level" INTEGER NOT NULL DEFAULT 0,
    "description" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_hr"."user_departments" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "department_id" UUID NOT NULL,
    "position_id" UUID,
    "manager_id" UUID,
    "is_primary" BOOLEAN NOT NULL DEFAULT false,
    "title" TEXT,
    "joined_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "left_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_request"."expense_requests" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "creator_id" UUID NOT NULL,
    "amount" DECIMAL(12,2) NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'CNY',
    "category" "corp_request"."ExpenseCategory" NOT NULL,
    "workflow_instance_id" UUID,
    "status" "corp_request"."ExpenseStatus" NOT NULL DEFAULT 'DRAFT',
    "attachments" JSONB,
    "expense_date" TIMESTAMPTZ(3) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_request"."expense_items" (
    "id" UUID NOT NULL,
    "expense_id" UUID NOT NULL,
    "description" TEXT NOT NULL,
    "amount" DECIMAL(12,2) NOT NULL,
    "date" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_request"."purchase_requests" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "creator_id" UUID NOT NULL,
    "totalAmount" DECIMAL(12,2) NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'CNY',
    "vendor_name" TEXT NOT NULL,
    "vendor_contact" TEXT,
    "workflow_instance_id" UUID,
    "status" "corp_request"."PurchaseStatus" NOT NULL DEFAULT 'DRAFT',
    "attachments" JSONB,
    "expected_delivery_date" TIMESTAMPTZ(3),
    "actual_delivery_date" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "corp_request"."purchase_items" (
    "id" UUID NOT NULL,
    "purchase_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "quantity" INTEGER NOT NULL,
    "unitPrice" DECIMAL(12,2) NOT NULL,
    "amount" DECIMAL(12,2) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_request"."contract_requests" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "creator_id" UUID NOT NULL,
    "contract_type" "corp_request"."ContractType" NOT NULL,
    "partner_name" TEXT NOT NULL,
    "partner_contact" TEXT,
    "amount" DECIMAL(12,2) NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'CNY',
    "workflow_instance_id" UUID,
    "status" "corp_request"."ContractStatus" NOT NULL DEFAULT 'DRAFT',
    "seal_required" BOOLEAN NOT NULL DEFAULT false,
    "seal_applied_at" TIMESTAMPTZ(3),
    "attachments" JSONB,
    "sign_date" TIMESTAMPTZ(3),
    "start_date" TIMESTAMPTZ(3),
    "end_date" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."parts" (
    "id" UUID NOT NULL,
    "part_number" TEXT NOT NULL,
    "part_name_en" TEXT NOT NULL,
    "part_name_cn" TEXT,
    "unit" TEXT NOT NULL DEFAULT 'pcs',
    "source" TEXT,
    "specifications" TEXT,
    "remark" TEXT,
    "status" "mfg_inventory"."PartStatus" NOT NULL DEFAULT 'ACTIVE',
    "current_stock" INTEGER NOT NULL DEFAULT 0,
    "min_stock" INTEGER NOT NULL DEFAULT 0,
    "max_stock" INTEGER,
    "station" TEXT,
    "station_id" UUID,
    "warehouse_location" TEXT,
    "warehouse_id" UUID,
    "storage_location_id" UUID,
    "image_url" TEXT,
    "custom_fields" JSONB,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."stations" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name_en" TEXT NOT NULL,
    "name_cn" TEXT,
    "description" TEXT,
    "area" TEXT,
    "line" TEXT,
    "type" TEXT,
    "status" "mfg_inventory"."StationStatus" NOT NULL DEFAULT 'ACTIVE',
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."warehouses" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name_en" TEXT NOT NULL,
    "name_cn" TEXT,
    "description" TEXT,
    "address" TEXT,
    "contact_person" TEXT,
    "contact_phone" TEXT,
    "status" "mfg_inventory"."WarehouseStatus" NOT NULL DEFAULT 'ACTIVE',
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."storage_locations" (
    "id" UUID NOT NULL,
    "warehouse_id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name_en" TEXT NOT NULL,
    "name_cn" TEXT,
    "description" TEXT,
    "zone" TEXT,
    "aisle" TEXT,
    "rack" TEXT,
    "level" TEXT,
    "position" TEXT,
    "capacity" INTEGER,
    "status" "mfg_inventory"."StorageLocationStatus" NOT NULL DEFAULT 'ACTIVE',
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."part_labels" (
    "id" UUID NOT NULL,
    "part_id" UUID NOT NULL,
    "label_code" TEXT NOT NULL,
    "qr_code_data" TEXT NOT NULL,
    "template" TEXT NOT NULL DEFAULT 'standard',
    "size" TEXT NOT NULL DEFAULT '50x30',
    "print_count" INTEGER NOT NULL DEFAULT 0,
    "last_printed_at" TIMESTAMPTZ(3),
    "printed_by" TEXT,
    "status" "mfg_inventory"."LabelStatus" NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "mfg_inventory"."inventory_logs" (
    "id" UUID NOT NULL,
    "part_id" UUID NOT NULL,
    "operation_type" "mfg_inventory"."InventoryOperationType" NOT NULL,
    "quantity" INTEGER NOT NULL,
    "previous_stock" INTEGER NOT NULL,
    "new_stock" INTEGER NOT NULL,
    "station" TEXT,
    "warehouse_location" TEXT,
    "reason" TEXT,
    "reference_type" TEXT,
    "reference_id" UUID,
    "operator_id" UUID NOT NULL,
    "operator_name" TEXT NOT NULL,
    "scanned_code" TEXT,
    "device_info" JSONB,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "mfg_inventory"."stock_alerts" (
    "id" UUID NOT NULL,
    "part_id" UUID NOT NULL,
    "alert_type" "mfg_inventory"."StockAlertType" NOT NULL,
    "severity" "mfg_inventory"."AlertSeverity" NOT NULL,
    "current_stock" INTEGER NOT NULL,
    "threshold" INTEGER NOT NULL,
    "message" TEXT NOT NULL,
    "status" "mfg_inventory"."AlertStatus" NOT NULL DEFAULT 'PENDING',
    "acknowledged_by" TEXT,
    "acknowledged_at" TIMESTAMPTZ(3),
    "resolved_by" TEXT,
    "resolved_at" TIMESTAMPTZ(3),
    "resolution" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "mfg_inventory"."import_logs" (
    "id" UUID NOT NULL,
    "file_name" TEXT NOT NULL,
    "file_size" INTEGER NOT NULL,
    "file_hash" TEXT,
    "status" "mfg_inventory"."ImportStatus" NOT NULL DEFAULT 'PENDING',
    "total_rows" INTEGER NOT NULL DEFAULT 0,
    "success_rows" INTEGER NOT NULL DEFAULT 0,
    "failed_rows" INTEGER NOT NULL DEFAULT 0,
    "skipped_rows" INTEGER NOT NULL DEFAULT 0,
    "errors" JSONB,
    "summary" JSONB,
    "imported_by" TEXT NOT NULL,
    "imported_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "metadata" JSONB NOT NULL DEFAULT '{}',

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

-- CreateTable
CREATE TABLE "mfg_inventory"."part_column_configs" (
    "id" UUID NOT NULL,
    "user_id" UUID,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "is_template" BOOLEAN NOT NULL DEFAULT false,
    "columns" JSONB NOT NULL,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "mfg_inventory"."part_groups" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "parts_count" INTEGER NOT NULL DEFAULT 0,
    "fields_count" INTEGER NOT NULL DEFAULT 0,
    "created_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_by" UUID,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "mfg_inventory"."group_custom_fields" (
    "id" UUID NOT NULL,
    "group_id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "name_en" TEXT NOT NULL,
    "type" "mfg_inventory"."GroupFieldType" NOT NULL,
    "required" BOOLEAN NOT NULL DEFAULT false,
    "visible" BOOLEAN NOT NULL DEFAULT true,
    "editable" BOOLEAN NOT NULL DEFAULT true,
    "searchable" BOOLEAN NOT NULL DEFAULT true,
    "options" JSONB,
    "validation" JSONB,
    "description" TEXT,
    "placeholder" TEXT,
    "help_text" TEXT,
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "created_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_by" UUID,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "name_cn" TEXT,

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

-- CreateTable
CREATE TABLE "mfg_inventory"."part_group_relations" (
    "part_id" UUID NOT NULL,
    "group_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" UUID,

    CONSTRAINT "part_group_relations_pkey" PRIMARY KEY ("part_id","group_id")
);

-- CreateTable
CREATE TABLE "platform_ai"."ai_conversations" (
    "id" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "title" VARCHAR(255),
    "status" "platform_ai"."AIConversationStatus" NOT NULL DEFAULT 'ACTIVE',
    "category" "platform_ai"."AIConversationCategory" NOT NULL DEFAULT 'GENERAL',
    "tags" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(6) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_messages" (
    "id" UUID NOT NULL,
    "conversationId" UUID NOT NULL,
    "role" "platform_ai"."AIMessageRole" NOT NULL,
    "source" "platform_ai"."AIMessageSource" NOT NULL DEFAULT 'USER',
    "content" TEXT NOT NULL,
    "tokens" INTEGER,
    "metadata" JSONB,
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_message_feedbacks" (
    "id" UUID NOT NULL,
    "messageId" UUID NOT NULL,
    "type" "platform_ai"."AIFeedbackType" NOT NULL,
    "comment" VARCHAR(500),
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_assistant_tickets" (
    "id" UUID NOT NULL,
    "conversationId" UUID NOT NULL,
    "category" "platform_ai"."AITicketCategory" NOT NULL,
    "priority" "platform_ai"."AITicketPriority" NOT NULL DEFAULT 'MEDIUM',
    "status" "platform_ai"."AITicketStatus" NOT NULL DEFAULT 'OPEN',
    "title" VARCHAR(255) NOT NULL,
    "description" TEXT NOT NULL,
    "assigneeId" UUID,
    "assigneeTeam" "platform_ai"."AIAssigneeTeam",
    "assignedAt" TIMESTAMPTZ(6),
    "resolvedAt" TIMESTAMPTZ(6),
    "resolution" TEXT,
    "externalTicketId" VARCHAR(100),
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(6) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_knowledge_fixes" (
    "id" UUID NOT NULL,
    "messageId" UUID NOT NULL,
    "correctAnswer" TEXT NOT NULL,
    "contributorId" UUID NOT NULL,
    "status" "platform_ai"."AIKnowledgeFixStatus" NOT NULL DEFAULT 'PENDING',
    "reviewerId" UUID,
    "reviewNote" VARCHAR(500),
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "reviewedAt" TIMESTAMPTZ(6),

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_prompt_templates" (
    "id" UUID NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "category" "platform_ai"."AIConversationCategory" NOT NULL,
    "content" TEXT NOT NULL,
    "version" INTEGER NOT NULL DEFAULT 1,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "createdBy" UUID NOT NULL,
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(6) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_configurations" (
    "id" UUID NOT NULL,
    "key" VARCHAR(100) NOT NULL,
    "value" TEXT NOT NULL,
    "category" VARCHAR(50) NOT NULL DEFAULT 'GENERAL',
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(6) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ai"."ai_usage_stats" (
    "id" UUID NOT NULL,
    "date" DATE NOT NULL,
    "category" "platform_ai"."AIConversationCategory" NOT NULL,
    "totalCalls" INTEGER NOT NULL DEFAULT 0,
    "successCalls" INTEGER NOT NULL DEFAULT 0,
    "failedCalls" INTEGER NOT NULL DEFAULT 0,
    "totalTokens" INTEGER NOT NULL DEFAULT 0,
    "avgTokensPerCall" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "avgResponseTime" INTEGER NOT NULL DEFAULT 0,
    "likesCount" INTEGER NOT NULL DEFAULT 0,
    "dislikesCount" INTEGER NOT NULL DEFAULT 0,
    "escalationCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMPTZ(6) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_audit"."audit_log" (
    "id" UUID NOT NULL,
    "region" TEXT NOT NULL DEFAULT 'cn',
    "tenant_id" TEXT NOT NULL DEFAULT 'default',
    "who" TEXT NOT NULL,
    "what" TEXT NOT NULL,
    "when" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "where" TEXT NOT NULL,
    "why" TEXT,
    "how" TEXT NOT NULL,
    "module" TEXT NOT NULL,
    "action" "platform_audit"."AuditAction" NOT NULL,
    "entity_type" TEXT NOT NULL,
    "entity_id" UUID NOT NULL,
    "old_value" JSONB,
    "new_value" JSONB,
    "changes" JSONB,
    "user_id" UUID,
    "session_id" UUID NOT NULL,
    "trace_id" UUID NOT NULL,
    "request_id" UUID NOT NULL,
    "ip_address" TEXT NOT NULL,
    "user_agent" TEXT NOT NULL,
    "device_id" UUID,
    "geo_location" TEXT,
    "business_type" TEXT,
    "business_key" TEXT,
    "status" "platform_audit"."AuditStatus" NOT NULL DEFAULT 'SUCCESS',
    "error_message" TEXT,
    "duration" INTEGER,
    "is_financial" BOOLEAN NOT NULL DEFAULT false,
    "is_sensitive" BOOLEAN NOT NULL DEFAULT false,
    "risk_level" "platform_audit"."RiskLevel" NOT NULL DEFAULT 'MEDIUM',
    "compliance_level" "platform_audit"."ComplianceLevel" NOT NULL DEFAULT 'MEDIUM',
    "retention_years" INTEGER NOT NULL DEFAULT 5,
    "previous_hash" TEXT,
    "current_hash" TEXT NOT NULL,
    "signature" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "archived_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_audit"."audit_database_change_log" (
    "id" UUID NOT NULL,
    "table_name" TEXT NOT NULL,
    "operation" "platform_audit"."DbOperation" NOT NULL,
    "record_id" UUID NOT NULL,
    "old_data" JSONB,
    "new_data" JSONB,
    "changed_fields" TEXT[],
    "user_id" UUID NOT NULL,
    "audit_log_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_audit"."audit_sensitive_operation_log" (
    "id" UUID NOT NULL,
    "operation_type" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "requires_approval" BOOLEAN NOT NULL DEFAULT false,
    "approved_by" TEXT,
    "approval_time" TIMESTAMPTZ(3),
    "risk_level" "platform_audit"."RiskLevel" NOT NULL,
    "mfa_verified" BOOLEAN NOT NULL DEFAULT false,
    "user_id" UUID NOT NULL,
    "audit_log_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_audit"."audit_integrity_check_log" (
    "id" UUID NOT NULL,
    "region" TEXT NOT NULL DEFAULT 'cn',
    "tenant_id" TEXT NOT NULL DEFAULT 'default',
    "check_type" "platform_audit"."IntegrityCheckType" NOT NULL,
    "start_time" TIMESTAMPTZ(3) NOT NULL,
    "end_time" TIMESTAMPTZ(3) NOT NULL,
    "record_count" INTEGER NOT NULL,
    "pass_count" INTEGER NOT NULL,
    "fail_count" INTEGER NOT NULL,
    "failures" JSONB,
    "status" "platform_audit"."CheckStatus" NOT NULL,
    "error_message" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_automation"."automation_tasks" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "description" TEXT,
    "type" "platform_automation"."AutomationTaskType" NOT NULL,
    "status" "platform_automation"."AutomationTaskStatus" NOT NULL DEFAULT 'ACTIVE',
    "schedule_type" TEXT NOT NULL,
    "schedule_config" JSONB NOT NULL DEFAULT '{}',
    "config" JSONB NOT NULL DEFAULT '{}',
    "timeout" INTEGER NOT NULL DEFAULT 300000,
    "retry_count" INTEGER NOT NULL DEFAULT 3,
    "last_run_at" TIMESTAMPTZ(3),
    "last_status" "platform_automation"."AutomationExecutionStatus",
    "next_run_at" TIMESTAMPTZ(3),
    "total_runs" INTEGER NOT NULL DEFAULT 0,
    "success_runs" INTEGER NOT NULL DEFAULT 0,
    "failed_runs" INTEGER NOT NULL DEFAULT 0,
    "created_by_id" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."automation_executions" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "status" "platform_automation"."AutomationExecutionStatus" NOT NULL,
    "started_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMPTZ(3),
    "duration" INTEGER,
    "result" JSONB,
    "error" TEXT,
    "logs" TEXT,
    "trigger_type" TEXT NOT NULL,
    "triggered_by" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_sync_configs" (
    "id" UUID NOT NULL,
    "task_code" TEXT NOT NULL,
    "form_uuid" TEXT,
    "app_type" TEXT,
    "fieldMapping" JSONB NOT NULL DEFAULT '{}',
    "sync_direction" TEXT NOT NULL,
    "search_condition" JSONB,
    "lookback_minutes" INTEGER NOT NULL DEFAULT 30,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_leave_codes" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "year" INTEGER,
    "category" TEXT NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_leave_quota_snapshots" (
    "id" UUID NOT NULL,
    "user_id" TEXT NOT NULL,
    "employee_name" TEXT NOT NULL,
    "employee_number" TEXT NOT NULL DEFAULT '',
    "leave_code" TEXT NOT NULL,
    "leave_type" TEXT NOT NULL,
    "quota_cycle" TEXT NOT NULL DEFAULT '',
    "total_days" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "used_days" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "remaining_days" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "start_date" DATE,
    "end_date" DATE,
    "usage_records" JSONB,
    "release_records" JSONB,
    "snapshot_at" TIMESTAMPTZ(3) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."sap_purchase_sync_records" (
    "id" UUID NOT NULL,
    "serial_number" TEXT NOT NULL,
    "form_instance_id" TEXT NOT NULL,
    "applicant_name" TEXT NOT NULL,
    "budget_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "sap_pr_number" TEXT,
    "sap_status" TEXT NOT NULL,
    "sap_messages" JSONB,
    "sap_raw_response" JSONB,
    "approver_chain" TEXT,
    "synced_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_employees" (
    "id" UUID NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL DEFAULT '',
    "employee_number" TEXT NOT NULL DEFAULT '',
    "department" TEXT NOT NULL DEFAULT '',
    "position" TEXT NOT NULL DEFAULT '',
    "rank" TEXT NOT NULL DEFAULT '',
    "phone" TEXT NOT NULL DEFAULT '',
    "country" TEXT NOT NULL DEFAULT '',
    "work_place" TEXT NOT NULL DEFAULT '',
    "contract_company" TEXT NOT NULL DEFAULT '',
    "bank" TEXT NOT NULL DEFAULT '',
    "bank_card_last4" TEXT NOT NULL DEFAULT '',
    "monthly_standard_hours" TEXT NOT NULL DEFAULT '',
    "work_hour_limit" TEXT NOT NULL DEFAULT '',
    "join_date" TIMESTAMPTZ(3),
    "work_start_date" TIMESTAMPTZ(3),
    "tenure_days" INTEGER NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL DEFAULT '正常',
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_employee_employment_periods" (
    "id" UUID NOT NULL,
    "user_id" TEXT NOT NULL,
    "period_index" INTEGER NOT NULL,
    "join_date" DATE NOT NULL,
    "leave_date" DATE,
    "count_in_tenure" BOOLEAN NOT NULL DEFAULT true,
    "note" TEXT NOT NULL DEFAULT '',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_employee_suspension_periods" (
    "id" UUID NOT NULL,
    "user_id" TEXT NOT NULL,
    "start_date" DATE NOT NULL,
    "end_date" DATE,
    "reason" TEXT NOT NULL DEFAULT '',
    "note" TEXT NOT NULL DEFAULT '',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."sap_purchase_cache" (
    "id" UUID NOT NULL,
    "serial_number" TEXT NOT NULL,
    "form_instance_id" TEXT NOT NULL,
    "sap_env" TEXT NOT NULL,
    "applicant_name" TEXT NOT NULL DEFAULT '',
    "applicant_dept" TEXT NOT NULL DEFAULT '',
    "budget_number" TEXT NOT NULL DEFAULT '',
    "budget_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "purchase_project_description" TEXT NOT NULL DEFAULT '',
    "application_date" TEXT NOT NULL DEFAULT '',
    "demand_category" TEXT NOT NULL DEFAULT '',
    "purchasing_company_name" TEXT NOT NULL DEFAULT '',
    "material_group" TEXT NOT NULL DEFAULT '',
    "form_data" JSONB NOT NULL DEFAULT '{}',
    "yida_modified_at" TEXT NOT NULL DEFAULT '',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_automation"."adp_pto_schedules" (
    "id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "adp_aoid" VARCHAR(64) NOT NULL,
    "leave_date" DATE NOT NULL,
    "start_time" TIMESTAMPTZ(3) NOT NULL,
    "end_time" TIMESTAMPTZ(3) NOT NULL,
    "adp_entry_id" VARCHAR(128) NOT NULL,
    "synced_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_automation"."dingtalk_annual_leave_release_plans" (
    "id" UUID NOT NULL,
    "user_id" TEXT NOT NULL,
    "employee_name" TEXT NOT NULL DEFAULT '',
    "employee_number" TEXT NOT NULL DEFAULT '',
    "year" INTEGER NOT NULL,
    "status" TEXT NOT NULL DEFAULT '正常',
    "join_date" DATE,
    "work_start_date" DATE,
    "adjustment_days" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "not_count_days" INTEGER NOT NULL DEFAULT 0,
    "total_days" INTEGER NOT NULL DEFAULT 0,
    "release_schedule" JSONB NOT NULL DEFAULT '[]',
    "last_calculated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_devtracker"."dev_items" (
    "id" UUID NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "item_type" "platform_devtracker"."DevItemType" NOT NULL,
    "status" "platform_devtracker"."DevItemStatus" NOT NULL DEFAULT 'DRAFT',
    "priority" "platform_devtracker"."DevItemPriority" NOT NULL,
    "severity" "platform_devtracker"."DevItemSeverity",
    "module_key" VARCHAR(100),
    "parent_id" UUID,
    "owner_id" UUID NOT NULL,
    "reporter_id" UUID NOT NULL,
    "reviewer_id" UUID,
    "reviewed_at" TIMESTAMPTZ(3),
    "start_at" TIMESTAMPTZ(3),
    "dev_eta_at" TIMESTAMPTZ(3),
    "test_eta_at" TIMESTAMPTZ(3),
    "eta_at" TIMESTAMPTZ(3),
    "dev_completed_at" TIMESTAMPTZ(3),
    "test_completed_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "content" JSONB,
    "labels" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_devtracker"."dev_item_sequences" (
    "prefix" VARCHAR(50) NOT NULL,
    "current" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "dev_item_sequences_pkey" PRIMARY KEY ("prefix")
);

-- CreateTable
CREATE TABLE "platform_devtracker"."release_notes" (
    "id" UUID NOT NULL,
    "version" VARCHAR(50) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "content" JSONB,
    "released_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_feedback"."feedbacks" (
    "id" UUID NOT NULL,
    "type" "platform_feedback"."FeedbackType" NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "content" TEXT NOT NULL,
    "attachments" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "page_url" VARCHAR(500),
    "user_agent" VARCHAR(500),
    "status" "platform_feedback"."FeedbackStatus" NOT NULL DEFAULT 'PENDING',
    "priority" "platform_feedback"."FeedbackPriority",
    "admin_note" TEXT,
    "admin_reply" TEXT,
    "assignee_id" UUID,
    "resolved_at" TIMESTAMPTZ(3),
    "user_id" UUID NOT NULL,
    "region" VARCHAR(10) NOT NULL DEFAULT 'CN',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_flow_diagram"."flow_diagram" (
    "id" UUID NOT NULL,
    "title" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "mermaid_source" TEXT NOT NULL,
    "previous_source" TEXT,
    "tags" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "color_preset" VARCHAR(20) NOT NULL DEFAULT 'default',
    "node_count" INTEGER,
    "created_by_id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."form_definitions" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "slug_history" JSONB,
    "aliases" JSONB,
    "name" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "description" TEXT,
    "icon" TEXT,
    "color" TEXT,
    "default_locale" TEXT NOT NULL,
    "supported_locales" JSONB NOT NULL,
    "latest_version" INTEGER NOT NULL,
    "status" "platform_form"."FormStatus" NOT NULL,
    "requires_approval" BOOLEAN NOT NULL DEFAULT false,
    "approval_process_key" TEXT,
    "organization_id" UUID,
    "created_by" UUID NOT NULL,
    "updated_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."form_versions" (
    "id" UUID NOT NULL,
    "definition_id" UUID NOT NULL,
    "version" INTEGER NOT NULL,
    "schema" JSONB NOT NULL,
    "ui_schema" JSONB,
    "view_schema" JSONB,
    "validation" JSONB,
    "changelog" TEXT,
    "name_i18n" JSONB NOT NULL,
    "description_i18n" JSONB,
    "is_default" BOOLEAN NOT NULL,
    "status" "platform_form"."FormVersionStatus" NOT NULL,
    "reviewed_by" UUID,
    "reviewed_at" TIMESTAMPTZ(3),
    "review_comment" TEXT,
    "published_at" TIMESTAMPTZ(3),
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_form"."form_instances" (
    "id" UUID NOT NULL,
    "form_definition_id" UUID NOT NULL,
    "form_version_id" UUID NOT NULL,
    "form_key" TEXT NOT NULL,
    "form_version" INTEGER NOT NULL,
    "snapshot_id" UUID,
    "business_key" TEXT NOT NULL,
    "region_id" TEXT,
    "data" JSONB NOT NULL,
    "status" "platform_form"."FormInstanceStatus" NOT NULL,
    "created_by" UUID NOT NULL,
    "updated_by" UUID,
    "submitted_by" UUID,
    "submitted_at" TIMESTAMPTZ(3),
    "approval_instance_id" UUID,
    "approval_status" TEXT,
    "approval_start_time" TIMESTAMPTZ(3),
    "approval_end_time" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_form"."form_translations" (
    "id" UUID NOT NULL,
    "version_id" UUID NOT NULL,
    "locale" TEXT NOT NULL,
    "translations" JSONB NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."form_templates" (
    "id" UUID NOT NULL,
    "name_i18n" JSONB NOT NULL,
    "description_i18n" JSONB,
    "category" TEXT NOT NULL,
    "icon" TEXT,
    "color" TEXT,
    "template" JSONB NOT NULL,
    "is_builtin" BOOLEAN NOT NULL,
    "is_public" BOOLEAN NOT NULL,
    "created_by" UUID NOT NULL,
    "updated_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."release_snapshots" (
    "id" UUID NOT NULL,
    "form_definition_id" UUID NOT NULL,
    "form_version_id" UUID NOT NULL,
    "process_definition_id" UUID,
    "process_version_id" UUID,
    "status" "platform_form"."ReleaseSnapshotStatus" NOT NULL DEFAULT 'DRAFT',
    "version_name" TEXT,
    "release_note" TEXT,
    "submitted_by" UUID,
    "submitted_at" TIMESTAMPTZ(3),
    "submit_comment" TEXT,
    "reviewed_by" UUID,
    "reviewed_at" TIMESTAMPTZ(3),
    "review_comment" TEXT,
    "published_by" UUID,
    "published_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."form_webhooks" (
    "id" UUID NOT NULL,
    "organization_id" UUID,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "url" TEXT NOT NULL,
    "secret" TEXT NOT NULL,
    "events" JSONB NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "max_retries" INTEGER NOT NULL DEFAULT 3,
    "timeout_ms" INTEGER NOT NULL DEFAULT 5000,
    "headers" JSONB,
    "created_by" UUID NOT NULL,
    "updated_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_form"."form_webhook_logs" (
    "id" UUID NOT NULL,
    "webhook_id" UUID NOT NULL,
    "event_type" TEXT NOT NULL,
    "payload" JSONB NOT NULL,
    "status_code" INTEGER,
    "response" TEXT,
    "success" BOOLEAN NOT NULL,
    "error" TEXT,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "duration" INTEGER,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_iam"."users" (
    "id" UUID NOT NULL,
    "username" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password_hash" TEXT,
    "display_name" TEXT NOT NULL,
    "avatar" TEXT,
    "phone" TEXT,
    "status" "platform_iam"."UserStatus" NOT NULL DEFAULT 'ACTIVE',
    "source" "platform_iam"."UserSource" NOT NULL DEFAULT 'LOCAL',
    "ldap_dn" TEXT,
    "employee_id" TEXT,
    "ldap_synced_at" TIMESTAMPTZ(3),
    "external_id" TEXT,
    "external_source" TEXT,
    "adp_aoid" VARCHAR(64),
    "adp_linked_at" TIMESTAMPTZ(3),
    "tenant_id" TEXT,
    "region" TEXT NOT NULL DEFAULT 'CN',
    "work_city" VARCHAR(100),
    "hired_at" TIMESTAMPTZ(3),
    "terminated_at" TIMESTAMPTZ(3),
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_iam"."roles" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "description" TEXT,
    "is_built_in" BOOLEAN NOT NULL DEFAULT false,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_iam"."permissions" (
    "id" UUID NOT NULL,
    "resource" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "description" TEXT,
    "module" TEXT,
    "is_built_in" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_iam"."workflow_roles" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "description" TEXT,
    "rule_type" "platform_iam"."WorkflowRuleType" NOT NULL,
    "rule_config" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_iam"."workflow_role_user_rel" (
    "id" UUID NOT NULL,
    "workflow_role_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_iam"."user_role_rel" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "organization_id" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_reviewed_at" TIMESTAMPTZ(3),
    "last_reviewed_by" UUID,
    "review_comment" TEXT,

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

-- CreateTable
CREATE TABLE "platform_iam"."role_permission_rel" (
    "id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "permission_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_iam"."data_scopes" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "scope_type" "platform_iam"."DataScopeType" NOT NULL,
    "rules" JSONB,
    "is_built_in" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_iam"."role_data_scope_rel" (
    "id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "data_scope_id" UUID NOT NULL,
    "resource" TEXT NOT NULL DEFAULT '*',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_iam"."ai_tool_grants" (
    "id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "tool_name" VARCHAR(128) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID,

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

-- CreateTable
CREATE TABLE "platform_iam"."ai_tool_grants_user" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "tool_name" VARCHAR(128) NOT NULL,
    "effect" VARCHAR(10) NOT NULL DEFAULT 'grant',
    "reason" VARCHAR(500),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID,

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

-- CreateTable
CREATE TABLE "platform_iam"."permission_delegations" (
    "id" UUID NOT NULL,
    "from_user_id" UUID NOT NULL,
    "to_user_id" UUID NOT NULL,
    "resource" TEXT NOT NULL DEFAULT '*',
    "valid_from" TIMESTAMPTZ(3) NOT NULL,
    "valid_to" TIMESTAMPTZ(3) NOT NULL,
    "reason" TEXT NOT NULL,
    "organization_id" UUID,
    "created_by_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "revoked_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_iam"."field_permissions" (
    "id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "resource" TEXT NOT NULL,
    "field" TEXT NOT NULL,
    "access" "platform_iam"."FieldAccess" NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_iam"."iam_audit_log" (
    "id" UUID NOT NULL,
    "actor" UUID NOT NULL,
    "action" VARCHAR(20) NOT NULL,
    "resource" VARCHAR(64) NOT NULL,
    "target_id" UUID,
    "before" JSONB,
    "after" JSONB,
    "ip" TEXT,
    "user_agent" TEXT,
    "timestamp" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."knowledge_articles" (
    "id" UUID NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "content" TEXT NOT NULL,
    "status" "platform_knowledge"."KnowledgeArticleStatus" NOT NULL DEFAULT 'DRAFT',
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sp_document_index" (
    "id" UUID NOT NULL,
    "sp_item_id" VARCHAR(255) NOT NULL,
    "sp_drive_id" VARCHAR(255) NOT NULL,
    "sp_site_id" VARCHAR(255) NOT NULL,
    "title" VARCHAR(500) NOT NULL,
    "web_url" TEXT NOT NULL,
    "file_type" VARCHAR(100),
    "file_extension" VARCHAR(20),
    "size" BIGINT,
    "sp_etag" VARCHAR(255),
    "doc_authority_level" "platform_knowledge"."DocAuthorityLevel" NOT NULL DEFAULT 'DRAFT',
    "doc_lifecycle_status" "platform_knowledge"."DocLifecycleStatus" NOT NULL DEFAULT 'DRAFT',
    "doc_type" "platform_knowledge"."DocType" NOT NULL DEFAULT 'GENERAL',
    "created_by" VARCHAR(255),
    "last_modified_by" VARCHAR(255),
    "sp_created_at" TIMESTAMPTZ(3),
    "sp_modified_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sp_folder_index" (
    "id" UUID NOT NULL,
    "sp_item_id" VARCHAR(255) NOT NULL,
    "sp_drive_id" VARCHAR(255) NOT NULL,
    "sp_site_id" VARCHAR(255) NOT NULL,
    "title" VARCHAR(500) NOT NULL,
    "folder_path" VARCHAR(1000) NOT NULL,
    "web_url" TEXT NOT NULL,
    "created_by" VARCHAR(255),
    "last_modified_by" VARCHAR(255),
    "sp_created_at" TIMESTAMPTZ(3),
    "sp_modified_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."ragflow_documents" (
    "id" UUID NOT NULL,
    "source_type" "platform_knowledge"."RagflowSourceType" NOT NULL,
    "source_id" UUID NOT NULL,
    "ragflow_document_id" VARCHAR(255) NOT NULL,
    "dataset_id" VARCHAR(255) NOT NULL,
    "content_hash" VARCHAR(64) NOT NULL,
    "sync_status" "platform_knowledge"."SyncStatus" NOT NULL DEFAULT 'PENDING',
    "sync_error" TEXT,
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."ai_qa_logs" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "question" TEXT NOT NULL,
    "answer" TEXT NOT NULL,
    "source_documents" JSONB,
    "confidence" DOUBLE PRECISION,
    "model_used" VARCHAR(100),
    "tokens_used" INTEGER,
    "cost" DOUBLE PRECISION,
    "response_time_ms" INTEGER,
    "feedback" "platform_knowledge"."QAFeedback",
    "feedback_comment" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."search_logs" (
    "id" UUID NOT NULL,
    "user_id_hash" VARCHAR(64) NOT NULL,
    "query" TEXT NOT NULL,
    "result_count" INTEGER NOT NULL,
    "response_time_ms" INTEGER NOT NULL,
    "search_type" "platform_knowledge"."SearchType" NOT NULL,
    "filters" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."document_views" (
    "id" UUID NOT NULL,
    "sp_document_id" UUID,
    "article_id" UUID,
    "user_id" UUID NOT NULL,
    "viewed_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "view_date" DATE NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."document_likes" (
    "id" UUID NOT NULL,
    "sp_document_id" UUID,
    "article_id" UUID,
    "user_id" UUID NOT NULL,
    "type" "platform_knowledge"."LikeType" NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."document_comments" (
    "id" UUID NOT NULL,
    "sp_document_id" UUID,
    "article_id" UUID,
    "user_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "parent_id" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_knowledge"."user_mentions" (
    "id" UUID NOT NULL,
    "comment_id" UUID NOT NULL,
    "mentioned_user_id" UUID NOT NULL,
    "read_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sync_tasks" (
    "id" UUID NOT NULL,
    "task_type" VARCHAR(50) NOT NULL,
    "trigger_source" VARCHAR(50),
    "status" "platform_knowledge"."SyncStatus" NOT NULL DEFAULT 'PENDING',
    "total_items" INTEGER,
    "processed_items" INTEGER NOT NULL DEFAULT 0,
    "processing_items" INTEGER NOT NULL DEFAULT 0,
    "failed_items" INTEGER NOT NULL DEFAULT 0,
    "skipped_items" INTEGER NOT NULL DEFAULT 0,
    "processed_tokens" INTEGER NOT NULL DEFAULT 0,
    "processed_chunks" INTEGER NOT NULL DEFAULT 0,
    "error" TEXT,
    "started_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "last_progress_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sync_task_skipped_items" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "source_type" "platform_knowledge"."RagflowSourceType" NOT NULL,
    "source_id" UUID NOT NULL,
    "filename" VARCHAR(255) NOT NULL,
    "file_extension" VARCHAR(20),
    "mime_type" VARCHAR(255),
    "reason" TEXT NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sync_task_processed_items" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "source_type" "platform_knowledge"."RagflowSourceType" NOT NULL,
    "source_id" UUID NOT NULL,
    "filename" VARCHAR(255) NOT NULL,
    "file_extension" VARCHAR(20),
    "mime_type" VARCHAR(255),
    "ragflow_document_id" VARCHAR(255),
    "token_count" INTEGER,
    "chunk_count" INTEGER,
    "source_size_bytes" BIGINT,
    "status" "platform_knowledge"."SyncItemStatus" NOT NULL DEFAULT 'PROCESSING',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sync_task_failed_items" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "source_type" "platform_knowledge"."RagflowSourceType" NOT NULL,
    "source_id" UUID NOT NULL,
    "filename" VARCHAR(255) NOT NULL,
    "file_extension" VARCHAR(20),
    "mime_type" VARCHAR(255),
    "error" TEXT NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."sharepoint_sync_cursors" (
    "id" UUID NOT NULL,
    "drive_id" VARCHAR(255) NOT NULL,
    "scope_path" VARCHAR(500) NOT NULL,
    "delta_link" TEXT NOT NULL,
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_knowledge"."webhook_subscriptions" (
    "id" UUID NOT NULL,
    "subscription_id" VARCHAR(255) NOT NULL,
    "drive_id" VARCHAR(255) NOT NULL,
    "resource" VARCHAR(500) NOT NULL,
    "expiration_date_time" TIMESTAMPTZ(3) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_logging"."system_log" (
    "id" UUID NOT NULL,
    "trace_id" TEXT NOT NULL,
    "span_id" TEXT,
    "parent_span_id" TEXT,
    "request_id" TEXT NOT NULL,
    "level" "platform_logging"."LogLevel" NOT NULL DEFAULT 'INFO',
    "message" TEXT,
    "method" VARCHAR(10),
    "url" VARCHAR(500),
    "status_code" INTEGER,
    "duration_ms" INTEGER,
    "user_id" UUID,
    "user_name" TEXT,
    "client_ip" TEXT,
    "user_agent" TEXT,
    "service" TEXT NOT NULL DEFAULT 'Backend',
    "region" TEXT NOT NULL DEFAULT 'CN',
    "instance" TEXT,
    "error_type" TEXT,
    "error_message" TEXT,
    "error_code" TEXT,
    "error_stack" TEXT,
    "request_body" JSONB,
    "response_body" JSONB,
    "metadata" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_logging"."log_config" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "value" JSONB NOT NULL,
    "description" TEXT,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "updated_by" TEXT,

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

-- CreateTable
CREATE TABLE "platform_logging"."log_alert" (
    "id" UUID NOT NULL,
    "type" "platform_logging"."LogAlertType" NOT NULL,
    "severity" "platform_logging"."LogAlertSeverity" NOT NULL DEFAULT 'WARNING',
    "message" TEXT NOT NULL,
    "context" JSONB,
    "channels" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "status" "platform_logging"."LogAlertStatus" NOT NULL DEFAULT 'SENT',
    "error_message" TEXT,
    "trace_id" TEXT,
    "log_id" UUID,
    "acknowledged_at" TIMESTAMPTZ(3),
    "acknowledged_by" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_logging"."log_cleanup_record" (
    "id" UUID NOT NULL,
    "type" TEXT NOT NULL,
    "older_than_days" INTEGER NOT NULL,
    "dry_run" BOOLEAN NOT NULL DEFAULT false,
    "deleted_count" INTEGER NOT NULL DEFAULT 0,
    "freed_bytes" BIGINT NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "error_message" TEXT,
    "started_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meetings" (
    "id" VARCHAR(32) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "start_time" TIMESTAMPTZ(3) NOT NULL,
    "end_time" TIMESTAMPTZ(3) NOT NULL,
    "timezone" VARCHAR(50) NOT NULL DEFAULT 'UTC',
    "location" VARCHAR(200),
    "type" "platform_meeting_attendance"."MeetingType" NOT NULL DEFAULT 'OFFLINE',
    "status" "platform_meeting_attendance"."MeetingStatus" NOT NULL DEFAULT 'SCHEDULED',
    "qr_code_online" TEXT,
    "qr_code_offline" TEXT,
    "creator_id" UUID NOT NULL,
    "series_id" VARCHAR(32),
    "instance_number" INTEGER,
    "is_series_master" BOOLEAN NOT NULL DEFAULT false,
    "has_custom_attendees" BOOLEAN NOT NULL DEFAULT false,
    "city" VARCHAR(100),
    "enforce_checkin_mode" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_series" (
    "id" VARCHAR(32) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "pattern" "platform_meeting_attendance"."RecurrencePattern" NOT NULL,
    "frequency" INTEGER NOT NULL DEFAULT 1,
    "start_date" TIMESTAMPTZ(3) NOT NULL,
    "end_date" TIMESTAMPTZ(3),
    "max_occurrences" INTEGER,
    "timezone" VARCHAR(50) NOT NULL DEFAULT 'UTC',
    "location" VARCHAR(200),
    "type" "platform_meeting_attendance"."MeetingType" NOT NULL DEFAULT 'OFFLINE',
    "creator_id" UUID NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "city" VARCHAR(100),
    "enforce_checkin_mode" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."attendances" (
    "id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "meeting_id" VARCHAR(32) NOT NULL,
    "status" "platform_meeting_attendance"."AttendanceStatus" NOT NULL DEFAULT 'ABSENT',
    "checkin_time" TIMESTAMPTZ(3),
    "checkout_time" TIMESTAMPTZ(3),
    "is_late" BOOLEAN NOT NULL DEFAULT false,
    "is_early_leave" BOOLEAN NOT NULL DEFAULT false,
    "notes" TEXT,
    "checkin_type" "platform_meeting_attendance"."CheckinType",
    "device_id" VARCHAR(100),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_required_attendees" (
    "id" VARCHAR(32) NOT NULL,
    "meeting_id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "role" "platform_meeting_attendance"."AttendeeRole" NOT NULL DEFAULT 'OPTIONAL_ATTENDEE',
    "checkin_mode" "platform_meeting_attendance"."AttendanceMode",
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_series_attendee_preferences" (
    "series_id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "default_checkin_mode" "platform_meeting_attendance"."AttendanceMode" NOT NULL,
    "updated_by_user_id" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "meeting_series_attendee_preferences_pkey" PRIMARY KEY ("series_id","user_id")
);

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_external_attendees" (
    "id" VARCHAR(32) NOT NULL,
    "meeting_id" VARCHAR(32) NOT NULL,
    "email" VARCHAR(255) NOT NULL,
    "display_name" VARCHAR(255),
    "attendee_type" VARCHAR(32),
    "response" VARCHAR(32),
    "source_role" VARCHAR(32) NOT NULL DEFAULT 'ATTENDEE',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_templates" (
    "id" VARCHAR(32) NOT NULL,
    "name" VARCHAR(200) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "duration" INTEGER NOT NULL,
    "timezone" VARCHAR(50) NOT NULL DEFAULT 'UTC',
    "location" VARCHAR(200),
    "type" "platform_meeting_attendance"."MeetingType" NOT NULL DEFAULT 'OFFLINE',
    "creator_id" UUID NOT NULL,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."meeting_template_attendees" (
    "id" VARCHAR(32) NOT NULL,
    "template_id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."leave_records" (
    "id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "type" "platform_meeting_attendance"."LeaveType" NOT NULL,
    "start_date" TIMESTAMPTZ(3) NOT NULL,
    "end_date" TIMESTAMPTZ(3) NOT NULL,
    "reason" TEXT,
    "status" "platform_meeting_attendance"."LeaveStatus" NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."audit_logs" (
    "id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "user_email" VARCHAR(255) NOT NULL,
    "user_name" VARCHAR(200) NOT NULL,
    "user_role" VARCHAR(50) NOT NULL,
    "action" VARCHAR(100) NOT NULL,
    "resource" VARCHAR(100) NOT NULL,
    "resource_id" VARCHAR(64),
    "method" VARCHAR(10) NOT NULL,
    "endpoint" VARCHAR(255) NOT NULL,
    "status_code" INTEGER NOT NULL,
    "source" VARCHAR(50) NOT NULL DEFAULT 'WEB',
    "device_id" VARCHAR(100),
    "session_id" VARCHAR(500),
    "request_id" VARCHAR(100),
    "trace_id" VARCHAR(100),
    "geo_location" VARCHAR(255),
    "ip_address" VARCHAR(100),
    "user_agent" TEXT,
    "field_diffs" TEXT,
    "request_body" TEXT,
    "changes" TEXT,
    "reason_code" VARCHAR(50),
    "reason_text" TEXT,
    "error_message" TEXT,
    "prev_hash" VARCHAR(100),
    "hash" VARCHAR(100),
    "is_redacted" BOOLEAN NOT NULL DEFAULT false,
    "is_tombstoned" BOOLEAN NOT NULL DEFAULT false,
    "retention_until" TIMESTAMPTZ(3),
    "duration_ms" INTEGER,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."data_access_logs" (
    "id" VARCHAR(32) NOT NULL,
    "actor_id" UUID,
    "actor_email" VARCHAR(255) NOT NULL,
    "actor_name" VARCHAR(200) NOT NULL,
    "actor_role" VARCHAR(50) NOT NULL,
    "access_type" VARCHAR(100) NOT NULL,
    "entity_type" VARCHAR(100) NOT NULL,
    "entity_ids" TEXT,
    "result_count" INTEGER NOT NULL,
    "has_sensitive_data" BOOLEAN NOT NULL DEFAULT false,
    "sensitive_fields" TEXT,
    "endpoint" VARCHAR(255) NOT NULL,
    "method" VARCHAR(10) NOT NULL,
    "status_code" INTEGER NOT NULL,
    "ip_address" VARCHAR(100),
    "session_id" VARCHAR(100),
    "request_id" VARCHAR(100),
    "device_id" VARCHAR(100),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."audit_alert_rules" (
    "id" VARCHAR(32) NOT NULL,
    "name" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "rule_type" VARCHAR(100) NOT NULL,
    "conditions" TEXT NOT NULL,
    "threshold" INTEGER,
    "time_window" INTEGER,
    "severity" VARCHAR(50) NOT NULL,
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,
    "notify_email" BOOLEAN NOT NULL DEFAULT true,
    "notify_in_app" BOOLEAN NOT NULL DEFAULT true,
    "notify_webhook" BOOLEAN NOT NULL DEFAULT false,
    "webhook_url" VARCHAR(500),
    "suppression_window" INTEGER,
    "last_triggered" TIMESTAMPTZ(3),
    "trigger_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."audit_alerts" (
    "id" VARCHAR(32) NOT NULL,
    "rule_id" VARCHAR(32) NOT NULL,
    "severity" VARCHAR(50) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT NOT NULL,
    "trigger_data" TEXT NOT NULL,
    "matched_logs" TEXT NOT NULL,
    "affected_entities" TEXT,
    "status" VARCHAR(50) NOT NULL DEFAULT 'OPEN',
    "acknowledged_by" VARCHAR(32),
    "acknowledged_at" TIMESTAMPTZ(3),
    "resolved_by" VARCHAR(32),
    "resolved_at" TIMESTAMPTZ(3),
    "resolution" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."retention_policies" (
    "id" VARCHAR(32) NOT NULL,
    "entity_type" VARCHAR(100) NOT NULL,
    "hot_days" INTEGER NOT NULL DEFAULT 365,
    "warm_days" INTEGER NOT NULL DEFAULT 730,
    "cold_days" INTEGER NOT NULL DEFAULT 2555,
    "auto_archive" BOOLEAN NOT NULL DEFAULT true,
    "auto_delete" BOOLEAN NOT NULL DEFAULT false,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_mailboxes" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_email" VARCHAR(255) NOT NULL,
    "mailbox_type" "platform_meeting_attendance"."OutlookMailboxType" NOT NULL,
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,
    "is_primary_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_subscriptions" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "graph_subscription_id" VARCHAR(128) NOT NULL,
    "resource" VARCHAR(255) NOT NULL,
    "status" "platform_meeting_attendance"."OutlookSubscriptionStatus" NOT NULL DEFAULT 'ACTIVE',
    "expiration_at" TIMESTAMPTZ(3) NOT NULL,
    "last_error" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_bindings" (
    "id" VARCHAR(32) NOT NULL,
    "meeting_id" VARCHAR(32),
    "meeting_series_id" VARCHAR(32),
    "graph_event_id" VARCHAR(255) NOT NULL,
    "ical_uid" VARCHAR(255) NOT NULL,
    "graph_series_master_id" VARCHAR(255),
    "graph_event_type" VARCHAR(32) NOT NULL,
    "manage_status" "platform_meeting_attendance"."OutlookManageStatus" NOT NULL DEFAULT 'PENDING_SELECTION',
    "bootstrap_status" "platform_meeting_attendance"."OutlookBootstrapStatus",
    "bootstrap_error" VARCHAR(500),
    "bootstrap_updated_at" TIMESTAMPTZ(3),
    "owner_user_id" VARCHAR(64),
    "owner_email" VARCHAR(255),
    "primary_mailbox_id" VARCHAR(32) NOT NULL,
    "sync_from" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "cancellation_source" "platform_meeting_attendance"."OutlookCancellationSource",
    "sync_mode" "platform_meeting_attendance"."OutlookBindingSyncMode" NOT NULL DEFAULT 'AUTO',
    "local_override_at" TIMESTAMPTZ(3),
    "local_override_by_user_id" UUID,
    "local_override_by_email" VARCHAR(255),
    "local_override_reason" VARCHAR(64),
    "local_override_fields" JSONB,
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_series_occurrence_exclusions" (
    "id" VARCHAR(32) NOT NULL,
    "binding_id" VARCHAR(32) NOT NULL,
    "occurrence_graph_event_id" VARCHAR(255) NOT NULL,
    "ical_uid" VARCHAR(255),
    "reason" VARCHAR(255),
    "created_by_email" VARCHAR(255),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_event_logs" (
    "id" VARCHAR(32) NOT NULL,
    "binding_id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "event_type" VARCHAR(64) NOT NULL,
    "result_status" VARCHAR(32) NOT NULL DEFAULT 'SUCCESS',
    "message" TEXT,
    "payload" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_event_source_versions" (
    "id" VARCHAR(32) NOT NULL,
    "binding_id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "graph_event_id" VARCHAR(255) NOT NULL,
    "graph_series_master_id" VARCHAR(255),
    "graph_event_type" VARCHAR(32) NOT NULL,
    "version_source" VARCHAR(32) NOT NULL,
    "graph_last_modified_at" TIMESTAMPTZ(3),
    "fetched_at" TIMESTAMPTZ(3) NOT NULL,
    "etag" VARCHAR(255),
    "payload_hash" VARCHAR(128) NOT NULL,
    "attendees_count" INTEGER NOT NULL,
    "attendees_required_count" INTEGER NOT NULL,
    "attendees_optional_count" INTEGER NOT NULL,
    "attendees_resource_count" INTEGER NOT NULL,
    "organizer_email" VARCHAR(255),
    "start_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),
    "is_cancelled" BOOLEAN NOT NULL DEFAULT false,
    "raw_payload" JSONB NOT NULL,
    "normalized_payload" JSONB NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_event_sync_diffs" (
    "id" VARCHAR(32) NOT NULL,
    "binding_id" VARCHAR(32) NOT NULL,
    "source_version_id" VARCHAR(32) NOT NULL,
    "previous_source_version_id" VARCHAR(32),
    "detected_at" TIMESTAMPTZ(3) NOT NULL,
    "diff_type" VARCHAR(32) NOT NULL,
    "changed_fields" JSONB NOT NULL,
    "summary_json" JSONB NOT NULL,
    "attendees_added" JSONB,
    "attendees_removed" JSONB,
    "attendees_response_changed" JSONB,
    "graph_attendees_count_before" INTEGER,
    "graph_attendees_count_after" INTEGER,
    "internal_matched_count_before" INTEGER,
    "internal_matched_count_after" INTEGER,
    "meeting_required_count_before" INTEGER,
    "meeting_required_count_after" INTEGER,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_cursors" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "delta_token" TEXT NOT NULL,
    "last_synced_at" TIMESTAMPTZ(3),
    "last_reconciled_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_event_snapshots" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "graph_event_id" VARCHAR(255) NOT NULL,
    "ical_uid" VARCHAR(255) NOT NULL,
    "title" VARCHAR(500) NOT NULL,
    "start_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),
    "timezone" VARCHAR(64) NOT NULL,
    "event_type" VARCHAR(32) NOT NULL,
    "is_cancelled" BOOLEAN NOT NULL DEFAULT false,
    "series_master_id" VARCHAR(255),
    "organizer_email" VARCHAR(255),
    "last_modified_at" TIMESTAMPTZ(3),
    "raw_payload" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_settings" (
    "id" VARCHAR(32) NOT NULL DEFAULT 'default',
    "reconcile_cron" VARCHAR(100) NOT NULL DEFAULT '*/10 * * * *',
    "delta_batch_size" INTEGER NOT NULL DEFAULT 100,
    "lookahead_days" INTEGER NOT NULL DEFAULT 180,
    "lookback_days" INTEGER NOT NULL DEFAULT 7,
    "renew_before_minutes" INTEGER NOT NULL DEFAULT 1440,
    "include_organizer_as_attendee" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_notify"."notification_messages" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "type" "platform_notify"."NotificationType" NOT NULL,
    "title" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "link" TEXT,
    "status" "platform_notify"."NotificationStatus" NOT NULL DEFAULT 'UNREAD',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "read_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_notify"."notification_preferences" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "email" BOOLEAN NOT NULL DEFAULT true,
    "webpush" BOOLEAN NOT NULL DEFAULT true,
    "websocket" BOOLEAN NOT NULL DEFAULT true,
    "quiet_hours" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_notify"."notification_templates" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "channel" "platform_notify"."NotificationChannel" NOT NULL,
    "subject" TEXT,
    "template" TEXT NOT NULL,
    "variables" TEXT[],
    "example" JSONB,
    "priority" "platform_notify"."NotificationPriority" NOT NULL DEFAULT 'NORMAL',
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_notify"."notification_log" (
    "id" UUID NOT NULL,
    "recipient_id" UUID NOT NULL,
    "recipient_email" TEXT,
    "channel" "platform_notify"."NotificationChannel" NOT NULL,
    "template_id" UUID,
    "subject" TEXT,
    "content" TEXT NOT NULL,
    "variables" JSONB,
    "status" "platform_notify"."NotificationSendStatus" NOT NULL DEFAULT 'PENDING',
    "error_message" TEXT,
    "sent_at" TIMESTAMPTZ(3),
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 3,
    "next_retry_at" TIMESTAMPTZ(3),
    "priority" "platform_notify"."NotificationPriority" NOT NULL DEFAULT 'NORMAL',
    "metadata" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ops_center"."m365_users" (
    "id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "created_by_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "graph_user_id" VARCHAR(64) NOT NULL,
    "user_principal_name" CITEXT NOT NULL,
    "display_name" TEXT,
    "mail" TEXT,
    "department" TEXT,
    "job_title" TEXT,
    "account_enabled" BOOLEAN NOT NULL,
    "has_license" BOOLEAN NOT NULL,
    "account_created_at" TIMESTAMPTZ(3),
    "licenses" JSONB NOT NULL DEFAULT '[]',
    "last_sign_in_at" TIMESTAMPTZ(3),
    "last_non_interactive_sign_in_at" TIMESTAMPTZ(3),
    "last_email_activity_at" TIMESTAMPTZ(3),
    "last_one_drive_activity_at" TIMESTAMPTZ(3),
    "last_teams_activity_at" TIMESTAMPTZ(3),
    "last_share_point_activity_at" TIMESTAMPTZ(3),
    "last_any_activity_at" TIMESTAMPTZ(3),
    "days_inactive" INTEGER,
    "first_seen_in_execution_id" UUID NOT NULL,
    "last_seen_in_execution_id" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_ops_center"."m365_user_activity_changes" (
    "id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "user_id" UUID NOT NULL,
    "execution_id" UUID NOT NULL,
    "field" "platform_ops_center"."M365UserActivityChangeField" NOT NULL,
    "previous_value" TEXT,
    "current_value" TEXT,

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

-- CreateTable
CREATE TABLE "platform_performance"."grade_config" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "grades" JSONB NOT NULL,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "organization_id" UUID,

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

-- CreateTable
CREATE TABLE "platform_performance"."performance_cycle" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "type" "platform_performance"."CycleType" NOT NULL,
    "status" "platform_performance"."CycleStatus" NOT NULL DEFAULT 'DRAFT',
    "start_date" DATE NOT NULL,
    "end_date" DATE NOT NULL,
    "grade_config_id" UUID,
    "parent_cycle_id" UUID,
    "results_published_at" TIMESTAMPTZ(3),
    "results_published_by" UUID,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "organization_id" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_performance"."performance_result" (
    "id" UUID NOT NULL,
    "cycle_id" UUID NOT NULL,
    "employee_id" UUID NOT NULL,
    "kpi_score" DECIMAL(5,2),
    "kpi_weight" DECIMAL(5,2),
    "e360_score" DECIMAL(5,2),
    "e360_weight" DECIMAL(5,2),
    "total_score" DECIMAL(5,2),
    "proposed_grade_code" TEXT,
    "proposed_grade_name" TEXT,
    "grade_code" TEXT,
    "grade_name" TEXT,
    "is_published" BOOLEAN NOT NULL DEFAULT false,
    "published_at" TIMESTAMPTZ(3),
    "viewed_by_employee" BOOLEAN NOT NULL DEFAULT false,
    "viewed_at" TIMESTAMPTZ(3),
    "confirm_status" "platform_performance"."ResultConfirmStatus" NOT NULL DEFAULT 'PENDING',
    "appeal_reason" TEXT,
    "appeal_response" TEXT,
    "appeal_resolved_at" TIMESTAMPTZ(3),
    "self_overall_comment" TEXT,
    "manager_overall_comment" TEXT,
    "remarks" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "organization_id" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_performance"."kpi_assignment" (
    "id" UUID NOT NULL,
    "cycle_id" UUID NOT NULL,
    "employee_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "weight" DECIMAL(5,2) NOT NULL,
    "unit" TEXT,
    "base_target" TEXT,
    "stretch_target" TEXT,
    "target_value" DECIMAL(10,2),
    "parent_id" UUID,
    "seq" INTEGER,
    "assignment_status" "platform_performance"."KpiAssignmentStatus" NOT NULL DEFAULT 'DRAFT',
    "maturity_score" DECIMAL(5,2),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."kpi_dependency" (
    "id" UUID NOT NULL,
    "source_assignment_id" UUID NOT NULL,
    "target_assignment_id" UUID,
    "target_user_id" UUID NOT NULL,
    "description" TEXT,
    "status" "platform_performance"."KpiDependencyStatus" NOT NULL DEFAULT 'PENDING',
    "rejection_reason" TEXT,
    "confirmed_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."kpi_assessment" (
    "id" UUID NOT NULL,
    "assignment_id" UUID NOT NULL,
    "status" "platform_performance"."KpiAssessmentStatus" NOT NULL DEFAULT 'PENDING',
    "self_score" DECIMAL(5,2),
    "completion_note" TEXT,
    "self_comment" TEXT,
    "self_evaluated_at" TIMESTAMPTZ(3),
    "manager_score" DECIMAL(5,2),
    "manager_comment" TEXT,
    "manager_id" UUID,
    "manager_evaluated_at" TIMESTAMPTZ(3),
    "final_score" DECIMAL(5,2),
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."evaluation_360" (
    "id" UUID NOT NULL,
    "cycle_id" UUID NOT NULL,
    "target_id" UUID NOT NULL,
    "status" "platform_performance"."Evaluation360Status" NOT NULL DEFAULT 'DRAFT',
    "template_id" UUID,
    "deadline" DATE NOT NULL,
    "min_evaluators" INTEGER,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."evaluation_task" (
    "id" UUID NOT NULL,
    "evaluation_id" UUID NOT NULL,
    "evaluator_id" UUID NOT NULL,
    "relation_type" "platform_performance"."RelationType" NOT NULL,
    "status" "platform_performance"."EvaluationTaskStatus" NOT NULL DEFAULT 'PENDING',
    "is_anonymous" BOOLEAN NOT NULL DEFAULT true,
    "submitted_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."evaluation_response" (
    "id" UUID NOT NULL,
    "task_id" UUID NOT NULL,
    "dimension" TEXT NOT NULL,
    "score" DECIMAL(5,2) NOT NULL,
    "comment" TEXT,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."grade_adjustment_log" (
    "id" UUID NOT NULL,
    "result_id" UUID NOT NULL,
    "previous_grade_code" TEXT NOT NULL,
    "new_grade_code" TEXT NOT NULL,
    "reason" TEXT NOT NULL,
    "adjusted_by" UUID NOT NULL,
    "adjusted_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."strategic_objective" (
    "id" UUID NOT NULL,
    "cycle_id" UUID NOT NULL,
    "seq" INTEGER NOT NULL DEFAULT 0,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "department_id" UUID,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."strategic_objective_assignment" (
    "id" UUID NOT NULL,
    "objective_id" UUID NOT NULL,
    "assignee_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "platform_performance"."evaluation_360_template" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "dimensions" JSONB NOT NULL,
    "relationship_types" JSONB NOT NULL,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "cycle_id" UUID,
    "scope" JSONB,
    "organization_id" UUID,

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

-- CreateTable
CREATE TABLE "platform_site_attendance"."site_checkpoints" (
    "id" VARCHAR(32) NOT NULL,
    "code" VARCHAR(32) NOT NULL,
    "name" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "address" VARCHAR(500),
    "timezone" VARCHAR(50) NOT NULL,
    "latitude" DOUBLE PRECISION NOT NULL,
    "longitude" DOUBLE PRECISION NOT NULL,
    "geo_policy" "platform_site_attendance"."SiteGeoPolicy" NOT NULL DEFAULT 'SKIP',
    "geo_radius" INTEGER NOT NULL DEFAULT 200,
    "geo_accuracy_threshold" INTEGER NOT NULL DEFAULT 100,
    "allow_unauthenticated_checkin" BOOLEAN NOT NULL DEFAULT false,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "access_mode" "platform_site_attendance"."SiteCheckpointAccessMode" NOT NULL DEFAULT 'PUBLIC',
    "qr_rotation_seconds" INTEGER,
    "qr_grace_seconds" INTEGER NOT NULL DEFAULT 120,
    "shared_checkin_enabled" BOOLEAN NOT NULL DEFAULT false,
    "shared_company_id" VARCHAR(64),
    "shared_company_label" VARCHAR(200),
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_site_attendance"."shared_checkin_partners" (
    "id" VARCHAR(32) NOT NULL,
    "checkpoint_id" VARCHAR(32) NOT NULL,
    "company_id" VARCHAR(64) NOT NULL,
    "company_label" VARCHAR(200) NOT NULL,
    "display_label" VARCHAR(200),
    "target_url" VARCHAR(1000) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "created_by" UUID NOT NULL,
    "updated_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_site_attendance"."shared_checkin_ticket_usage" (
    "nonce" VARCHAR(64) NOT NULL,
    "used_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "shared_checkin_ticket_usage_pkey" PRIMARY KEY ("nonce")
);

-- CreateTable
CREATE TABLE "platform_site_attendance"."site_attendance_events" (
    "id" VARCHAR(32) NOT NULL,
    "checkpoint_id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "event_type" "platform_site_attendance"."SiteEventType" NOT NULL,
    "timestamp" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "local_date" VARCHAR(10) NOT NULL,
    "auth_method" "platform_site_attendance"."SiteAuthMethod" NOT NULL,
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "accuracy" DOUBLE PRECISION,
    "geo_status" "platform_site_attendance"."SiteGeoStatus" NOT NULL,
    "distance_to_checkpoint" DOUBLE PRECISION,
    "device_id" VARCHAR(200),
    "user_agent" TEXT,
    "ip_address" VARCHAR(45),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_site_attendance"."site_daily_summaries" (
    "id" VARCHAR(32) NOT NULL,
    "checkpoint_id" VARCHAR(32) NOT NULL,
    "user_id" UUID NOT NULL,
    "local_date" VARCHAR(10) NOT NULL,
    "first_check_in_at" TIMESTAMPTZ(3),
    "last_check_out_at" TIMESTAMPTZ(3),
    "check_in_count" INTEGER NOT NULL DEFAULT 0,
    "check_out_count" INTEGER NOT NULL DEFAULT 0,
    "has_geo_anomaly" BOOLEAN NOT NULL DEFAULT false,
    "last_event_id" VARCHAR(32),
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_tickets"."tickets" (
    "id" UUID NOT NULL,
    "ticket_no" VARCHAR(30) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT NOT NULL,
    "category_id" UUID NOT NULL,
    "priority" "platform_tickets"."TicketPriority" NOT NULL DEFAULT 'MEDIUM',
    "tags" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "status" "platform_tickets"."TicketStatus" NOT NULL DEFAULT 'OPEN',
    "creator_id" UUID NOT NULL,
    "assignee_id" UUID,
    "assignee_group_id" UUID,
    "watcher_ids" UUID[] DEFAULT ARRAY[]::UUID[],
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "first_response_at" TIMESTAMP(3),
    "resolved_at" TIMESTAMP(3),
    "closed_at" TIMESTAMP(3),
    "due_at" TIMESTAMP(3),
    "sla_id" UUID,
    "sla_breached" BOOLEAN NOT NULL DEFAULT false,
    "sla_paused_at" TIMESTAMP(3),
    "sla_paused_duration" INTEGER NOT NULL DEFAULT 0,
    "satisfaction_rating" INTEGER,
    "satisfaction_comment" TEXT,
    "satisfaction_rated_at" TIMESTAMP(3),
    "source" "platform_tickets"."TicketSource" NOT NULL DEFAULT 'WEB',
    "external_id" VARCHAR(100),
    "channel_metadata" JSONB,
    "resolution" TEXT,
    "root_cause" TEXT,
    "related_ticket_ids" UUID[] DEFAULT ARRAY[]::UUID[],
    "parent_ticket_id" UUID,
    "approval_instance_id" UUID,
    "region" VARCHAR(10) NOT NULL DEFAULT 'CN',
    "tenant_id" UUID,
    "language" VARCHAR(10),
    "compliance_flag" BOOLEAN NOT NULL DEFAULT false,
    "deleted_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "platform_tickets"."ticket_categories" (
    "id" UUID NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "description" TEXT,
    "icon" VARCHAR(50),
    "parent_id" UUID,
    "default_priority" "platform_tickets"."TicketPriority" NOT NULL DEFAULT 'MEDIUM',
    "default_assignee_group_id" UUID,
    "sla_id" UUID,
    "form_template_id" UUID,
    "allowed_department_ids" UUID[] DEFAULT ARRAY[]::UUID[],
    "allowed_role_ids" UUID[] DEFAULT ARRAY[]::UUID[],
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_tickets"."ticket_comments" (
    "id" UUID NOT NULL,
    "ticket_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "type" "platform_tickets"."CommentType" NOT NULL DEFAULT 'COMMENT',
    "is_internal" BOOLEAN NOT NULL DEFAULT false,
    "visibility_scope" "platform_tickets"."VisibilityScope" NOT NULL DEFAULT 'ALL',
    "author_id" UUID NOT NULL,
    "mentioned_user_ids" UUID[] DEFAULT ARRAY[]::UUID[],
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "deleted_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "platform_tickets"."ticket_attachments" (
    "id" UUID NOT NULL,
    "ticket_id" UUID NOT NULL,
    "comment_id" UUID,
    "file_name" VARCHAR(255) NOT NULL,
    "file_url" VARCHAR(500) NOT NULL,
    "file_size" INTEGER NOT NULL,
    "mime_type" VARCHAR(100) NOT NULL,
    "uploaded_by" UUID NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_tickets"."ticket_activities" (
    "id" UUID NOT NULL,
    "ticket_id" UUID NOT NULL,
    "type" "platform_tickets"."ActivityType" NOT NULL,
    "content" JSONB NOT NULL,
    "operator_id" UUID,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "platform_tickets"."ticket_slas" (
    "id" UUID NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "first_response_time" JSONB NOT NULL,
    "resolution_time" JSONB NOT NULL,
    "business_hours" JSONB NOT NULL,
    "exclude_holidays" BOOLEAN NOT NULL DEFAULT true,
    "holiday_calendar_id" UUID,
    "escalation_rules" JSONB,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_tickets"."assignment_groups" (
    "id" UUID NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "description" TEXT,
    "member_ids" UUID[],
    "manager_id" UUID,
    "skills" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "assignment_strategy" "platform_tickets"."AssignmentStrategy" NOT NULL DEFAULT 'ROUND_ROBIN',
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_field_defs" (
    "id" UUID NOT NULL,
    "scope" TEXT NOT NULL DEFAULT 'unit',
    "key" TEXT NOT NULL,
    "label_en" TEXT NOT NULL,
    "label" TEXT,
    "description" TEXT,
    "type" TEXT NOT NULL,
    "group" TEXT,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "required" BOOLEAN NOT NULL DEFAULT false,
    "default_value" TEXT,
    "options" JSONB NOT NULL DEFAULT '[]',
    "validation" JSONB NOT NULL DEFAULT '{}',
    "show_in_list" BOOLEAN NOT NULL DEFAULT false,
    "indexed" BOOLEAN NOT NULL DEFAULT false,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_system_config" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "value" JSONB NOT NULL,
    "description" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_models" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "brand" TEXT,
    "description" TEXT,
    "image_url" TEXT,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_skus" (
    "id" UUID NOT NULL,
    "model_id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "variant" TEXT,
    "default_price" DECIMAL(12,2),
    "default_cost" DECIMAL(12,2),
    "description" TEXT,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_suppliers" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "contact_name" TEXT,
    "contact_phone" TEXT,
    "contact_email" TEXT,
    "address" TEXT,
    "payment_terms" TEXT,
    "lead_time_days" INTEGER,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_customers" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "industry" TEXT,
    "contact_name" TEXT,
    "contact_phone" TEXT,
    "contact_email" TEXT,
    "address" TEXT,
    "credit_limit" DECIMAL(14,2),
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_locations" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type_code" TEXT NOT NULL,
    "address" TEXT,
    "contact" TEXT,
    "customer_id" UUID,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_units" (
    "id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "version" INTEGER NOT NULL DEFAULT 0,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID NOT NULL,
    "updated_by" UUID NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),
    "ffsn" TEXT NOT NULL,
    "model_id" UUID NOT NULL,
    "sku_id" UUID NOT NULL,
    "current_status" "robot_manager"."RobotStatus" NOT NULL DEFAULT 'ORDERED',
    "supplier_id" UUID,
    "customer_id" UUID,
    "location_id" UUID,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_service_records" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "service_type_code" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "resolution" TEXT,
    "service_date" DATE NOT NULL,
    "completed_date" DATE,
    "customer_feedback" TEXT,
    "created_by" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_status_change_logs" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "from_status" "robot_manager"."RobotStatus",
    "to_status" "robot_manager"."RobotStatus" NOT NULL,
    "changed_by" UUID NOT NULL,
    "changed_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "remark" TEXT,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_attachments" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "filename" TEXT NOT NULL,
    "mime_type" TEXT NOT NULL,
    "size" INTEGER NOT NULL,
    "storage_path" TEXT NOT NULL,
    "uploaded_by" UUID NOT NULL,
    "uploaded_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "approval_definitions_key_key" ON "corp_approval"."approval_definitions"("key");

-- CreateIndex
CREATE INDEX "approval_definitions_key_idx" ON "corp_approval"."approval_definitions"("key");

-- CreateIndex
CREATE INDEX "approval_definitions_category_idx" ON "corp_approval"."approval_definitions"("category");

-- CreateIndex
CREATE INDEX "approval_definitions_status_idx" ON "corp_approval"."approval_definitions"("status");

-- CreateIndex
CREATE INDEX "approval_definitions_name_idx" ON "corp_approval"."approval_definitions"("name");

-- CreateIndex
CREATE INDEX "approval_definitions_organization_id_idx" ON "corp_approval"."approval_definitions"("organization_id");

-- CreateIndex
CREATE INDEX "approval_versions_definition_id_idx" ON "corp_approval"."approval_versions"("definition_id");

-- CreateIndex
CREATE INDEX "approval_versions_status_idx" ON "corp_approval"."approval_versions"("status");

-- CreateIndex
CREATE INDEX "approval_versions_is_default_idx" ON "corp_approval"."approval_versions"("is_default");

-- CreateIndex
CREATE UNIQUE INDEX "approval_versions_definition_id_version_key" ON "corp_approval"."approval_versions"("definition_id", "version");

-- CreateIndex
CREATE UNIQUE INDEX "approval_instances_workflow_id_key" ON "corp_approval"."approval_instances"("workflow_id");

-- CreateIndex
CREATE INDEX "approval_instances_business_key_idx" ON "corp_approval"."approval_instances"("business_key");

-- CreateIndex
CREATE INDEX "approval_instances_business_type_business_id_idx" ON "corp_approval"."approval_instances"("business_type", "business_id");

-- CreateIndex
CREATE INDEX "approval_instances_initiator_id_idx" ON "corp_approval"."approval_instances"("initiator_id");

-- CreateIndex
CREATE INDEX "approval_instances_region_id_idx" ON "corp_approval"."approval_instances"("region_id");

-- CreateIndex
CREATE INDEX "approval_instances_status_idx" ON "corp_approval"."approval_instances"("status");

-- CreateIndex
CREATE INDEX "approval_instances_workflow_id_idx" ON "corp_approval"."approval_instances"("workflow_id");

-- CreateIndex
CREATE INDEX "approval_instances_version_id_idx" ON "corp_approval"."approval_instances"("version_id");

-- CreateIndex
CREATE INDEX "approval_instances_start_time_idx" ON "corp_approval"."approval_instances"("start_time" DESC);

-- CreateIndex
CREATE INDEX "approval_instances_initiator_id_start_time_idx" ON "corp_approval"."approval_instances"("initiator_id", "start_time" DESC);

-- CreateIndex
CREATE INDEX "approval_instances_region_id_status_idx" ON "corp_approval"."approval_instances"("region_id", "status");

-- CreateIndex
CREATE INDEX "approval_instances_region_id_start_time_idx" ON "corp_approval"."approval_instances"("region_id", "start_time" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "approval_instances_business_type_business_id_key" ON "corp_approval"."approval_instances"("business_type", "business_id");

-- CreateIndex
CREATE INDEX "approval_node_instances_instance_id_idx" ON "corp_approval"."approval_node_instances"("instance_id");

-- CreateIndex
CREATE INDEX "approval_node_instances_node_id_idx" ON "corp_approval"."approval_node_instances"("node_id");

-- CreateIndex
CREATE INDEX "approval_node_instances_status_idx" ON "corp_approval"."approval_node_instances"("status");

-- CreateIndex
CREATE INDEX "approval_node_instances_instance_id_status_idx" ON "corp_approval"."approval_node_instances"("instance_id", "status");

-- CreateIndex
CREATE INDEX "approval_tasks_instance_id_idx" ON "corp_approval"."approval_tasks"("instance_id");

-- CreateIndex
CREATE INDEX "approval_tasks_node_instance_id_idx" ON "corp_approval"."approval_tasks"("node_instance_id");

-- CreateIndex
CREATE INDEX "approval_tasks_assignee_status_idx" ON "corp_approval"."approval_tasks"("assignee", "status");

-- CreateIndex
CREATE INDEX "approval_tasks_status_create_time_idx" ON "corp_approval"."approval_tasks"("status", "create_time" DESC);

-- CreateIndex
CREATE INDEX "approval_tasks_due_date_idx" ON "corp_approval"."approval_tasks"("due_date");

-- CreateIndex
CREATE INDEX "approval_tasks_create_time_idx" ON "corp_approval"."approval_tasks"("create_time" DESC);

-- CreateIndex
CREATE INDEX "approval_task_logs_task_id_idx" ON "corp_approval"."approval_task_logs"("task_id");

-- CreateIndex
CREATE INDEX "approval_task_logs_instance_id_idx" ON "corp_approval"."approval_task_logs"("instance_id");

-- CreateIndex
CREATE INDEX "approval_task_logs_operator_id_idx" ON "corp_approval"."approval_task_logs"("operator_id");

-- CreateIndex
CREATE INDEX "approval_task_logs_target_user_id_idx" ON "corp_approval"."approval_task_logs"("target_user_id");

-- CreateIndex
CREATE INDEX "approval_task_logs_action_idx" ON "corp_approval"."approval_task_logs"("action");

-- CreateIndex
CREATE INDEX "approval_task_logs_action_time_idx" ON "corp_approval"."approval_task_logs"("action_time" DESC);

-- CreateIndex
CREATE INDEX "approval_task_logs_operator_id_action_time_idx" ON "corp_approval"."approval_task_logs"("operator_id", "action_time" DESC);

-- CreateIndex
CREATE INDEX "approval_task_logs_risk_level_idx" ON "corp_approval"."approval_task_logs"("risk_level");

-- CreateIndex
CREATE INDEX "callback_retry_queue_status_next_retry_at_idx" ON "corp_approval"."callback_retry_queue"("status", "next_retry_at");

-- CreateIndex
CREATE INDEX "callback_retry_queue_instance_id_idx" ON "corp_approval"."callback_retry_queue"("instance_id");

-- CreateIndex
CREATE INDEX "reminder_queue_status_scheduled_at_idx" ON "corp_approval"."reminder_queue"("status", "scheduled_at");

-- CreateIndex
CREATE INDEX "reminder_queue_task_id_idx" ON "corp_approval"."reminder_queue"("task_id");

-- CreateIndex
CREATE INDEX "reminder_queue_instance_id_idx" ON "corp_approval"."reminder_queue"("instance_id");

-- CreateIndex
CREATE INDEX "user_delegation_settings_user_id_enabled_idx" ON "corp_approval"."user_delegation_settings"("user_id", "enabled");

-- CreateIndex
CREATE INDEX "user_delegation_settings_delegate_user_id_idx" ON "corp_approval"."user_delegation_settings"("delegate_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_delegation_settings_user_id_key" ON "corp_approval"."user_delegation_settings"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "approval_admin_configs_key_key" ON "corp_approval"."approval_admin_configs"("key");

-- CreateIndex
CREATE INDEX "approval_admin_exports_status_idx" ON "corp_approval"."approval_admin_exports"("status");

-- CreateIndex
CREATE INDEX "approval_admin_exports_requested_by_created_at_idx" ON "corp_approval"."approval_admin_exports"("requested_by", "created_at" DESC);

-- CreateIndex
CREATE INDEX "approval_admin_exports_expires_at_idx" ON "corp_approval"."approval_admin_exports"("expires_at");

-- CreateIndex
CREATE UNIQUE INDEX "organizations_code_key" ON "corp_hr"."organizations"("code");

-- CreateIndex
CREATE INDEX "organizations_code_idx" ON "corp_hr"."organizations"("code");

-- CreateIndex
CREATE INDEX "organizations_status_idx" ON "corp_hr"."organizations"("status");

-- CreateIndex
CREATE INDEX "organizations_primary_region_id_idx" ON "corp_hr"."organizations"("primary_region_id");

-- CreateIndex
CREATE INDEX "organization_regions_organization_id_idx" ON "corp_hr"."organization_regions"("organization_id");

-- CreateIndex
CREATE INDEX "organization_regions_is_default_idx" ON "corp_hr"."organization_regions"("is_default");

-- CreateIndex
CREATE UNIQUE INDEX "organization_regions_organization_id_region_id_key" ON "corp_hr"."organization_regions"("organization_id", "region_id");

-- CreateIndex
CREATE UNIQUE INDEX "regions_code_key" ON "corp_hr"."regions"("code");

-- CreateIndex
CREATE INDEX "regions_code_idx" ON "corp_hr"."regions"("code");

-- CreateIndex
CREATE INDEX "regions_is_active_idx" ON "corp_hr"."regions"("is_active");

-- CreateIndex
CREATE INDEX "departments_organization_id_idx" ON "corp_hr"."departments"("organization_id");

-- CreateIndex
CREATE INDEX "departments_organization_id_parent_id_idx" ON "corp_hr"."departments"("organization_id", "parent_id");

-- CreateIndex
CREATE INDEX "departments_parent_id_idx" ON "corp_hr"."departments"("parent_id");

-- CreateIndex
CREATE INDEX "departments_head_id_idx" ON "corp_hr"."departments"("head_id");

-- CreateIndex
CREATE UNIQUE INDEX "departments_organization_id_code_key" ON "corp_hr"."departments"("organization_id", "code");

-- CreateIndex
CREATE UNIQUE INDEX "positions_code_key" ON "corp_hr"."positions"("code");

-- CreateIndex
CREATE INDEX "user_departments_user_id_idx" ON "corp_hr"."user_departments"("user_id");

-- CreateIndex
CREATE INDEX "user_departments_department_id_idx" ON "corp_hr"."user_departments"("department_id");

-- CreateIndex
CREATE INDEX "user_departments_organization_id_idx" ON "corp_hr"."user_departments"("organization_id");

-- CreateIndex
CREATE INDEX "user_departments_user_id_organization_id_idx" ON "corp_hr"."user_departments"("user_id", "organization_id");

-- CreateIndex
CREATE INDEX "user_departments_is_primary_idx" ON "corp_hr"."user_departments"("is_primary");

-- CreateIndex
CREATE INDEX "user_departments_manager_id_idx" ON "corp_hr"."user_departments"("manager_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_departments_user_id_department_id_key" ON "corp_hr"."user_departments"("user_id", "department_id");

-- CreateIndex
CREATE UNIQUE INDEX "expense_requests_code_key" ON "corp_request"."expense_requests"("code");

-- CreateIndex
CREATE UNIQUE INDEX "expense_requests_workflow_instance_id_key" ON "corp_request"."expense_requests"("workflow_instance_id");

-- CreateIndex
CREATE INDEX "expense_requests_creator_id_status_idx" ON "corp_request"."expense_requests"("creator_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_requests_code_key" ON "corp_request"."purchase_requests"("code");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_requests_workflow_instance_id_key" ON "corp_request"."purchase_requests"("workflow_instance_id");

-- CreateIndex
CREATE INDEX "purchase_requests_creator_id_status_idx" ON "corp_request"."purchase_requests"("creator_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "contract_requests_code_key" ON "corp_request"."contract_requests"("code");

-- CreateIndex
CREATE UNIQUE INDEX "contract_requests_workflow_instance_id_key" ON "corp_request"."contract_requests"("workflow_instance_id");

-- CreateIndex
CREATE INDEX "contract_requests_creator_id_status_idx" ON "corp_request"."contract_requests"("creator_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "parts_part_number_key" ON "mfg_inventory"."parts"("part_number");

-- CreateIndex
CREATE INDEX "parts_part_number_idx" ON "mfg_inventory"."parts"("part_number");

-- CreateIndex
CREATE INDEX "parts_part_name_en_idx" ON "mfg_inventory"."parts"("part_name_en");

-- CreateIndex
CREATE INDEX "parts_part_name_cn_idx" ON "mfg_inventory"."parts"("part_name_cn");

-- CreateIndex
CREATE INDEX "parts_station_idx" ON "mfg_inventory"."parts"("station");

-- CreateIndex
CREATE INDEX "parts_station_id_idx" ON "mfg_inventory"."parts"("station_id");

-- CreateIndex
CREATE INDEX "parts_warehouse_location_idx" ON "mfg_inventory"."parts"("warehouse_location");

-- CreateIndex
CREATE INDEX "parts_warehouse_id_idx" ON "mfg_inventory"."parts"("warehouse_id");

-- CreateIndex
CREATE INDEX "parts_storage_location_id_idx" ON "mfg_inventory"."parts"("storage_location_id");

-- CreateIndex
CREATE INDEX "parts_status_idx" ON "mfg_inventory"."parts"("status");

-- CreateIndex
CREATE INDEX "parts_source_idx" ON "mfg_inventory"."parts"("source");

-- CreateIndex
CREATE UNIQUE INDEX "stations_code_key" ON "mfg_inventory"."stations"("code");

-- CreateIndex
CREATE INDEX "stations_code_idx" ON "mfg_inventory"."stations"("code");

-- CreateIndex
CREATE INDEX "stations_area_idx" ON "mfg_inventory"."stations"("area");

-- CreateIndex
CREATE INDEX "stations_line_idx" ON "mfg_inventory"."stations"("line");

-- CreateIndex
CREATE INDEX "stations_status_idx" ON "mfg_inventory"."stations"("status");

-- CreateIndex
CREATE UNIQUE INDEX "warehouses_code_key" ON "mfg_inventory"."warehouses"("code");

-- CreateIndex
CREATE INDEX "warehouses_code_idx" ON "mfg_inventory"."warehouses"("code");

-- CreateIndex
CREATE INDEX "warehouses_status_idx" ON "mfg_inventory"."warehouses"("status");

-- CreateIndex
CREATE INDEX "warehouses_is_default_idx" ON "mfg_inventory"."warehouses"("is_default");

-- CreateIndex
CREATE INDEX "storage_locations_warehouse_id_idx" ON "mfg_inventory"."storage_locations"("warehouse_id");

-- CreateIndex
CREATE INDEX "storage_locations_code_idx" ON "mfg_inventory"."storage_locations"("code");

-- CreateIndex
CREATE INDEX "storage_locations_zone_idx" ON "mfg_inventory"."storage_locations"("zone");

-- CreateIndex
CREATE INDEX "storage_locations_status_idx" ON "mfg_inventory"."storage_locations"("status");

-- CreateIndex
CREATE UNIQUE INDEX "storage_locations_warehouse_id_code_key" ON "mfg_inventory"."storage_locations"("warehouse_id", "code");

-- CreateIndex
CREATE UNIQUE INDEX "part_labels_label_code_key" ON "mfg_inventory"."part_labels"("label_code");

-- CreateIndex
CREATE INDEX "part_labels_part_id_idx" ON "mfg_inventory"."part_labels"("part_id");

-- CreateIndex
CREATE INDEX "part_labels_label_code_idx" ON "mfg_inventory"."part_labels"("label_code");

-- CreateIndex
CREATE INDEX "inventory_logs_part_id_created_at_idx" ON "mfg_inventory"."inventory_logs"("part_id", "created_at");

-- CreateIndex
CREATE INDEX "inventory_logs_operator_id_created_at_idx" ON "mfg_inventory"."inventory_logs"("operator_id", "created_at");

-- CreateIndex
CREATE INDEX "inventory_logs_operation_type_idx" ON "mfg_inventory"."inventory_logs"("operation_type");

-- CreateIndex
CREATE INDEX "inventory_logs_station_idx" ON "mfg_inventory"."inventory_logs"("station");

-- CreateIndex
CREATE INDEX "inventory_logs_warehouse_location_idx" ON "mfg_inventory"."inventory_logs"("warehouse_location");

-- CreateIndex
CREATE INDEX "stock_alerts_part_id_status_idx" ON "mfg_inventory"."stock_alerts"("part_id", "status");

-- CreateIndex
CREATE INDEX "stock_alerts_alert_type_idx" ON "mfg_inventory"."stock_alerts"("alert_type");

-- CreateIndex
CREATE INDEX "stock_alerts_severity_idx" ON "mfg_inventory"."stock_alerts"("severity");

-- CreateIndex
CREATE INDEX "stock_alerts_created_at_idx" ON "mfg_inventory"."stock_alerts"("created_at");

-- CreateIndex
CREATE INDEX "import_logs_imported_by_imported_at_idx" ON "mfg_inventory"."import_logs"("imported_by", "imported_at");

-- CreateIndex
CREATE INDEX "import_logs_status_idx" ON "mfg_inventory"."import_logs"("status");

-- CreateIndex
CREATE INDEX "part_column_configs_user_id_idx" ON "mfg_inventory"."part_column_configs"("user_id");

-- CreateIndex
CREATE INDEX "part_column_configs_is_default_user_id_idx" ON "mfg_inventory"."part_column_configs"("is_default", "user_id");

-- CreateIndex
CREATE INDEX "part_column_configs_is_template_is_public_idx" ON "mfg_inventory"."part_column_configs"("is_template", "is_public");

-- CreateIndex
CREATE UNIQUE INDEX "part_groups_name_key" ON "mfg_inventory"."part_groups"("name");

-- CreateIndex
CREATE INDEX "part_groups_is_active_display_order_idx" ON "mfg_inventory"."part_groups"("is_active", "display_order");

-- CreateIndex
CREATE INDEX "part_groups_name_idx" ON "mfg_inventory"."part_groups"("name");

-- CreateIndex
CREATE INDEX "group_custom_fields_group_id_display_order_idx" ON "mfg_inventory"."group_custom_fields"("group_id", "display_order");

-- CreateIndex
CREATE INDEX "group_custom_fields_type_idx" ON "mfg_inventory"."group_custom_fields"("type");

-- CreateIndex
CREATE UNIQUE INDEX "group_custom_fields_group_id_key_key" ON "mfg_inventory"."group_custom_fields"("group_id", "key");

-- CreateIndex
CREATE INDEX "part_group_relations_group_id_idx" ON "mfg_inventory"."part_group_relations"("group_id");

-- CreateIndex
CREATE INDEX "part_group_relations_part_id_idx" ON "mfg_inventory"."part_group_relations"("part_id");

-- CreateIndex
CREATE INDEX "ai_conversations_userId_createdAt_idx" ON "platform_ai"."ai_conversations"("userId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "ai_conversations_status_idx" ON "platform_ai"."ai_conversations"("status");

-- CreateIndex
CREATE INDEX "ai_conversations_category_idx" ON "platform_ai"."ai_conversations"("category");

-- CreateIndex
CREATE INDEX "ai_messages_conversationId_createdAt_idx" ON "platform_ai"."ai_messages"("conversationId", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "ai_message_feedbacks_messageId_key" ON "platform_ai"."ai_message_feedbacks"("messageId");

-- CreateIndex
CREATE INDEX "ai_assistant_tickets_status_assigneeId_idx" ON "platform_ai"."ai_assistant_tickets"("status", "assigneeId");

-- CreateIndex
CREATE INDEX "ai_assistant_tickets_category_status_idx" ON "platform_ai"."ai_assistant_tickets"("category", "status");

-- CreateIndex
CREATE INDEX "ai_assistant_tickets_assigneeTeam_status_idx" ON "platform_ai"."ai_assistant_tickets"("assigneeTeam", "status");

-- CreateIndex
CREATE UNIQUE INDEX "ai_knowledge_fixes_messageId_key" ON "platform_ai"."ai_knowledge_fixes"("messageId");

-- CreateIndex
CREATE INDEX "ai_knowledge_fixes_status_idx" ON "platform_ai"."ai_knowledge_fixes"("status");

-- CreateIndex
CREATE INDEX "ai_knowledge_fixes_contributorId_idx" ON "platform_ai"."ai_knowledge_fixes"("contributorId");

-- CreateIndex
CREATE INDEX "ai_prompt_templates_category_isActive_idx" ON "platform_ai"."ai_prompt_templates"("category", "isActive");

-- CreateIndex
CREATE UNIQUE INDEX "ai_prompt_templates_category_version_key" ON "platform_ai"."ai_prompt_templates"("category", "version");

-- CreateIndex
CREATE UNIQUE INDEX "ai_configurations_key_category_key" ON "platform_ai"."ai_configurations"("key", "category");

-- CreateIndex
CREATE INDEX "ai_usage_stats_date_idx" ON "platform_ai"."ai_usage_stats"("date");

-- CreateIndex
CREATE UNIQUE INDEX "ai_usage_stats_date_category_key" ON "platform_ai"."ai_usage_stats"("date", "category");

-- CreateIndex
CREATE UNIQUE INDEX "audit_log_current_hash_key" ON "platform_audit"."audit_log"("current_hash");

-- CreateIndex
CREATE INDEX "audit_log_region_tenant_id_when_idx" ON "platform_audit"."audit_log"("region", "tenant_id", "when");

-- CreateIndex
CREATE INDEX "audit_log_tenant_id_user_id_when_idx" ON "platform_audit"."audit_log"("tenant_id", "user_id", "when");

-- CreateIndex
CREATE INDEX "audit_log_tenant_id_entity_type_entity_id_when_idx" ON "platform_audit"."audit_log"("tenant_id", "entity_type", "entity_id", "when");

-- CreateIndex
CREATE INDEX "audit_log_tenant_id_module_action_when_idx" ON "platform_audit"."audit_log"("tenant_id", "module", "action", "when");

-- CreateIndex
CREATE INDEX "audit_log_tenant_id_is_financial_when_idx" ON "platform_audit"."audit_log"("tenant_id", "is_financial", "when");

-- CreateIndex
CREATE INDEX "audit_log_tenant_id_is_sensitive_when_idx" ON "platform_audit"."audit_log"("tenant_id", "is_sensitive", "when");

-- CreateIndex
CREATE INDEX "audit_log_trace_id_idx" ON "platform_audit"."audit_log"("trace_id");

-- CreateIndex
CREATE INDEX "audit_log_current_hash_idx" ON "platform_audit"."audit_log"("current_hash");

-- CreateIndex
CREATE INDEX "audit_log_session_id_idx" ON "platform_audit"."audit_log"("session_id");

-- CreateIndex
CREATE INDEX "audit_database_change_log_table_name_record_id_idx" ON "platform_audit"."audit_database_change_log"("table_name", "record_id");

-- CreateIndex
CREATE INDEX "audit_database_change_log_user_id_created_at_idx" ON "platform_audit"."audit_database_change_log"("user_id", "created_at");

-- CreateIndex
CREATE INDEX "audit_database_change_log_audit_log_id_idx" ON "platform_audit"."audit_database_change_log"("audit_log_id");

-- CreateIndex
CREATE INDEX "audit_sensitive_operation_log_user_id_created_at_idx" ON "platform_audit"."audit_sensitive_operation_log"("user_id", "created_at");

-- CreateIndex
CREATE INDEX "audit_sensitive_operation_log_risk_level_created_at_idx" ON "platform_audit"."audit_sensitive_operation_log"("risk_level", "created_at");

-- CreateIndex
CREATE INDEX "audit_sensitive_operation_log_audit_log_id_idx" ON "platform_audit"."audit_sensitive_operation_log"("audit_log_id");

-- CreateIndex
CREATE INDEX "audit_integrity_check_log_region_tenant_id_created_at_idx" ON "platform_audit"."audit_integrity_check_log"("region", "tenant_id", "created_at");

-- CreateIndex
CREATE INDEX "audit_integrity_check_log_check_type_created_at_idx" ON "platform_audit"."audit_integrity_check_log"("check_type", "created_at");

-- CreateIndex
CREATE INDEX "audit_integrity_check_log_status_created_at_idx" ON "platform_audit"."audit_integrity_check_log"("status", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "automation_tasks_code_key" ON "platform_automation"."automation_tasks"("code");

-- CreateIndex
CREATE INDEX "automation_tasks_code_idx" ON "platform_automation"."automation_tasks"("code");

-- CreateIndex
CREATE INDEX "automation_tasks_type_idx" ON "platform_automation"."automation_tasks"("type");

-- CreateIndex
CREATE INDEX "automation_tasks_status_idx" ON "platform_automation"."automation_tasks"("status");

-- CreateIndex
CREATE INDEX "automation_executions_task_id_idx" ON "platform_automation"."automation_executions"("task_id");

-- CreateIndex
CREATE INDEX "automation_executions_status_idx" ON "platform_automation"."automation_executions"("status");

-- CreateIndex
CREATE INDEX "automation_executions_started_at_idx" ON "platform_automation"."automation_executions"("started_at");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_sync_configs_task_code_key" ON "platform_automation"."dingtalk_sync_configs"("task_code");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_leave_codes_code_key" ON "platform_automation"."dingtalk_leave_codes"("code");

-- CreateIndex
CREATE INDEX "dingtalk_leave_quota_snapshots_user_id_idx" ON "platform_automation"."dingtalk_leave_quota_snapshots"("user_id");

-- CreateIndex
CREATE INDEX "dingtalk_leave_quota_snapshots_leave_code_idx" ON "platform_automation"."dingtalk_leave_quota_snapshots"("leave_code");

-- CreateIndex
CREATE INDEX "dingtalk_leave_quota_snapshots_snapshot_at_idx" ON "platform_automation"."dingtalk_leave_quota_snapshots"("snapshot_at");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_leave_quota_snapshots_user_period_key" ON "platform_automation"."dingtalk_leave_quota_snapshots"("user_id", "leave_code", "quota_cycle", "start_date", "end_date");

-- CreateIndex
CREATE UNIQUE INDEX "sap_purchase_sync_records_serial_number_key" ON "platform_automation"."sap_purchase_sync_records"("serial_number");

-- CreateIndex
CREATE INDEX "sap_purchase_sync_records_status_idx" ON "platform_automation"."sap_purchase_sync_records"("sap_status");

-- CreateIndex
CREATE INDEX "sap_purchase_sync_records_synced_at_idx" ON "platform_automation"."sap_purchase_sync_records"("synced_at");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_employees_user_id_key" ON "platform_automation"."dingtalk_employees"("user_id");

-- CreateIndex
CREATE INDEX "dingtalk_employees_status_idx" ON "platform_automation"."dingtalk_employees"("status");

-- CreateIndex
CREATE INDEX "dingtalk_employees_name_idx" ON "platform_automation"."dingtalk_employees"("name");

-- CreateIndex
CREATE INDEX "dingtalk_employee_employment_periods_user_id_idx" ON "platform_automation"."dingtalk_employee_employment_periods"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_employee_employment_periods_user_index_key" ON "platform_automation"."dingtalk_employee_employment_periods"("user_id", "period_index");

-- CreateIndex
CREATE INDEX "dingtalk_employee_suspension_periods_user_id_idx" ON "platform_automation"."dingtalk_employee_suspension_periods"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "sap_purchase_cache_serial_number_key" ON "platform_automation"."sap_purchase_cache"("serial_number");

-- CreateIndex
CREATE INDEX "sap_purchase_cache_env_idx" ON "platform_automation"."sap_purchase_cache"("sap_env");

-- CreateIndex
CREATE INDEX "sap_purchase_cache_applicant_idx" ON "platform_automation"."sap_purchase_cache"("applicant_name");

-- CreateIndex
CREATE INDEX "adp_pto_schedules_user_id_leave_date_idx" ON "platform_automation"."adp_pto_schedules"("user_id", "leave_date");

-- CreateIndex
CREATE INDEX "adp_pto_schedules_start_time_end_time_idx" ON "platform_automation"."adp_pto_schedules"("start_time", "end_time");

-- CreateIndex
CREATE UNIQUE INDEX "adp_pto_schedules_user_id_adp_entry_id_key" ON "platform_automation"."adp_pto_schedules"("user_id", "adp_entry_id");

-- CreateIndex
CREATE INDEX "dingtalk_annual_leave_release_plans_year_idx" ON "platform_automation"."dingtalk_annual_leave_release_plans"("year");

-- CreateIndex
CREATE INDEX "dingtalk_annual_leave_release_plans_status_idx" ON "platform_automation"."dingtalk_annual_leave_release_plans"("status");

-- CreateIndex
CREATE INDEX "dingtalk_annual_leave_release_plans_user_id_idx" ON "platform_automation"."dingtalk_annual_leave_release_plans"("user_id");

-- CreateIndex
CREATE INDEX "dingtalk_annual_leave_release_plans_last_calculated_at_idx" ON "platform_automation"."dingtalk_annual_leave_release_plans"("last_calculated_at");

-- CreateIndex
CREATE UNIQUE INDEX "dingtalk_annual_leave_release_plans_user_year_key" ON "platform_automation"."dingtalk_annual_leave_release_plans"("user_id", "year");

-- CreateIndex
CREATE UNIQUE INDEX "dev_items_code_key" ON "platform_devtracker"."dev_items"("code");

-- CreateIndex
CREATE INDEX "dev_items_status_idx" ON "platform_devtracker"."dev_items"("status");

-- CreateIndex
CREATE INDEX "dev_items_item_type_idx" ON "platform_devtracker"."dev_items"("item_type");

-- CreateIndex
CREATE INDEX "dev_items_priority_idx" ON "platform_devtracker"."dev_items"("priority");

-- CreateIndex
CREATE INDEX "dev_items_owner_id_idx" ON "platform_devtracker"."dev_items"("owner_id");

-- CreateIndex
CREATE INDEX "dev_items_reporter_id_idx" ON "platform_devtracker"."dev_items"("reporter_id");

-- CreateIndex
CREATE INDEX "dev_items_reviewer_id_idx" ON "platform_devtracker"."dev_items"("reviewer_id");

-- CreateIndex
CREATE INDEX "dev_items_parent_id_idx" ON "platform_devtracker"."dev_items"("parent_id");

-- CreateIndex
CREATE INDEX "dev_items_module_key_idx" ON "platform_devtracker"."dev_items"("module_key");

-- CreateIndex
CREATE INDEX "dev_items_eta_at_idx" ON "platform_devtracker"."dev_items"("eta_at");

-- CreateIndex
CREATE INDEX "dev_items_created_at_idx" ON "platform_devtracker"."dev_items"("created_at");

-- CreateIndex
CREATE INDEX "release_notes_released_at_idx" ON "platform_devtracker"."release_notes"("released_at");

-- CreateIndex
CREATE INDEX "release_notes_created_at_idx" ON "platform_devtracker"."release_notes"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "release_notes_version_key" ON "platform_devtracker"."release_notes"("version");

-- CreateIndex
CREATE INDEX "feedbacks_user_id_idx" ON "platform_feedback"."feedbacks"("user_id");

-- CreateIndex
CREATE INDEX "feedbacks_assignee_id_idx" ON "platform_feedback"."feedbacks"("assignee_id");

-- CreateIndex
CREATE INDEX "feedbacks_status_idx" ON "platform_feedback"."feedbacks"("status");

-- CreateIndex
CREATE INDEX "feedbacks_type_idx" ON "platform_feedback"."feedbacks"("type");

-- CreateIndex
CREATE INDEX "feedbacks_region_idx" ON "platform_feedback"."feedbacks"("region");

-- CreateIndex
CREATE INDEX "feedbacks_created_at_idx" ON "platform_feedback"."feedbacks"("created_at");

-- CreateIndex
CREATE INDEX "feedbacks_region_status_idx" ON "platform_feedback"."feedbacks"("region", "status");

-- CreateIndex
CREATE INDEX "flow_diagram_organization_id_created_by_id_updated_at_idx" ON "platform_flow_diagram"."flow_diagram"("organization_id", "created_by_id", "updated_at" DESC);

-- CreateIndex
CREATE INDEX "flow_diagram_tags_idx" ON "platform_flow_diagram"."flow_diagram" USING GIN ("tags");

-- CreateIndex
CREATE UNIQUE INDEX "form_definitions_key_key" ON "platform_form"."form_definitions"("key");

-- CreateIndex
CREATE UNIQUE INDEX "form_definitions_slug_key" ON "platform_form"."form_definitions"("slug");

-- CreateIndex
CREATE INDEX "form_definitions_key_idx" ON "platform_form"."form_definitions"("key");

-- CreateIndex
CREATE INDEX "form_definitions_slug_idx" ON "platform_form"."form_definitions"("slug");

-- CreateIndex
CREATE INDEX "form_definitions_category_idx" ON "platform_form"."form_definitions"("category");

-- CreateIndex
CREATE INDEX "form_definitions_status_idx" ON "platform_form"."form_definitions"("status");

-- CreateIndex
CREATE INDEX "form_definitions_organization_id_idx" ON "platform_form"."form_definitions"("organization_id");

-- CreateIndex
CREATE INDEX "form_definitions_created_by_idx" ON "platform_form"."form_definitions"("created_by");

-- CreateIndex
CREATE INDEX "form_versions_definition_id_idx" ON "platform_form"."form_versions"("definition_id");

-- CreateIndex
CREATE INDEX "form_versions_status_idx" ON "platform_form"."form_versions"("status");

-- CreateIndex
CREATE INDEX "form_versions_created_by_idx" ON "platform_form"."form_versions"("created_by");

-- CreateIndex
CREATE INDEX "form_versions_reviewed_by_idx" ON "platform_form"."form_versions"("reviewed_by");

-- CreateIndex
CREATE UNIQUE INDEX "form_versions_definition_id_version_key" ON "platform_form"."form_versions"("definition_id", "version");

-- CreateIndex
CREATE UNIQUE INDEX "form_instances_business_key_key" ON "platform_form"."form_instances"("business_key");

-- CreateIndex
CREATE INDEX "form_instances_form_definition_id_idx" ON "platform_form"."form_instances"("form_definition_id");

-- CreateIndex
CREATE INDEX "form_instances_form_version_id_idx" ON "platform_form"."form_instances"("form_version_id");

-- CreateIndex
CREATE INDEX "form_instances_form_key_idx" ON "platform_form"."form_instances"("form_key");

-- CreateIndex
CREATE INDEX "form_instances_form_version_idx" ON "platform_form"."form_instances"("form_version");

-- CreateIndex
CREATE INDEX "form_instances_snapshot_id_idx" ON "platform_form"."form_instances"("snapshot_id");

-- CreateIndex
CREATE INDEX "form_instances_business_key_idx" ON "platform_form"."form_instances"("business_key");

-- CreateIndex
CREATE INDEX "form_instances_region_id_idx" ON "platform_form"."form_instances"("region_id");

-- CreateIndex
CREATE INDEX "form_instances_created_by_idx" ON "platform_form"."form_instances"("created_by");

-- CreateIndex
CREATE INDEX "form_instances_submitted_by_idx" ON "platform_form"."form_instances"("submitted_by");

-- CreateIndex
CREATE INDEX "form_instances_status_idx" ON "platform_form"."form_instances"("status");

-- CreateIndex
CREATE INDEX "form_instances_approval_instance_id_idx" ON "platform_form"."form_instances"("approval_instance_id");

-- CreateIndex
CREATE INDEX "form_instances_created_at_idx" ON "platform_form"."form_instances"("created_at");

-- CreateIndex
CREATE INDEX "form_instances_deleted_at_idx" ON "platform_form"."form_instances"("deleted_at");

-- CreateIndex
CREATE INDEX "form_instances_form_definition_id_status_idx" ON "platform_form"."form_instances"("form_definition_id", "status");

-- CreateIndex
CREATE INDEX "form_instances_form_definition_id_deleted_at_idx" ON "platform_form"."form_instances"("form_definition_id", "deleted_at");

-- CreateIndex
CREATE INDEX "form_instances_region_id_status_idx" ON "platform_form"."form_instances"("region_id", "status");

-- CreateIndex
CREATE INDEX "form_instances_region_id_created_at_idx" ON "platform_form"."form_instances"("region_id", "created_at");

-- CreateIndex
CREATE INDEX "form_instances_form_definition_id_region_id_idx" ON "platform_form"."form_instances"("form_definition_id", "region_id");

-- CreateIndex
CREATE INDEX "form_translations_version_id_idx" ON "platform_form"."form_translations"("version_id");

-- CreateIndex
CREATE INDEX "form_translations_locale_idx" ON "platform_form"."form_translations"("locale");

-- CreateIndex
CREATE UNIQUE INDEX "form_translations_version_id_locale_key" ON "platform_form"."form_translations"("version_id", "locale");

-- CreateIndex
CREATE INDEX "form_templates_category_idx" ON "platform_form"."form_templates"("category");

-- CreateIndex
CREATE INDEX "form_templates_is_public_idx" ON "platform_form"."form_templates"("is_public");

-- CreateIndex
CREATE INDEX "form_templates_created_by_idx" ON "platform_form"."form_templates"("created_by");

-- CreateIndex
CREATE INDEX "release_snapshots_form_definition_id_idx" ON "platform_form"."release_snapshots"("form_definition_id");

-- CreateIndex
CREATE INDEX "release_snapshots_form_version_id_idx" ON "platform_form"."release_snapshots"("form_version_id");

-- CreateIndex
CREATE INDEX "release_snapshots_process_version_id_idx" ON "platform_form"."release_snapshots"("process_version_id");

-- CreateIndex
CREATE INDEX "release_snapshots_status_idx" ON "platform_form"."release_snapshots"("status");

-- CreateIndex
CREATE INDEX "release_snapshots_published_at_idx" ON "platform_form"."release_snapshots"("published_at");

-- CreateIndex
CREATE UNIQUE INDEX "release_snapshots_form_definition_id_status_key" ON "platform_form"."release_snapshots"("form_definition_id", "status");

-- CreateIndex
CREATE INDEX "form_webhooks_organization_id_idx" ON "platform_form"."form_webhooks"("organization_id");

-- CreateIndex
CREATE INDEX "form_webhooks_enabled_idx" ON "platform_form"."form_webhooks"("enabled");

-- CreateIndex
CREATE INDEX "form_webhooks_created_by_idx" ON "platform_form"."form_webhooks"("created_by");

-- CreateIndex
CREATE INDEX "form_webhook_logs_webhook_id_idx" ON "platform_form"."form_webhook_logs"("webhook_id");

-- CreateIndex
CREATE INDEX "form_webhook_logs_event_type_idx" ON "platform_form"."form_webhook_logs"("event_type");

-- CreateIndex
CREATE INDEX "form_webhook_logs_success_idx" ON "platform_form"."form_webhook_logs"("success");

-- CreateIndex
CREATE INDEX "form_webhook_logs_created_at_idx" ON "platform_form"."form_webhook_logs"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "platform_iam"."users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "platform_iam"."users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "users_employee_id_key" ON "platform_iam"."users"("employee_id");

-- CreateIndex
CREATE UNIQUE INDEX "users_adp_aoid_key" ON "platform_iam"."users"("adp_aoid");

-- CreateIndex
CREATE INDEX "users_status_idx" ON "platform_iam"."users"("status");

-- CreateIndex
CREATE INDEX "users_external_id_idx" ON "platform_iam"."users"("external_id");

-- CreateIndex
CREATE UNIQUE INDEX "roles_name_key" ON "platform_iam"."roles"("name");

-- CreateIndex
CREATE UNIQUE INDEX "roles_code_key" ON "platform_iam"."roles"("code");

-- CreateIndex
CREATE UNIQUE INDEX "permissions_resource_action_key" ON "platform_iam"."permissions"("resource", "action");

-- CreateIndex
CREATE UNIQUE INDEX "workflow_roles_name_key" ON "platform_iam"."workflow_roles"("name");

-- CreateIndex
CREATE UNIQUE INDEX "workflow_roles_code_key" ON "platform_iam"."workflow_roles"("code");

-- CreateIndex
CREATE UNIQUE INDEX "workflow_role_user_rel_workflow_role_id_user_id_key" ON "platform_iam"."workflow_role_user_rel"("workflow_role_id", "user_id");

-- CreateIndex
CREATE INDEX "user_role_rel_user_id_organization_id_idx" ON "platform_iam"."user_role_rel"("user_id", "organization_id");

-- CreateIndex
CREATE INDEX "user_role_rel_last_reviewed_at_idx" ON "platform_iam"."user_role_rel"("last_reviewed_at");

-- CreateIndex
CREATE UNIQUE INDEX "user_role_rel_user_id_role_id_organization_id_key" ON "platform_iam"."user_role_rel"("user_id", "role_id", "organization_id");

-- CreateIndex
CREATE UNIQUE INDEX "role_permission_rel_role_id_permission_id_key" ON "platform_iam"."role_permission_rel"("role_id", "permission_id");

-- CreateIndex
CREATE UNIQUE INDEX "data_scopes_code_key" ON "platform_iam"."data_scopes"("code");

-- CreateIndex
CREATE INDEX "role_data_scope_rel_role_id_resource_idx" ON "platform_iam"."role_data_scope_rel"("role_id", "resource");

-- CreateIndex
CREATE INDEX "role_data_scope_rel_data_scope_id_idx" ON "platform_iam"."role_data_scope_rel"("data_scope_id");

-- CreateIndex
CREATE UNIQUE INDEX "role_data_scope_rel_role_id_data_scope_id_resource_key" ON "platform_iam"."role_data_scope_rel"("role_id", "data_scope_id", "resource");

-- CreateIndex
CREATE INDEX "ai_tool_grants_tool_name_idx" ON "platform_iam"."ai_tool_grants"("tool_name");

-- CreateIndex
CREATE UNIQUE INDEX "ai_tool_grants_role_id_tool_name_key" ON "platform_iam"."ai_tool_grants"("role_id", "tool_name");

-- CreateIndex
CREATE INDEX "ai_tool_grants_user_tool_name_idx" ON "platform_iam"."ai_tool_grants_user"("tool_name");

-- CreateIndex
CREATE UNIQUE INDEX "ai_tool_grants_user_user_id_tool_name_key" ON "platform_iam"."ai_tool_grants_user"("user_id", "tool_name");

-- CreateIndex
CREATE INDEX "permission_delegations_to_user_id_valid_from_valid_to_idx" ON "platform_iam"."permission_delegations"("to_user_id", "valid_from", "valid_to");

-- CreateIndex
CREATE INDEX "permission_delegations_from_user_id_idx" ON "platform_iam"."permission_delegations"("from_user_id");

-- CreateIndex
CREATE INDEX "permission_delegations_organization_id_idx" ON "platform_iam"."permission_delegations"("organization_id");

-- CreateIndex
CREATE INDEX "field_permissions_resource_field_idx" ON "platform_iam"."field_permissions"("resource", "field");

-- CreateIndex
CREATE UNIQUE INDEX "field_permissions_role_id_resource_field_key" ON "platform_iam"."field_permissions"("role_id", "resource", "field");

-- CreateIndex
CREATE INDEX "iam_audit_log_actor_timestamp_idx" ON "platform_iam"."iam_audit_log"("actor", "timestamp");

-- CreateIndex
CREATE INDEX "iam_audit_log_resource_timestamp_idx" ON "platform_iam"."iam_audit_log"("resource", "timestamp");

-- CreateIndex
CREATE INDEX "knowledge_articles_created_by_idx" ON "platform_knowledge"."knowledge_articles"("created_by");

-- CreateIndex
CREATE INDEX "knowledge_articles_status_idx" ON "platform_knowledge"."knowledge_articles"("status");

-- CreateIndex
CREATE INDEX "knowledge_articles_created_at_idx" ON "platform_knowledge"."knowledge_articles"("created_at");

-- CreateIndex
CREATE INDEX "sp_document_index_sp_site_id_idx" ON "platform_knowledge"."sp_document_index"("sp_site_id");

-- CreateIndex
CREATE INDEX "sp_document_index_doc_authority_level_idx" ON "platform_knowledge"."sp_document_index"("doc_authority_level");

-- CreateIndex
CREATE INDEX "sp_document_index_doc_lifecycle_status_idx" ON "platform_knowledge"."sp_document_index"("doc_lifecycle_status");

-- CreateIndex
CREATE INDEX "sp_document_index_doc_type_idx" ON "platform_knowledge"."sp_document_index"("doc_type");

-- CreateIndex
CREATE INDEX "sp_document_index_sp_modified_at_idx" ON "platform_knowledge"."sp_document_index"("sp_modified_at");

-- CreateIndex
CREATE UNIQUE INDEX "sp_document_index_sp_item_id_sp_drive_id_key" ON "platform_knowledge"."sp_document_index"("sp_item_id", "sp_drive_id");

-- CreateIndex
CREATE INDEX "sp_folder_index_sp_site_id_idx" ON "platform_knowledge"."sp_folder_index"("sp_site_id");

-- CreateIndex
CREATE INDEX "sp_folder_index_sp_modified_at_idx" ON "platform_knowledge"."sp_folder_index"("sp_modified_at");

-- CreateIndex
CREATE INDEX "sp_folder_index_title_idx" ON "platform_knowledge"."sp_folder_index"("title");

-- CreateIndex
CREATE INDEX "sp_folder_index_folder_path_idx" ON "platform_knowledge"."sp_folder_index"("folder_path");

-- CreateIndex
CREATE UNIQUE INDEX "sp_folder_index_sp_item_id_sp_drive_id_key" ON "platform_knowledge"."sp_folder_index"("sp_item_id", "sp_drive_id");

-- CreateIndex
CREATE INDEX "ragflow_documents_sync_status_idx" ON "platform_knowledge"."ragflow_documents"("sync_status");

-- CreateIndex
CREATE INDEX "ragflow_documents_source_type_idx" ON "platform_knowledge"."ragflow_documents"("source_type");

-- CreateIndex
CREATE UNIQUE INDEX "ragflow_documents_source_type_source_id_key" ON "platform_knowledge"."ragflow_documents"("source_type", "source_id");

-- CreateIndex
CREATE UNIQUE INDEX "ragflow_documents_ragflow_document_id_key" ON "platform_knowledge"."ragflow_documents"("ragflow_document_id");

-- CreateIndex
CREATE INDEX "ai_qa_logs_user_id_idx" ON "platform_knowledge"."ai_qa_logs"("user_id");

-- CreateIndex
CREATE INDEX "ai_qa_logs_created_at_idx" ON "platform_knowledge"."ai_qa_logs"("created_at");

-- CreateIndex
CREATE INDEX "ai_qa_logs_feedback_idx" ON "platform_knowledge"."ai_qa_logs"("feedback");

-- CreateIndex
CREATE INDEX "search_logs_created_at_idx" ON "platform_knowledge"."search_logs"("created_at");

-- CreateIndex
CREATE INDEX "search_logs_search_type_idx" ON "platform_knowledge"."search_logs"("search_type");

-- CreateIndex
CREATE INDEX "document_views_user_id_idx" ON "platform_knowledge"."document_views"("user_id");

-- CreateIndex
CREATE INDEX "document_views_viewed_at_idx" ON "platform_knowledge"."document_views"("viewed_at");

-- CreateIndex
CREATE UNIQUE INDEX "document_views_sp_document_id_user_id_view_date_key" ON "platform_knowledge"."document_views"("sp_document_id", "user_id", "view_date");

-- CreateIndex
CREATE UNIQUE INDEX "document_views_article_id_user_id_view_date_key" ON "platform_knowledge"."document_views"("article_id", "user_id", "view_date");

-- CreateIndex
CREATE INDEX "document_likes_user_id_idx" ON "platform_knowledge"."document_likes"("user_id");

-- CreateIndex
CREATE INDEX "document_likes_type_idx" ON "platform_knowledge"."document_likes"("type");

-- CreateIndex
CREATE UNIQUE INDEX "document_likes_sp_document_id_user_id_key" ON "platform_knowledge"."document_likes"("sp_document_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "document_likes_article_id_user_id_key" ON "platform_knowledge"."document_likes"("article_id", "user_id");

-- CreateIndex
CREATE INDEX "document_comments_sp_document_id_idx" ON "platform_knowledge"."document_comments"("sp_document_id");

-- CreateIndex
CREATE INDEX "document_comments_article_id_idx" ON "platform_knowledge"."document_comments"("article_id");

-- CreateIndex
CREATE INDEX "document_comments_user_id_idx" ON "platform_knowledge"."document_comments"("user_id");

-- CreateIndex
CREATE INDEX "document_comments_parent_id_idx" ON "platform_knowledge"."document_comments"("parent_id");

-- CreateIndex
CREATE INDEX "document_comments_created_at_idx" ON "platform_knowledge"."document_comments"("created_at");

-- CreateIndex
CREATE INDEX "user_mentions_mentioned_user_id_idx" ON "platform_knowledge"."user_mentions"("mentioned_user_id");

-- CreateIndex
CREATE INDEX "user_mentions_read_at_idx" ON "platform_knowledge"."user_mentions"("read_at");

-- CreateIndex
CREATE UNIQUE INDEX "user_mentions_comment_id_mentioned_user_id_key" ON "platform_knowledge"."user_mentions"("comment_id", "mentioned_user_id");

-- CreateIndex
CREATE INDEX "sync_tasks_status_idx" ON "platform_knowledge"."sync_tasks"("status");

-- CreateIndex
CREATE INDEX "sync_tasks_created_at_idx" ON "platform_knowledge"."sync_tasks"("created_at");

-- CreateIndex
CREATE INDEX "sync_task_skipped_items_task_id_idx" ON "platform_knowledge"."sync_task_skipped_items"("task_id");

-- CreateIndex
CREATE INDEX "sync_task_skipped_items_source_type_idx" ON "platform_knowledge"."sync_task_skipped_items"("source_type");

-- CreateIndex
CREATE INDEX "sync_task_processed_items_task_id_idx" ON "platform_knowledge"."sync_task_processed_items"("task_id");

-- CreateIndex
CREATE INDEX "sync_task_processed_items_source_type_idx" ON "platform_knowledge"."sync_task_processed_items"("source_type");

-- CreateIndex
CREATE INDEX "sync_task_processed_items_ragflow_document_id_idx" ON "platform_knowledge"."sync_task_processed_items"("ragflow_document_id");

-- CreateIndex
CREATE INDEX "sync_task_processed_items_status_idx" ON "platform_knowledge"."sync_task_processed_items"("status");

-- CreateIndex
CREATE INDEX "sync_task_failed_items_task_id_idx" ON "platform_knowledge"."sync_task_failed_items"("task_id");

-- CreateIndex
CREATE INDEX "sync_task_failed_items_source_type_idx" ON "platform_knowledge"."sync_task_failed_items"("source_type");

-- CreateIndex
CREATE INDEX "sharepoint_sync_cursors_drive_id_idx" ON "platform_knowledge"."sharepoint_sync_cursors"("drive_id");

-- CreateIndex
CREATE UNIQUE INDEX "sharepoint_sync_cursors_drive_id_scope_path_key" ON "platform_knowledge"."sharepoint_sync_cursors"("drive_id", "scope_path");

-- CreateIndex
CREATE UNIQUE INDEX "webhook_subscriptions_subscription_id_key" ON "platform_knowledge"."webhook_subscriptions"("subscription_id");

-- CreateIndex
CREATE INDEX "webhook_subscriptions_drive_id_idx" ON "platform_knowledge"."webhook_subscriptions"("drive_id");

-- CreateIndex
CREATE INDEX "webhook_subscriptions_is_active_idx" ON "platform_knowledge"."webhook_subscriptions"("is_active");

-- CreateIndex
CREATE INDEX "webhook_subscriptions_expiration_date_time_idx" ON "platform_knowledge"."webhook_subscriptions"("expiration_date_time");

-- CreateIndex
CREATE INDEX "system_log_created_at_idx" ON "platform_logging"."system_log"("created_at" DESC);

-- CreateIndex
CREATE INDEX "system_log_level_created_at_idx" ON "platform_logging"."system_log"("level", "created_at" DESC);

-- CreateIndex
CREATE INDEX "system_log_trace_id_idx" ON "platform_logging"."system_log"("trace_id");

-- CreateIndex
CREATE INDEX "system_log_user_id_created_at_idx" ON "platform_logging"."system_log"("user_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "system_log_service_created_at_idx" ON "platform_logging"."system_log"("service", "created_at" DESC);

-- CreateIndex
CREATE INDEX "system_log_status_code_created_at_idx" ON "platform_logging"."system_log"("status_code", "created_at" DESC);

-- CreateIndex
CREATE INDEX "system_log_duration_ms_idx" ON "platform_logging"."system_log"("duration_ms" DESC);

-- CreateIndex
CREATE INDEX "system_log_error_type_created_at_idx" ON "platform_logging"."system_log"("error_type", "created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "log_config_key_key" ON "platform_logging"."log_config"("key");

-- CreateIndex
CREATE INDEX "log_alert_type_created_at_idx" ON "platform_logging"."log_alert"("type", "created_at" DESC);

-- CreateIndex
CREATE INDEX "log_alert_status_created_at_idx" ON "platform_logging"."log_alert"("status", "created_at" DESC);

-- CreateIndex
CREATE INDEX "log_alert_severity_created_at_idx" ON "platform_logging"."log_alert"("severity", "created_at" DESC);

-- CreateIndex
CREATE INDEX "log_cleanup_record_started_at_idx" ON "platform_logging"."log_cleanup_record"("started_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "attendances_user_id_meeting_id_key" ON "platform_meeting_attendance"."attendances"("user_id", "meeting_id");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_required_attendees_meeting_id_user_id_key" ON "platform_meeting_attendance"."meeting_required_attendees"("meeting_id", "user_id");

-- CreateIndex
CREATE INDEX "meeting_series_attendee_preferences_user_id_idx" ON "platform_meeting_attendance"."meeting_series_attendee_preferences"("user_id");

-- CreateIndex
CREATE INDEX "meeting_external_attendees_meeting_id_idx" ON "platform_meeting_attendance"."meeting_external_attendees"("meeting_id");

-- CreateIndex
CREATE INDEX "meeting_external_attendees_email_idx" ON "platform_meeting_attendance"."meeting_external_attendees"("email");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_external_attendees_meeting_id_email_source_role_key" ON "platform_meeting_attendance"."meeting_external_attendees"("meeting_id", "email", "source_role");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_template_attendees_template_id_user_id_key" ON "platform_meeting_attendance"."meeting_template_attendees"("template_id", "user_id");

-- CreateIndex
CREATE INDEX "audit_logs_user_id_idx" ON "platform_meeting_attendance"."audit_logs"("user_id");

-- CreateIndex
CREATE INDEX "audit_logs_user_email_idx" ON "platform_meeting_attendance"."audit_logs"("user_email");

-- CreateIndex
CREATE INDEX "audit_logs_action_idx" ON "platform_meeting_attendance"."audit_logs"("action");

-- CreateIndex
CREATE INDEX "audit_logs_resource_idx" ON "platform_meeting_attendance"."audit_logs"("resource");

-- CreateIndex
CREATE INDEX "audit_logs_resource_id_idx" ON "platform_meeting_attendance"."audit_logs"("resource_id");

-- CreateIndex
CREATE INDEX "audit_logs_created_at_idx" ON "platform_meeting_attendance"."audit_logs"("created_at");

-- CreateIndex
CREATE INDEX "audit_logs_trace_id_idx" ON "platform_meeting_attendance"."audit_logs"("trace_id");

-- CreateIndex
CREATE INDEX "audit_logs_session_id_idx" ON "platform_meeting_attendance"."audit_logs"("session_id");

-- CreateIndex
CREATE INDEX "audit_logs_device_id_idx" ON "platform_meeting_attendance"."audit_logs"("device_id");

-- CreateIndex
CREATE INDEX "audit_logs_source_idx" ON "platform_meeting_attendance"."audit_logs"("source");

-- CreateIndex
CREATE INDEX "audit_logs_status_code_idx" ON "platform_meeting_attendance"."audit_logs"("status_code");

-- CreateIndex
CREATE INDEX "data_access_logs_actor_id_idx" ON "platform_meeting_attendance"."data_access_logs"("actor_id");

-- CreateIndex
CREATE INDEX "data_access_logs_actor_email_idx" ON "platform_meeting_attendance"."data_access_logs"("actor_email");

-- CreateIndex
CREATE INDEX "data_access_logs_access_type_idx" ON "platform_meeting_attendance"."data_access_logs"("access_type");

-- CreateIndex
CREATE INDEX "data_access_logs_entity_type_idx" ON "platform_meeting_attendance"."data_access_logs"("entity_type");

-- CreateIndex
CREATE INDEX "data_access_logs_created_at_idx" ON "platform_meeting_attendance"."data_access_logs"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "audit_alert_rules_name_key" ON "platform_meeting_attendance"."audit_alert_rules"("name");

-- CreateIndex
CREATE INDEX "audit_alert_rules_rule_type_idx" ON "platform_meeting_attendance"."audit_alert_rules"("rule_type");

-- CreateIndex
CREATE INDEX "audit_alert_rules_severity_idx" ON "platform_meeting_attendance"."audit_alert_rules"("severity");

-- CreateIndex
CREATE INDEX "audit_alert_rules_is_enabled_idx" ON "platform_meeting_attendance"."audit_alert_rules"("is_enabled");

-- CreateIndex
CREATE INDEX "audit_alerts_rule_id_idx" ON "platform_meeting_attendance"."audit_alerts"("rule_id");

-- CreateIndex
CREATE INDEX "audit_alerts_status_idx" ON "platform_meeting_attendance"."audit_alerts"("status");

-- CreateIndex
CREATE INDEX "audit_alerts_severity_idx" ON "platform_meeting_attendance"."audit_alerts"("severity");

-- CreateIndex
CREATE INDEX "audit_alerts_created_at_idx" ON "platform_meeting_attendance"."audit_alerts"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "retention_policies_entity_type_key" ON "platform_meeting_attendance"."retention_policies"("entity_type");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_mailboxes_mailbox_email_key" ON "platform_meeting_attendance"."outlook_sync_mailboxes"("mailbox_email");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_subscriptions_graph_subscription_id_key" ON "platform_meeting_attendance"."outlook_subscriptions"("graph_subscription_id");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_mailbox_id_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("mailbox_id");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_status_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("status");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_expiration_at_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("expiration_at");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_ical_uid_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("ical_uid");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_owner_user_id_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("owner_user_id");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_manage_status_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("manage_status");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_manage_status_sync_mode_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("manage_status", "sync_mode");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_bootstrap_status_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("bootstrap_status");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_meeting_id_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("meeting_id");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_bindings_graph_event_id_key" ON "platform_meeting_attendance"."outlook_sync_bindings"("graph_event_id");

-- CreateIndex
CREATE INDEX "outlook_series_occurrence_exclusions_occurrence_graph_event_idx" ON "platform_meeting_attendance"."outlook_series_occurrence_exclusions"("occurrence_graph_event_id");

-- CreateIndex
CREATE INDEX "outlook_series_occurrence_exclusions_created_at_idx" ON "platform_meeting_attendance"."outlook_series_occurrence_exclusions"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_series_occurrence_exclusions_binding_id_occurrence__key" ON "platform_meeting_attendance"."outlook_series_occurrence_exclusions"("binding_id", "occurrence_graph_event_id");

-- CreateIndex
CREATE INDEX "outlook_sync_event_logs_binding_id_created_at_idx" ON "platform_meeting_attendance"."outlook_sync_event_logs"("binding_id", "created_at");

-- CreateIndex
CREATE INDEX "outlook_sync_event_logs_mailbox_id_created_at_idx" ON "platform_meeting_attendance"."outlook_sync_event_logs"("mailbox_id", "created_at");

-- CreateIndex
CREATE INDEX "outlook_sync_event_logs_event_type_idx" ON "platform_meeting_attendance"."outlook_sync_event_logs"("event_type");

-- CreateIndex
CREATE INDEX "outlook_event_source_versions_binding_id_created_at_idx" ON "platform_meeting_attendance"."outlook_event_source_versions"("binding_id", "created_at");

-- CreateIndex
CREATE INDEX "outlook_event_source_versions_mailbox_id_graph_event_id_cre_idx" ON "platform_meeting_attendance"."outlook_event_source_versions"("mailbox_id", "graph_event_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_event_source_versions_binding_id_payload_hash_key" ON "platform_meeting_attendance"."outlook_event_source_versions"("binding_id", "payload_hash");

-- CreateIndex
CREATE INDEX "outlook_event_sync_diffs_binding_id_detected_at_idx" ON "platform_meeting_attendance"."outlook_event_sync_diffs"("binding_id", "detected_at");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_cursors_mailbox_id_key" ON "platform_meeting_attendance"."outlook_sync_cursors"("mailbox_id");

-- CreateIndex
CREATE INDEX "outlook_event_snapshots_mailbox_id_start_time_idx" ON "platform_meeting_attendance"."outlook_event_snapshots"("mailbox_id", "start_time");

-- CreateIndex
CREATE INDEX "outlook_event_snapshots_mailbox_id_event_type_idx" ON "platform_meeting_attendance"."outlook_event_snapshots"("mailbox_id", "event_type");

-- CreateIndex
CREATE INDEX "outlook_event_snapshots_mailbox_id_is_cancelled_idx" ON "platform_meeting_attendance"."outlook_event_snapshots"("mailbox_id", "is_cancelled");

-- CreateIndex
CREATE INDEX "outlook_event_snapshots_mailbox_id_series_master_id_idx" ON "platform_meeting_attendance"."outlook_event_snapshots"("mailbox_id", "series_master_id");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_event_snapshots_mailbox_id_graph_event_id_key" ON "platform_meeting_attendance"."outlook_event_snapshots"("mailbox_id", "graph_event_id");

-- CreateIndex
CREATE INDEX "notification_messages_user_id_status_idx" ON "platform_notify"."notification_messages"("user_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "notification_preferences_user_id_key" ON "platform_notify"."notification_preferences"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "notification_templates_code_key" ON "platform_notify"."notification_templates"("code");

-- CreateIndex
CREATE INDEX "notification_templates_code_idx" ON "platform_notify"."notification_templates"("code");

-- CreateIndex
CREATE INDEX "notification_templates_channel_idx" ON "platform_notify"."notification_templates"("channel");

-- CreateIndex
CREATE INDEX "notification_templates_is_active_idx" ON "platform_notify"."notification_templates"("is_active");

-- CreateIndex
CREATE INDEX "notification_log_recipient_id_created_at_idx" ON "platform_notify"."notification_log"("recipient_id", "created_at");

-- CreateIndex
CREATE INDEX "notification_log_status_next_retry_at_idx" ON "platform_notify"."notification_log"("status", "next_retry_at");

-- CreateIndex
CREATE INDEX "notification_log_channel_status_idx" ON "platform_notify"."notification_log"("channel", "status");

-- CreateIndex
CREATE INDEX "notification_log_created_at_idx" ON "platform_notify"."notification_log"("created_at");

-- CreateIndex
CREATE INDEX "m365_users_organization_id_days_inactive_idx" ON "platform_ops_center"."m365_users"("organization_id", "days_inactive" DESC);

-- CreateIndex
CREATE INDEX "m365_users_organization_id_user_principal_name_idx" ON "platform_ops_center"."m365_users"("organization_id", "user_principal_name");

-- CreateIndex
CREATE INDEX "m365_users_organization_id_last_seen_in_execution_id_idx" ON "platform_ops_center"."m365_users"("organization_id", "last_seen_in_execution_id");

-- CreateIndex
CREATE UNIQUE INDEX "m365_users_organization_id_graph_user_id_key" ON "platform_ops_center"."m365_users"("organization_id", "graph_user_id");

-- CreateIndex
CREATE INDEX "m365_user_activity_changes_user_id_created_at_idx" ON "platform_ops_center"."m365_user_activity_changes"("user_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "m365_user_activity_changes_organization_id_execution_id_idx" ON "platform_ops_center"."m365_user_activity_changes"("organization_id", "execution_id");

-- CreateIndex
CREATE INDEX "m365_user_activity_changes_organization_id_field_created_at_idx" ON "platform_ops_center"."m365_user_activity_changes"("organization_id", "field", "created_at" DESC);

-- CreateIndex
CREATE INDEX "grade_config_is_default_is_active_idx" ON "platform_performance"."grade_config"("is_default", "is_active");

-- CreateIndex
CREATE INDEX "grade_config_organization_id_idx" ON "platform_performance"."grade_config"("organization_id");

-- CreateIndex
CREATE INDEX "performance_cycle_status_idx" ON "platform_performance"."performance_cycle"("status");

-- CreateIndex
CREATE INDEX "performance_cycle_start_date_end_date_idx" ON "platform_performance"."performance_cycle"("start_date", "end_date");

-- CreateIndex
CREATE INDEX "performance_cycle_type_status_idx" ON "platform_performance"."performance_cycle"("type", "status");

-- CreateIndex
CREATE INDEX "performance_cycle_organization_id_idx" ON "platform_performance"."performance_cycle"("organization_id");

-- CreateIndex
CREATE INDEX "performance_cycle_parent_cycle_id_idx" ON "platform_performance"."performance_cycle"("parent_cycle_id");

-- CreateIndex
CREATE INDEX "performance_cycle_grade_config_id_idx" ON "platform_performance"."performance_cycle"("grade_config_id");

-- CreateIndex
CREATE INDEX "performance_result_employee_id_is_published_idx" ON "platform_performance"."performance_result"("employee_id", "is_published");

-- CreateIndex
CREATE INDEX "performance_result_cycle_id_confirm_status_idx" ON "platform_performance"."performance_result"("cycle_id", "confirm_status");

-- CreateIndex
CREATE INDEX "performance_result_cycle_id_grade_code_idx" ON "platform_performance"."performance_result"("cycle_id", "grade_code");

-- CreateIndex
CREATE INDEX "performance_result_organization_id_idx" ON "platform_performance"."performance_result"("organization_id");

-- CreateIndex
CREATE UNIQUE INDEX "performance_result_cycle_id_employee_id_key" ON "platform_performance"."performance_result"("cycle_id", "employee_id");

-- CreateIndex
CREATE INDEX "kpi_assignment_cycle_id_employee_id_idx" ON "platform_performance"."kpi_assignment"("cycle_id", "employee_id");

-- CreateIndex
CREATE INDEX "kpi_assignment_cycle_id_employee_id_assignment_status_idx" ON "platform_performance"."kpi_assignment"("cycle_id", "employee_id", "assignment_status");

-- CreateIndex
CREATE INDEX "kpi_assignment_cycle_id_assignment_status_idx" ON "platform_performance"."kpi_assignment"("cycle_id", "assignment_status");

-- CreateIndex
CREATE INDEX "kpi_assignment_parent_id_idx" ON "platform_performance"."kpi_assignment"("parent_id");

-- CreateIndex
CREATE UNIQUE INDEX "kpi_assignment_cycle_id_employee_id_name_key" ON "platform_performance"."kpi_assignment"("cycle_id", "employee_id", "name");

-- CreateIndex
CREATE INDEX "kpi_dependency_target_user_id_status_idx" ON "platform_performance"."kpi_dependency"("target_user_id", "status");

-- CreateIndex
CREATE INDEX "kpi_dependency_source_assignment_id_idx" ON "platform_performance"."kpi_dependency"("source_assignment_id");

-- CreateIndex
CREATE UNIQUE INDEX "kpi_dependency_source_assignment_id_target_user_id_key" ON "platform_performance"."kpi_dependency"("source_assignment_id", "target_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "kpi_assessment_assignment_id_key" ON "platform_performance"."kpi_assessment"("assignment_id");

-- CreateIndex
CREATE INDEX "kpi_assessment_status_idx" ON "platform_performance"."kpi_assessment"("status");

-- CreateIndex
CREATE INDEX "evaluation_360_cycle_id_target_id_idx" ON "platform_performance"."evaluation_360"("cycle_id", "target_id");

-- CreateIndex
CREATE INDEX "evaluation_360_cycle_id_target_id_status_idx" ON "platform_performance"."evaluation_360"("cycle_id", "target_id", "status");

-- CreateIndex
CREATE INDEX "evaluation_360_status_idx" ON "platform_performance"."evaluation_360"("status");

-- CreateIndex
CREATE INDEX "evaluation_360_template_id_idx" ON "platform_performance"."evaluation_360"("template_id");

-- CreateIndex
CREATE INDEX "evaluation_task_evaluator_id_status_idx" ON "platform_performance"."evaluation_task"("evaluator_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "evaluation_task_evaluation_id_evaluator_id_key" ON "platform_performance"."evaluation_task"("evaluation_id", "evaluator_id");

-- CreateIndex
CREATE INDEX "evaluation_response_task_id_idx" ON "platform_performance"."evaluation_response"("task_id");

-- CreateIndex
CREATE INDEX "grade_adjustment_log_result_id_idx" ON "platform_performance"."grade_adjustment_log"("result_id");

-- CreateIndex
CREATE INDEX "strategic_objective_cycle_id_idx" ON "platform_performance"."strategic_objective"("cycle_id");

-- CreateIndex
CREATE INDEX "strategic_objective_department_id_idx" ON "platform_performance"."strategic_objective"("department_id");

-- CreateIndex
CREATE INDEX "strategic_objective_assignment_assignee_id_idx" ON "platform_performance"."strategic_objective_assignment"("assignee_id");

-- CreateIndex
CREATE UNIQUE INDEX "strategic_objective_assignment_objective_id_assignee_id_key" ON "platform_performance"."strategic_objective_assignment"("objective_id", "assignee_id");

-- CreateIndex
CREATE INDEX "evaluation_360_template_is_default_is_active_idx" ON "platform_performance"."evaluation_360_template"("is_default", "is_active");

-- CreateIndex
CREATE INDEX "evaluation_360_template_organization_id_idx" ON "platform_performance"."evaluation_360_template"("organization_id");

-- CreateIndex
CREATE UNIQUE INDEX "site_checkpoints_code_key" ON "platform_site_attendance"."site_checkpoints"("code");

-- CreateIndex
CREATE INDEX "site_checkpoints_is_active_idx" ON "platform_site_attendance"."site_checkpoints"("is_active");

-- CreateIndex
CREATE INDEX "site_checkpoints_shared_checkin_enabled_is_active_idx" ON "platform_site_attendance"."site_checkpoints"("shared_checkin_enabled", "is_active");

-- CreateIndex
CREATE INDEX "shared_checkin_partners_checkpoint_id_is_active_sort_order_idx" ON "platform_site_attendance"."shared_checkin_partners"("checkpoint_id", "is_active", "sort_order");

-- CreateIndex
CREATE INDEX "shared_checkin_ticket_usage_expires_at_idx" ON "platform_site_attendance"."shared_checkin_ticket_usage"("expires_at");

-- CreateIndex
CREATE INDEX "site_attendance_events_checkpoint_id_user_id_local_date_idx" ON "platform_site_attendance"."site_attendance_events"("checkpoint_id", "user_id", "local_date");

-- CreateIndex
CREATE INDEX "site_attendance_events_checkpoint_id_local_date_idx" ON "platform_site_attendance"."site_attendance_events"("checkpoint_id", "local_date");

-- CreateIndex
CREATE INDEX "site_attendance_events_user_id_local_date_idx" ON "platform_site_attendance"."site_attendance_events"("user_id", "local_date");

-- CreateIndex
CREATE INDEX "site_attendance_events_timestamp_idx" ON "platform_site_attendance"."site_attendance_events"("timestamp");

-- CreateIndex
CREATE INDEX "site_daily_summaries_checkpoint_id_local_date_idx" ON "platform_site_attendance"."site_daily_summaries"("checkpoint_id", "local_date");

-- CreateIndex
CREATE INDEX "site_daily_summaries_user_id_local_date_idx" ON "platform_site_attendance"."site_daily_summaries"("user_id", "local_date");

-- CreateIndex
CREATE UNIQUE INDEX "site_daily_summaries_checkpoint_id_user_id_local_date_key" ON "platform_site_attendance"."site_daily_summaries"("checkpoint_id", "user_id", "local_date");

-- CreateIndex
CREATE UNIQUE INDEX "tickets_ticket_no_key" ON "platform_tickets"."tickets"("ticket_no");

-- CreateIndex
CREATE INDEX "tickets_ticket_no_idx" ON "platform_tickets"."tickets"("ticket_no");

-- CreateIndex
CREATE INDEX "tickets_status_idx" ON "platform_tickets"."tickets"("status");

-- CreateIndex
CREATE INDEX "tickets_category_id_idx" ON "platform_tickets"."tickets"("category_id");

-- CreateIndex
CREATE INDEX "tickets_creator_id_idx" ON "platform_tickets"."tickets"("creator_id");

-- CreateIndex
CREATE INDEX "tickets_assignee_id_idx" ON "platform_tickets"."tickets"("assignee_id");

-- CreateIndex
CREATE INDEX "tickets_assignee_group_id_idx" ON "platform_tickets"."tickets"("assignee_group_id");

-- CreateIndex
CREATE INDEX "tickets_created_at_idx" ON "platform_tickets"."tickets"("created_at");

-- CreateIndex
CREATE INDEX "tickets_region_idx" ON "platform_tickets"."tickets"("region");

-- CreateIndex
CREATE INDEX "tickets_tenant_id_idx" ON "platform_tickets"."tickets"("tenant_id");

-- CreateIndex
CREATE INDEX "tickets_tenant_id_region_idx" ON "platform_tickets"."tickets"("tenant_id", "region");

-- CreateIndex
CREATE UNIQUE INDEX "ticket_categories_code_key" ON "platform_tickets"."ticket_categories"("code");

-- CreateIndex
CREATE INDEX "ticket_categories_code_idx" ON "platform_tickets"."ticket_categories"("code");

-- CreateIndex
CREATE INDEX "ticket_categories_parent_id_idx" ON "platform_tickets"."ticket_categories"("parent_id");

-- CreateIndex
CREATE INDEX "ticket_categories_is_active_idx" ON "platform_tickets"."ticket_categories"("is_active");

-- CreateIndex
CREATE INDEX "ticket_comments_ticket_id_idx" ON "platform_tickets"."ticket_comments"("ticket_id");

-- CreateIndex
CREATE INDEX "ticket_comments_author_id_idx" ON "platform_tickets"."ticket_comments"("author_id");

-- CreateIndex
CREATE INDEX "ticket_attachments_ticket_id_idx" ON "platform_tickets"."ticket_attachments"("ticket_id");

-- CreateIndex
CREATE INDEX "ticket_attachments_comment_id_idx" ON "platform_tickets"."ticket_attachments"("comment_id");

-- CreateIndex
CREATE INDEX "ticket_activities_ticket_id_idx" ON "platform_tickets"."ticket_activities"("ticket_id");

-- CreateIndex
CREATE INDEX "ticket_activities_created_at_idx" ON "platform_tickets"."ticket_activities"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "assignment_groups_code_key" ON "platform_tickets"."assignment_groups"("code");

-- CreateIndex
CREATE INDEX "assignment_groups_code_idx" ON "platform_tickets"."assignment_groups"("code");

-- CreateIndex
CREATE INDEX "robot_field_defs_scope_group_sort_order_idx" ON "robot_manager"."robot_field_defs"("scope", "group", "sort_order");

-- CreateIndex
CREATE INDEX "robot_field_defs_enabled_idx" ON "robot_manager"."robot_field_defs"("enabled");

-- CreateIndex
CREATE UNIQUE INDEX "robot_field_defs_scope_key_key" ON "robot_manager"."robot_field_defs"("scope", "key");

-- CreateIndex
CREATE UNIQUE INDEX "robot_system_config_key_key" ON "robot_manager"."robot_system_config"("key");

-- CreateIndex
CREATE UNIQUE INDEX "robot_models_code_key" ON "robot_manager"."robot_models"("code");

-- CreateIndex
CREATE INDEX "robot_models_enabled_idx" ON "robot_manager"."robot_models"("enabled");

-- CreateIndex
CREATE UNIQUE INDEX "robot_skus_code_key" ON "robot_manager"."robot_skus"("code");

-- CreateIndex
CREATE INDEX "robot_skus_model_id_enabled_idx" ON "robot_manager"."robot_skus"("model_id", "enabled");

-- CreateIndex
CREATE UNIQUE INDEX "robot_suppliers_code_key" ON "robot_manager"."robot_suppliers"("code");

-- CreateIndex
CREATE INDEX "robot_suppliers_enabled_idx" ON "robot_manager"."robot_suppliers"("enabled");

-- CreateIndex
CREATE UNIQUE INDEX "robot_customers_code_key" ON "robot_manager"."robot_customers"("code");

-- CreateIndex
CREATE INDEX "robot_customers_enabled_idx" ON "robot_manager"."robot_customers"("enabled");

-- CreateIndex
CREATE UNIQUE INDEX "robot_locations_code_key" ON "robot_manager"."robot_locations"("code");

-- CreateIndex
CREATE INDEX "robot_locations_enabled_idx" ON "robot_manager"."robot_locations"("enabled");

-- CreateIndex
CREATE INDEX "robot_locations_customer_id_idx" ON "robot_manager"."robot_locations"("customer_id");

-- CreateIndex
CREATE INDEX "robot_locations_type_code_idx" ON "robot_manager"."robot_locations"("type_code");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_current_status_idx" ON "robot_manager"."robot_units"("organization_id", "current_status");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_model_id_idx" ON "robot_manager"."robot_units"("organization_id", "model_id");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_customer_id_idx" ON "robot_manager"."robot_units"("organization_id", "customer_id");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_supplier_id_idx" ON "robot_manager"."robot_units"("organization_id", "supplier_id");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_location_id_idx" ON "robot_manager"."robot_units"("organization_id", "location_id");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_deleted_at_idx" ON "robot_manager"."robot_units"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "robot_service_records_robot_unit_id_idx" ON "robot_manager"."robot_service_records"("robot_unit_id");

-- CreateIndex
CREATE INDEX "robot_status_change_logs_robot_unit_id_idx" ON "robot_manager"."robot_status_change_logs"("robot_unit_id");

-- CreateIndex
CREATE INDEX "robot_status_change_logs_robot_unit_id_changed_at_idx" ON "robot_manager"."robot_status_change_logs"("robot_unit_id", "changed_at");

-- CreateIndex
CREATE INDEX "robot_attachments_robot_unit_id_idx" ON "robot_manager"."robot_attachments"("robot_unit_id");

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_definitions" ADD CONSTRAINT "approval_definitions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_versions" ADD CONSTRAINT "approval_versions_definition_id_fkey" FOREIGN KEY ("definition_id") REFERENCES "corp_approval"."approval_definitions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_instances" ADD CONSTRAINT "approval_instances_initiator_id_fkey" FOREIGN KEY ("initiator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_instances" ADD CONSTRAINT "approval_instances_version_id_fkey" FOREIGN KEY ("version_id") REFERENCES "corp_approval"."approval_versions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_node_instances" ADD CONSTRAINT "approval_node_instances_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "corp_approval"."approval_instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_tasks" ADD CONSTRAINT "approval_tasks_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "corp_approval"."approval_instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_tasks" ADD CONSTRAINT "approval_tasks_node_instance_id_fkey" FOREIGN KEY ("node_instance_id") REFERENCES "corp_approval"."approval_node_instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_task_logs" ADD CONSTRAINT "approval_task_logs_operator_id_fkey" FOREIGN KEY ("operator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_task_logs" ADD CONSTRAINT "approval_task_logs_target_user_id_fkey" FOREIGN KEY ("target_user_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_task_logs" ADD CONSTRAINT "approval_task_logs_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "corp_approval"."approval_tasks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."callback_retry_queue" ADD CONSTRAINT "callback_retry_queue_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "corp_approval"."approval_instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."reminder_queue" ADD CONSTRAINT "reminder_queue_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "corp_approval"."approval_tasks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."reminder_queue" ADD CONSTRAINT "reminder_queue_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "corp_approval"."approval_instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."user_delegation_settings" ADD CONSTRAINT "user_delegation_settings_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."user_delegation_settings" ADD CONSTRAINT "user_delegation_settings_delegate_user_id_fkey" FOREIGN KEY ("delegate_user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_admin_exports" ADD CONSTRAINT "approval_admin_exports_requested_by_fkey" FOREIGN KEY ("requested_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."organizations" ADD CONSTRAINT "organizations_primary_region_id_fkey" FOREIGN KEY ("primary_region_id") REFERENCES "corp_hr"."regions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."organization_regions" ADD CONSTRAINT "organization_regions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."organization_regions" ADD CONSTRAINT "organization_regions_region_id_fkey" FOREIGN KEY ("region_id") REFERENCES "corp_hr"."regions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."departments" ADD CONSTRAINT "departments_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."departments" ADD CONSTRAINT "departments_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "corp_hr"."departments"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."user_departments" ADD CONSTRAINT "user_departments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."user_departments" ADD CONSTRAINT "user_departments_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."user_departments" ADD CONSTRAINT "user_departments_department_id_fkey" FOREIGN KEY ("department_id") REFERENCES "corp_hr"."departments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."user_departments" ADD CONSTRAINT "user_departments_position_id_fkey" FOREIGN KEY ("position_id") REFERENCES "corp_hr"."positions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_hr"."user_departments" ADD CONSTRAINT "user_departments_manager_id_fkey" FOREIGN KEY ("manager_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_request"."expense_requests" ADD CONSTRAINT "expense_requests_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_request"."expense_items" ADD CONSTRAINT "expense_items_expense_id_fkey" FOREIGN KEY ("expense_id") REFERENCES "corp_request"."expense_requests"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_request"."purchase_requests" ADD CONSTRAINT "purchase_requests_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_request"."purchase_items" ADD CONSTRAINT "purchase_items_purchase_id_fkey" FOREIGN KEY ("purchase_id") REFERENCES "corp_request"."purchase_requests"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "corp_request"."contract_requests" ADD CONSTRAINT "contract_requests_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."parts" ADD CONSTRAINT "parts_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "mfg_inventory"."warehouses"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."parts" ADD CONSTRAINT "parts_storage_location_id_fkey" FOREIGN KEY ("storage_location_id") REFERENCES "mfg_inventory"."storage_locations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."parts" ADD CONSTRAINT "parts_station_id_fkey" FOREIGN KEY ("station_id") REFERENCES "mfg_inventory"."stations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."storage_locations" ADD CONSTRAINT "storage_locations_warehouse_id_fkey" FOREIGN KEY ("warehouse_id") REFERENCES "mfg_inventory"."warehouses"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."part_labels" ADD CONSTRAINT "part_labels_part_id_fkey" FOREIGN KEY ("part_id") REFERENCES "mfg_inventory"."parts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."inventory_logs" ADD CONSTRAINT "inventory_logs_part_id_fkey" FOREIGN KEY ("part_id") REFERENCES "mfg_inventory"."parts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."stock_alerts" ADD CONSTRAINT "stock_alerts_part_id_fkey" FOREIGN KEY ("part_id") REFERENCES "mfg_inventory"."parts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."group_custom_fields" ADD CONSTRAINT "group_custom_fields_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "mfg_inventory"."part_groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."part_group_relations" ADD CONSTRAINT "part_group_relations_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "mfg_inventory"."part_groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mfg_inventory"."part_group_relations" ADD CONSTRAINT "part_group_relations_part_id_fkey" FOREIGN KEY ("part_id") REFERENCES "mfg_inventory"."parts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_ai"."ai_messages" ADD CONSTRAINT "ai_messages_conversationId_fkey" FOREIGN KEY ("conversationId") REFERENCES "platform_ai"."ai_conversations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_ai"."ai_message_feedbacks" ADD CONSTRAINT "ai_message_feedbacks_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "platform_ai"."ai_messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_ai"."ai_assistant_tickets" ADD CONSTRAINT "ai_assistant_tickets_conversationId_fkey" FOREIGN KEY ("conversationId") REFERENCES "platform_ai"."ai_conversations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_ai"."ai_knowledge_fixes" ADD CONSTRAINT "ai_knowledge_fixes_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "platform_ai"."ai_messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_audit"."audit_log" ADD CONSTRAINT "audit_log_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_audit"."audit_database_change_log" ADD CONSTRAINT "audit_database_change_log_audit_log_id_fkey" FOREIGN KEY ("audit_log_id") REFERENCES "platform_audit"."audit_log"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_audit"."audit_sensitive_operation_log" ADD CONSTRAINT "audit_sensitive_operation_log_audit_log_id_fkey" FOREIGN KEY ("audit_log_id") REFERENCES "platform_audit"."audit_log"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_automation"."automation_tasks" ADD CONSTRAINT "automation_tasks_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_automation"."automation_executions" ADD CONSTRAINT "automation_executions_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "platform_automation"."automation_tasks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_automation"."adp_pto_schedules" ADD CONSTRAINT "adp_pto_schedules_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_devtracker"."dev_items" ADD CONSTRAINT "dev_items_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "platform_devtracker"."dev_items"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_devtracker"."dev_items" ADD CONSTRAINT "dev_items_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_devtracker"."dev_items" ADD CONSTRAINT "dev_items_reporter_id_fkey" FOREIGN KEY ("reporter_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_devtracker"."dev_items" ADD CONSTRAINT "dev_items_reviewer_id_fkey" FOREIGN KEY ("reviewer_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_devtracker"."release_notes" ADD CONSTRAINT "release_notes_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_feedback"."feedbacks" ADD CONSTRAINT "feedbacks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_feedback"."feedbacks" ADD CONSTRAINT "feedbacks_assignee_id_fkey" FOREIGN KEY ("assignee_id") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_flow_diagram"."flow_diagram" ADD CONSTRAINT "flow_diagram_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_flow_diagram"."flow_diagram" ADD CONSTRAINT "flow_diagram_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_definitions" ADD CONSTRAINT "form_definitions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_definitions" ADD CONSTRAINT "form_definitions_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_definitions" ADD CONSTRAINT "form_definitions_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_versions" ADD CONSTRAINT "form_versions_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_versions" ADD CONSTRAINT "form_versions_reviewed_by_fkey" FOREIGN KEY ("reviewed_by") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_versions" ADD CONSTRAINT "form_versions_definition_id_fkey" FOREIGN KEY ("definition_id") REFERENCES "platform_form"."form_definitions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_form_definition_id_fkey" FOREIGN KEY ("form_definition_id") REFERENCES "platform_form"."form_definitions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_form_version_id_fkey" FOREIGN KEY ("form_version_id") REFERENCES "platform_form"."form_versions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_snapshot_id_fkey" FOREIGN KEY ("snapshot_id") REFERENCES "platform_form"."release_snapshots"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_submitted_by_fkey" FOREIGN KEY ("submitted_by") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_instances" ADD CONSTRAINT "form_instances_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_translations" ADD CONSTRAINT "form_translations_version_id_fkey" FOREIGN KEY ("version_id") REFERENCES "platform_form"."form_versions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_templates" ADD CONSTRAINT "form_templates_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_templates" ADD CONSTRAINT "form_templates_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "platform_iam"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_webhooks" ADD CONSTRAINT "form_webhooks_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_form"."form_webhook_logs" ADD CONSTRAINT "form_webhook_logs_webhook_id_fkey" FOREIGN KEY ("webhook_id") REFERENCES "platform_form"."form_webhooks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."workflow_role_user_rel" ADD CONSTRAINT "workflow_role_user_rel_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."workflow_role_user_rel" ADD CONSTRAINT "workflow_role_user_rel_workflow_role_id_fkey" FOREIGN KEY ("workflow_role_id") REFERENCES "platform_iam"."workflow_roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."user_role_rel" ADD CONSTRAINT "user_role_rel_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "platform_iam"."roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."user_role_rel" ADD CONSTRAINT "user_role_rel_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."user_role_rel" ADD CONSTRAINT "user_role_rel_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."role_permission_rel" ADD CONSTRAINT "role_permission_rel_permission_id_fkey" FOREIGN KEY ("permission_id") REFERENCES "platform_iam"."permissions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."role_permission_rel" ADD CONSTRAINT "role_permission_rel_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "platform_iam"."roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."role_data_scope_rel" ADD CONSTRAINT "role_data_scope_rel_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "platform_iam"."roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."role_data_scope_rel" ADD CONSTRAINT "role_data_scope_rel_data_scope_id_fkey" FOREIGN KEY ("data_scope_id") REFERENCES "platform_iam"."data_scopes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."ai_tool_grants" ADD CONSTRAINT "ai_tool_grants_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "platform_iam"."roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."ai_tool_grants_user" ADD CONSTRAINT "ai_tool_grants_user_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."knowledge_articles" ADD CONSTRAINT "knowledge_articles_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."ai_qa_logs" ADD CONSTRAINT "ai_qa_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_views" ADD CONSTRAINT "document_views_sp_document_id_fkey" FOREIGN KEY ("sp_document_id") REFERENCES "platform_knowledge"."sp_document_index"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_views" ADD CONSTRAINT "document_views_article_id_fkey" FOREIGN KEY ("article_id") REFERENCES "platform_knowledge"."knowledge_articles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_views" ADD CONSTRAINT "document_views_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_likes" ADD CONSTRAINT "document_likes_sp_document_id_fkey" FOREIGN KEY ("sp_document_id") REFERENCES "platform_knowledge"."sp_document_index"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_likes" ADD CONSTRAINT "document_likes_article_id_fkey" FOREIGN KEY ("article_id") REFERENCES "platform_knowledge"."knowledge_articles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_likes" ADD CONSTRAINT "document_likes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_comments" ADD CONSTRAINT "document_comments_sp_document_id_fkey" FOREIGN KEY ("sp_document_id") REFERENCES "platform_knowledge"."sp_document_index"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_comments" ADD CONSTRAINT "document_comments_article_id_fkey" FOREIGN KEY ("article_id") REFERENCES "platform_knowledge"."knowledge_articles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_comments" ADD CONSTRAINT "document_comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."document_comments" ADD CONSTRAINT "document_comments_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "platform_knowledge"."document_comments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."user_mentions" ADD CONSTRAINT "user_mentions_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "platform_knowledge"."document_comments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."user_mentions" ADD CONSTRAINT "user_mentions_mentioned_user_id_fkey" FOREIGN KEY ("mentioned_user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."sync_task_skipped_items" ADD CONSTRAINT "sync_task_skipped_items_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "platform_knowledge"."sync_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."sync_task_processed_items" ADD CONSTRAINT "sync_task_processed_items_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "platform_knowledge"."sync_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_knowledge"."sync_task_failed_items" ADD CONSTRAINT "sync_task_failed_items_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "platform_knowledge"."sync_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meetings" ADD CONSTRAINT "meetings_series_id_fkey" FOREIGN KEY ("series_id") REFERENCES "platform_meeting_attendance"."meeting_series"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meetings" ADD CONSTRAINT "meetings_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_series" ADD CONSTRAINT "meeting_series_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."attendances" ADD CONSTRAINT "attendances_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "platform_meeting_attendance"."meetings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."attendances" ADD CONSTRAINT "attendances_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_required_attendees" ADD CONSTRAINT "meeting_required_attendees_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_required_attendees" ADD CONSTRAINT "meeting_required_attendees_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "platform_meeting_attendance"."meetings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_series_attendee_preferences" ADD CONSTRAINT "meeting_series_attendee_preferences_series_id_fkey" FOREIGN KEY ("series_id") REFERENCES "platform_meeting_attendance"."meeting_series"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_series_attendee_preferences" ADD CONSTRAINT "meeting_series_attendee_preferences_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_external_attendees" ADD CONSTRAINT "meeting_external_attendees_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "platform_meeting_attendance"."meetings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_templates" ADD CONSTRAINT "meeting_templates_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_template_attendees" ADD CONSTRAINT "meeting_template_attendees_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."meeting_template_attendees" ADD CONSTRAINT "meeting_template_attendees_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "platform_meeting_attendance"."meeting_templates"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."leave_records" ADD CONSTRAINT "leave_records_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."audit_logs" ADD CONSTRAINT "audit_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."audit_alerts" ADD CONSTRAINT "audit_alerts_rule_id_fkey" FOREIGN KEY ("rule_id") REFERENCES "platform_meeting_attendance"."audit_alert_rules"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_subscriptions" ADD CONSTRAINT "outlook_subscriptions_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "platform_meeting_attendance"."meetings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_meeting_series_id_fkey" FOREIGN KEY ("meeting_series_id") REFERENCES "platform_meeting_attendance"."meeting_series"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_primary_mailbox_id_fkey" FOREIGN KEY ("primary_mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_series_occurrence_exclusions" ADD CONSTRAINT "outlook_series_occurrence_exclusions_binding_id_fkey" FOREIGN KEY ("binding_id") REFERENCES "platform_meeting_attendance"."outlook_sync_bindings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_event_logs" ADD CONSTRAINT "outlook_sync_event_logs_binding_id_fkey" FOREIGN KEY ("binding_id") REFERENCES "platform_meeting_attendance"."outlook_sync_bindings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_event_logs" ADD CONSTRAINT "outlook_sync_event_logs_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_source_versions" ADD CONSTRAINT "outlook_event_source_versions_binding_id_fkey" FOREIGN KEY ("binding_id") REFERENCES "platform_meeting_attendance"."outlook_sync_bindings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_source_versions" ADD CONSTRAINT "outlook_event_source_versions_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_sync_diffs" ADD CONSTRAINT "outlook_event_sync_diffs_binding_id_fkey" FOREIGN KEY ("binding_id") REFERENCES "platform_meeting_attendance"."outlook_sync_bindings"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_sync_diffs" ADD CONSTRAINT "outlook_event_sync_diffs_source_version_id_fkey" FOREIGN KEY ("source_version_id") REFERENCES "platform_meeting_attendance"."outlook_event_source_versions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_sync_diffs" ADD CONSTRAINT "outlook_event_sync_diffs_previous_source_version_id_fkey" FOREIGN KEY ("previous_source_version_id") REFERENCES "platform_meeting_attendance"."outlook_event_source_versions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_cursors" ADD CONSTRAINT "outlook_sync_cursors_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_event_snapshots" ADD CONSTRAINT "outlook_event_snapshots_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_notify"."notification_messages" ADD CONSTRAINT "notification_messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_notify"."notification_log" ADD CONSTRAINT "notification_log_recipient_id_fkey" FOREIGN KEY ("recipient_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_notify"."notification_log" ADD CONSTRAINT "notification_log_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "platform_notify"."notification_templates"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_ops_center"."m365_user_activity_changes" ADD CONSTRAINT "m365_user_activity_changes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_ops_center"."m365_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."grade_config" ADD CONSTRAINT "grade_config_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."performance_cycle" ADD CONSTRAINT "performance_cycle_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."performance_cycle" ADD CONSTRAINT "performance_cycle_grade_config_id_fkey" FOREIGN KEY ("grade_config_id") REFERENCES "platform_performance"."grade_config"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."performance_cycle" ADD CONSTRAINT "performance_cycle_parent_cycle_id_fkey" FOREIGN KEY ("parent_cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."performance_result" ADD CONSTRAINT "performance_result_cycle_id_fkey" FOREIGN KEY ("cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."performance_result" ADD CONSTRAINT "performance_result_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."kpi_assignment" ADD CONSTRAINT "kpi_assignment_cycle_id_fkey" FOREIGN KEY ("cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."kpi_assignment" ADD CONSTRAINT "kpi_assignment_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "platform_performance"."kpi_assignment"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."kpi_dependency" ADD CONSTRAINT "kpi_dependency_source_assignment_id_fkey" FOREIGN KEY ("source_assignment_id") REFERENCES "platform_performance"."kpi_assignment"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."kpi_dependency" ADD CONSTRAINT "kpi_dependency_target_assignment_id_fkey" FOREIGN KEY ("target_assignment_id") REFERENCES "platform_performance"."kpi_assignment"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."kpi_assessment" ADD CONSTRAINT "kpi_assessment_assignment_id_fkey" FOREIGN KEY ("assignment_id") REFERENCES "platform_performance"."kpi_assignment"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_360" ADD CONSTRAINT "evaluation_360_cycle_id_fkey" FOREIGN KEY ("cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_360" ADD CONSTRAINT "evaluation_360_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "platform_performance"."evaluation_360_template"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_task" ADD CONSTRAINT "evaluation_task_evaluation_id_fkey" FOREIGN KEY ("evaluation_id") REFERENCES "platform_performance"."evaluation_360"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_response" ADD CONSTRAINT "evaluation_response_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "platform_performance"."evaluation_task"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."grade_adjustment_log" ADD CONSTRAINT "grade_adjustment_log_result_id_fkey" FOREIGN KEY ("result_id") REFERENCES "platform_performance"."performance_result"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."strategic_objective" ADD CONSTRAINT "strategic_objective_cycle_id_fkey" FOREIGN KEY ("cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."strategic_objective_assignment" ADD CONSTRAINT "strategic_objective_assignment_objective_id_fkey" FOREIGN KEY ("objective_id") REFERENCES "platform_performance"."strategic_objective"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."strategic_objective_assignment" ADD CONSTRAINT "strategic_objective_assignment_assignee_id_fkey" FOREIGN KEY ("assignee_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_360_template" ADD CONSTRAINT "evaluation_360_template_cycle_id_fkey" FOREIGN KEY ("cycle_id") REFERENCES "platform_performance"."performance_cycle"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_performance"."evaluation_360_template" ADD CONSTRAINT "evaluation_360_template_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."site_checkpoints" ADD CONSTRAINT "site_checkpoints_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."shared_checkin_partners" ADD CONSTRAINT "shared_checkin_partners_checkpoint_id_fkey" FOREIGN KEY ("checkpoint_id") REFERENCES "platform_site_attendance"."site_checkpoints"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."shared_checkin_partners" ADD CONSTRAINT "shared_checkin_partners_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."shared_checkin_partners" ADD CONSTRAINT "shared_checkin_partners_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."site_attendance_events" ADD CONSTRAINT "site_attendance_events_checkpoint_id_fkey" FOREIGN KEY ("checkpoint_id") REFERENCES "platform_site_attendance"."site_checkpoints"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."site_attendance_events" ADD CONSTRAINT "site_attendance_events_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."site_daily_summaries" ADD CONSTRAINT "site_daily_summaries_checkpoint_id_fkey" FOREIGN KEY ("checkpoint_id") REFERENCES "platform_site_attendance"."site_checkpoints"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_site_attendance"."site_daily_summaries" ADD CONSTRAINT "site_daily_summaries_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."tickets" ADD CONSTRAINT "tickets_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "platform_tickets"."ticket_categories"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."tickets" ADD CONSTRAINT "tickets_assignee_group_id_fkey" FOREIGN KEY ("assignee_group_id") REFERENCES "platform_tickets"."assignment_groups"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."tickets" ADD CONSTRAINT "tickets_sla_id_fkey" FOREIGN KEY ("sla_id") REFERENCES "platform_tickets"."ticket_slas"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."tickets" ADD CONSTRAINT "tickets_parent_ticket_id_fkey" FOREIGN KEY ("parent_ticket_id") REFERENCES "platform_tickets"."tickets"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_categories" ADD CONSTRAINT "ticket_categories_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "platform_tickets"."ticket_categories"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_categories" ADD CONSTRAINT "ticket_categories_default_assignee_group_id_fkey" FOREIGN KEY ("default_assignee_group_id") REFERENCES "platform_tickets"."assignment_groups"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_categories" ADD CONSTRAINT "ticket_categories_sla_id_fkey" FOREIGN KEY ("sla_id") REFERENCES "platform_tickets"."ticket_slas"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_comments" ADD CONSTRAINT "ticket_comments_ticket_id_fkey" FOREIGN KEY ("ticket_id") REFERENCES "platform_tickets"."tickets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_attachments" ADD CONSTRAINT "ticket_attachments_ticket_id_fkey" FOREIGN KEY ("ticket_id") REFERENCES "platform_tickets"."tickets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_attachments" ADD CONSTRAINT "ticket_attachments_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "platform_tickets"."ticket_comments"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_tickets"."ticket_activities" ADD CONSTRAINT "ticket_activities_ticket_id_fkey" FOREIGN KEY ("ticket_id") REFERENCES "platform_tickets"."tickets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_skus" ADD CONSTRAINT "robot_skus_model_id_fkey" FOREIGN KEY ("model_id") REFERENCES "robot_manager"."robot_models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_locations" ADD CONSTRAINT "robot_locations_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "robot_manager"."robot_customers"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_model_id_fkey" FOREIGN KEY ("model_id") REFERENCES "robot_manager"."robot_models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_sku_id_fkey" FOREIGN KEY ("sku_id") REFERENCES "robot_manager"."robot_skus"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_supplier_id_fkey" FOREIGN KEY ("supplier_id") REFERENCES "robot_manager"."robot_suppliers"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "robot_manager"."robot_customers"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_location_id_fkey" FOREIGN KEY ("location_id") REFERENCES "robot_manager"."robot_locations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_service_records" ADD CONSTRAINT "robot_service_records_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_status_change_logs" ADD CONSTRAINT "robot_status_change_logs_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_attachments" ADD CONSTRAINT "robot_attachments_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

