-- 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_feedback";

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

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

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

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

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

-- 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', 'COMPLETED', 'TERMINATED', 'FAILED');

-- 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', '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_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"."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_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_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_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');

-- 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,
    "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_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 NOT NULL,
    "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_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_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,
    "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,
    "tenant_id" TEXT,
    "region" TEXT NOT NULL DEFAULT 'CN',
    "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,

    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_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_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_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")
);

-- 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_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 "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 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 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 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 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 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 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 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");

-- 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_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_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 RESTRICT 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_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_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_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_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;
