-- M1 PR-A · Import 工具核心 schema（详见 docs/modules/robot-manager/14-import-export-tool-prd.md §5）
-- 新增 ImportBatch + ImportBatchEntry 表 + 3 enum；RobotImportAudit 保留给 v5 历史只读
-- 跨 schema FK：createdById / confirmedById → platform_iam.users.id（仅存 UUID，无 @relation）

-- CreateEnum
CREATE TYPE "robot_manager"."ImportBatchType" AS ENUM ('PURCHASE_ORDER', 'ROBOT_UNIT', 'MASTER_MODEL', 'MASTER_SKU', 'MASTER_CUSTOMER', 'MASTER_SUPPLIER', 'MASTER_LOCATION', 'SERVICE_TICKET');

-- CreateEnum
CREATE TYPE "robot_manager"."ImportBatchStatus" AS ENUM ('PENDING', 'VALIDATING', 'VALIDATED', 'IMPORTING', 'COMPLETED', 'FAILED', 'SUPERSEDED');

-- CreateEnum
CREATE TYPE "robot_manager"."ImportEntryStatus" AS ENUM ('OK', 'ERROR', 'WARNING');

-- CreateTable
CREATE TABLE "robot_manager"."import_batches" (
    "id" UUID NOT NULL,
    "type" "robot_manager"."ImportBatchType" NOT NULL,
    "status" "robot_manager"."ImportBatchStatus" NOT NULL DEFAULT 'PENDING',
    "fileName" VARCHAR(255) NOT NULL,
    "file_hash" VARCHAR(64) NOT NULL,
    "template_schema_hash" VARCHAR(16) NOT NULL,
    "total_rows" INTEGER NOT NULL DEFAULT 0,
    "success_rows" INTEGER NOT NULL DEFAULT 0,
    "error_rows" INTEGER NOT NULL DEFAULT 0,
    "warning_rows" INTEGER NOT NULL DEFAULT 0,
    "started_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "confirmed_by_id" UUID,
    "confirmed_at" TIMESTAMPTZ(3),
    "client_ip" VARCHAR(45),
    "user_agent" VARCHAR(512),
    "error_summary" JSONB,
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by_id" UUID NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "robot_manager"."import_batch_entries" (
    "id" UUID NOT NULL,
    "batch_id" UUID NOT NULL,
    "row_no" INTEGER NOT NULL,
    "status" "robot_manager"."ImportEntryStatus" NOT NULL,
    "entity_ids" UUID[],
    "payload" JSONB NOT NULL,
    "payload_hash" VARCHAR(64) NOT NULL,
    "error_detail" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateIndex
CREATE INDEX "import_batches_organization_id_deleted_at_idx" ON "robot_manager"."import_batches"("organization_id", "deleted_at");
CREATE INDEX "import_batches_organization_id_type_created_at_idx" ON "robot_manager"."import_batches"("organization_id", "type", "created_at" DESC);
CREATE INDEX "import_batches_organization_id_created_by_id_created_at_idx" ON "robot_manager"."import_batches"("organization_id", "created_by_id", "created_at" DESC);
CREATE INDEX "import_batches_status_idx" ON "robot_manager"."import_batches"("status");

-- CreateIndex
CREATE INDEX "import_batch_entries_batch_id_row_no_idx" ON "robot_manager"."import_batch_entries"("batch_id", "row_no");
CREATE INDEX "import_batch_entries_batch_id_status_idx" ON "robot_manager"."import_batch_entries"("batch_id", "status");

-- AddForeignKey
ALTER TABLE "robot_manager"."import_batch_entries" ADD CONSTRAINT "import_batch_entries_batch_id_fkey" FOREIGN KEY ("batch_id") REFERENCES "robot_manager"."import_batches"("id") ON DELETE CASCADE ON UPDATE CASCADE;
