-- ⚠️ DESTRUCTIVE: v2 → v3 robot_manager 整模块重构
-- ============================================================================
-- 本迁移 DROP 7 张 v2 robot_manager 表（attachments / customers / field_defs /
-- locations / service_records / status_change_logs / suppliers）+ robot_units
-- 表 6 列（created_by / current_status / customer_id / location_id /
-- supplier_id / updated_by）。**无 backfill**：执行后 v2 数据永久丢失。
--
-- platform_master schema 仅改 geo_regions.countries DROP DEFAULT（保留列，
-- 不丢数据）。
--
-- 部署前检查清单（UAT / production）：
--   1. 确认 robot_manager schema 上的 v2 数据已不再需要（业务确认 — 2026-05-18
--      维护者已确认 robot_manager v2 数据可丢）
--   2. 备份 robot_manager schema（标准 db-backup skill）
--   3. 准备 v3 reseed 计划（v3 是 28-stage 状态机 + CQRS Snapshot/Event）
--
-- 本机 dev / 测试库 db:push 重建无影响（属于空库 / 新建场景）。
-- ============================================================================

-- CreateEnum
CREATE TYPE "platform_master"."CustomerType" AS ENUM ('B2B', 'B2C', 'INTERNAL');

-- CreateEnum
CREATE TYPE "platform_master"."SupplierType" AS ENUM ('MANUFACTURER', 'PARTS', 'LOGISTICS', 'SERVICE');

-- CreateEnum
CREATE TYPE "platform_master"."PartnerRole" AS ENUM ('SALES_AGENT', 'MENTOR', 'MENTEE', 'CUSTOMS_BROKER', 'TRAINING_PARTNER');

-- CreateEnum
CREATE TYPE "platform_master"."LocationType" AS ENUM ('WAREHOUSE', 'CUSTOMER_SITE', 'FACTORY', 'BONDED_ZONE', 'IN_TRANSIT', 'SERVICE_CENTER');

-- CreateEnum
CREATE TYPE "robot_manager"."RobotLifecycleEventType" AS ENUM ('stage_changed', 'held', 'unheld', 'location_moved', 'sn_activated', 'usage_type_changed', 'label_applied', 'inspection_logged', 'readiness_completed', 'payment_collected', 'delivery_signed', 'service_opened', 'service_closed', 'imported_from_v5', 'note_added');

-- CreateEnum
CREATE TYPE "robot_manager"."RobotEventRelatedType" AS ENUM ('PO', 'SO', 'DELIVERY_REQUEST', 'DELIVERY_FULFILLMENT', 'SERVICE_TICKET', 'INSPECTION', 'COMPLIANCE_CHECK', 'READINESS', 'LABEL', 'LOGISTICS_LEG', 'PAYMENT', 'RENTAL_AGREEMENT');

-- CreateEnum
CREATE TYPE "robot_manager"."ServiceTicketActivityType" AS ENUM ('COMMENT', 'STATUS_CHANGE', 'HANDOVER', 'RESOLVED');

-- AlterEnum
ALTER TYPE "robot_manager"."PaymentRelatedType" ADD VALUE 'RENTAL_AGREEMENT';

-- AlterEnum: RobotLifecycleStage 旧值移除
-- Prisma 自动生成的 BEGIN..COMMIT 块引用了尚未创建的新表（robot_unit_snapshots / robot_lifecycle_events），
-- 这里手动收敛为只针对当前存在的列：robot_units.current_status。
-- 该列在下文 ALTER TABLE robot_units DROP COLUMN current_status 后会消失，
-- 因此先把 DEFAULT 干掉再做 enum rename。
ALTER TABLE "robot_manager"."robot_units" ALTER COLUMN "current_status" DROP DEFAULT;
ALTER TABLE "robot_manager"."robot_units" ALTER COLUMN "current_status" TYPE TEXT USING "current_status"::text;
-- CASCADE 自动 drop robot_status_change_logs / robot_service_records 等 dependent columns —
-- 这些表 / 列在下文都会被 DROP TABLE / DROP COLUMN 干掉，CASCADE 顺序提前不影响最终一致性。
DROP TYPE "robot_manager"."RobotLifecycleStage" CASCADE;
CREATE TYPE "robot_manager"."RobotLifecycleStage" AS ENUM ('SUPPLY_PO_CREATED', 'SUPPLY_IN_PRODUCTION', 'SUPPLY_READY_TO_SHIP', 'LOGISTICS_IN_TRANSIT', 'LOGISTICS_BONDED', 'LOGISTICS_CUSTOMS_CLEARED', 'WAREHOUSE_RECEIVED', 'WAREHOUSE_AT_W1_PDI', 'WAREHOUSE_MODIFICATION', 'WAREHOUSE_AT_W2', 'WAREHOUSE_AT_W2_RLE', 'WAREHOUSE_BRANDED_READY', 'SALES_RESERVED', 'SALES_PAYMENT_VALIDATED', 'DELIVERY_APPROVAL', 'DELIVERY_PAYMENT_COLLECTED', 'DELIVERY_READY', 'DELIVERY_DELIVERED', 'RENTAL_ACTIVE', 'AFTERSALES_TICKET', 'AFTERSALES_RETURN_INITIATED', 'AFTERSALES_RETURN_RECEIVED', 'AFTERSALES_AT_W6', 'AFTERSALES_UNDER_REPAIR', 'AFTERSALES_QUOTE_APPROVAL', 'AFTERSALES_REPAIRED', 'CLOSED', 'CANCELLED', 'RETURNED');

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_attachments" DROP CONSTRAINT "robot_attachments_robot_unit_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_locations" DROP CONSTRAINT "robot_locations_customer_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_service_records" DROP CONSTRAINT "robot_service_records_robot_unit_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_status_change_logs" DROP CONSTRAINT "robot_status_change_logs_robot_unit_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_units" DROP CONSTRAINT "robot_units_customer_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_units" DROP CONSTRAINT "robot_units_location_id_fkey";

-- DropForeignKey
ALTER TABLE "robot_manager"."robot_units" DROP CONSTRAINT "robot_units_supplier_id_fkey";

-- DropIndex
DROP INDEX "robot_manager"."robot_units_organization_id_current_status_idx";

-- DropIndex
DROP INDEX "robot_manager"."robot_units_organization_id_customer_id_idx";

-- DropIndex
DROP INDEX "robot_manager"."robot_units_organization_id_location_id_idx";

-- DropIndex
DROP INDEX "robot_manager"."robot_units_organization_id_supplier_id_idx";

-- AlterTable

-- AlterTable
ALTER TABLE "platform_master"."geo_regions" ALTER COLUMN "countries" DROP DEFAULT;

-- AlterTable
ALTER TABLE "robot_manager"."robot_skus" ADD COLUMN     "currency_code" VARCHAR(3);

-- AlterTable
ALTER TABLE "robot_manager"."robot_units" DROP COLUMN "created_by",
DROP COLUMN "current_status",
DROP COLUMN "customer_id",
DROP COLUMN "location_id",
DROP COLUMN "supplier_id",
DROP COLUMN "updated_by",
ADD COLUMN     "created_by_id" UUID NOT NULL,
ADD COLUMN     "disposal_notes" TEXT,
ADD COLUMN     "disposal_type" "robot_manager"."RobotDisposalType",
ADD COLUMN     "ffsn_display" TEXT,
ADD COLUMN     "manufacture_date" DATE,
ADD COLUMN     "original_supplier_id" UUID,
ADD COLUMN     "placeholder_sn_orig" TEXT,
ADD COLUMN     "purchase_order_id" UUID,
ADD COLUMN     "purchase_order_line_id" UUID,
ADD COLUMN     "retired_at" TIMESTAMPTZ(3),
ADD COLUMN     "sap_material_no" TEXT,
ADD COLUMN     "supplier_sn" TEXT,
ADD COLUMN     "usage_type" "robot_manager"."RobotUsageType" NOT NULL DEFAULT 'SALES';

-- DropTable
DROP TABLE "robot_manager"."robot_attachments";

-- DropTable
DROP TABLE "robot_manager"."robot_customers";

-- DropTable
DROP TABLE "robot_manager"."robot_field_defs";

-- DropTable
DROP TABLE "robot_manager"."robot_locations";

-- DropTable
DROP TABLE "robot_manager"."robot_service_records";

-- DropTable
DROP TABLE "robot_manager"."robot_status_change_logs";

-- DropTable
DROP TABLE "robot_manager"."robot_suppliers";

-- CreateTable
CREATE TABLE "platform_master"."customers" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "platform_master"."CustomerType" NOT NULL DEFAULT 'B2B',
    "industry" TEXT,
    "country_code" VARCHAR(2),
    "tax_id" TEXT,
    "credit_limit" DECIMAL(14,2),
    "currency_code" VARCHAR(3),
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "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 "customers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "platform_master"."customer_contacts" (
    "id" UUID NOT NULL,
    "customer_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "role" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "is_primary" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_master"."customer_addresses" (
    "id" UUID NOT NULL,
    "customer_id" UUID NOT NULL,
    "address_type" TEXT NOT NULL,
    "line1" TEXT NOT NULL,
    "line2" TEXT,
    "city" TEXT,
    "state" TEXT,
    "postal_code" TEXT,
    "country_code" VARCHAR(2),
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_master"."suppliers" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "platform_master"."SupplierType" NOT NULL DEFAULT 'MANUFACTURER',
    "country_code" VARCHAR(2),
    "currency_code" VARCHAR(3),
    "payment_terms" TEXT,
    "lead_time_days" INTEGER,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "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 "suppliers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "platform_master"."supplier_contacts" (
    "id" UUID NOT NULL,
    "supplier_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "role" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "is_primary" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_master"."partners" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "role" "platform_master"."PartnerRole" NOT NULL,
    "country_code" VARCHAR(2),
    "currency_code" VARCHAR(3),
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "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 "partners_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "platform_master"."partner_contacts" (
    "id" UUID NOT NULL,
    "partner_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "role" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "is_primary" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_master"."locations" (
    "id" UUID NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "platform_master"."LocationType" NOT NULL,
    "country_code" VARCHAR(2),
    "address" TEXT,
    "latitude" DECIMAL(10,6),
    "longitude" DECIMAL(10,6),
    "parent_location_id" UUID,
    "customer_id" UUID,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "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 "locations_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "platform_master"."attachments" (
    "id" UUID NOT NULL,
    "owner_type" TEXT NOT NULL,
    "owner_id" UUID NOT NULL,
    "category" TEXT,
    "filename" TEXT NOT NULL,
    "mime_type" TEXT NOT NULL,
    "size" INTEGER NOT NULL,
    "storage_path" TEXT NOT NULL,
    "organization_id" UUID NOT NULL,
    "uploaded_by_id" UUID NOT NULL,
    "uploaded_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "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 "attachments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."robot_unit_snapshots" (
    "robot_unit_id" UUID NOT NULL,
    "current_stage" "robot_manager"."RobotLifecycleStage" NOT NULL,
    "is_held" BOOLEAN NOT NULL DEFAULT false,
    "hold_reason" TEXT,
    "current_location_id" UUID,
    "current_customer_id" UUID,
    "current_sales_order_id" UUID,
    "current_delivery_request_id" UUID,
    "current_specialist_id" UUID,
    "physical_product_status" "robot_manager"."RobotPhysicalStatus",
    "days_ready_for_delivery" INTEGER,
    "warranty_status" "robot_manager"."RobotWarrantyStatus",
    "last_event_id" UUID,
    "last_event_at" TIMESTAMPTZ(3),
    "derived_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "version" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "robot_unit_snapshots_pkey" PRIMARY KEY ("robot_unit_id")
);

-- CreateTable
CREATE TABLE "robot_manager"."robot_lifecycle_events" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "event_type" "robot_manager"."RobotLifecycleEventType" NOT NULL,
    "from_stage" "robot_manager"."RobotLifecycleStage",
    "to_stage" "robot_manager"."RobotLifecycleStage",
    "from_location_id" UUID,
    "to_location_id" UUID,
    "actor_user_id" UUID,
    "partner_id" UUID,
    "customer_id" UUID,
    "related_type" "robot_manager"."RobotEventRelatedType",
    "related_id" UUID,
    "payload" JSONB NOT NULL DEFAULT '{}',
    "notes" TEXT,
    "occurred_at" TIMESTAMPTZ(3) NOT NULL,
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by_id" UUID NOT NULL,
    "deleted_at" TIMESTAMPTZ(3),

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

-- CreateTable
CREATE TABLE "robot_manager"."purchase_orders" (
    "id" UUID NOT NULL,
    "po_no" TEXT NOT NULL,
    "sap_po_no" TEXT,
    "supplier_id" UUID NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "total_amount" DECIMAL(14,2) NOT NULL DEFAULT 0,
    "status" "robot_manager"."PurchaseOrderStatus" NOT NULL DEFAULT 'DRAFT',
    "ordered_at" TIMESTAMPTZ(3) NOT NULL,
    "expected_at" TIMESTAMPTZ(3),
    "closed_at" TIMESTAMPTZ(3),
    "notes" TEXT,
    "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 "purchase_orders_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."purchase_order_lines" (
    "id" UUID NOT NULL,
    "purchase_order_id" UUID NOT NULL,
    "line_no" INTEGER NOT NULL,
    "sku_id" UUID NOT NULL,
    "quantity" INTEGER NOT NULL,
    "unit_price" DECIMAL(12,2) NOT NULL,
    "total_price" DECIMAL(14,2) NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "default_usage_type" "robot_manager"."RobotUsageType",
    "placeholder_pattern" TEXT,
    "expected_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."sales_orders" (
    "id" UUID NOT NULL,
    "so_no" TEXT NOT NULL,
    "d365_so_id" TEXT,
    "customer_id" UUID NOT NULL,
    "sales_person_id" UUID,
    "mentor_id" UUID,
    "mentee_id" UUID,
    "currency_code" VARCHAR(3) NOT NULL,
    "total_amount" DECIMAL(14,2) NOT NULL DEFAULT 0,
    "contract_status" "robot_manager"."SalesContractStatus" NOT NULL DEFAULT 'DRAFT',
    "status" "robot_manager"."SalesOrderStatus" NOT NULL DEFAULT 'RESERVED',
    "signed_at" TIMESTAMPTZ(3),
    "closed_at" TIMESTAMPTZ(3),
    "notes" TEXT,
    "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 "sales_orders_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."sales_order_lines" (
    "id" UUID NOT NULL,
    "sales_order_id" UUID NOT NULL,
    "line_no" INTEGER NOT NULL,
    "robot_unit_id" UUID,
    "line_type" "robot_manager"."SalesLineType" NOT NULL,
    "unit_price" DECIMAL(12,2) NOT NULL,
    "discount" DECIMAL(12,2),
    "net_amount" DECIMAL(14,2) NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "expected_delivery_date" DATE,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."delivery_requests" (
    "id" UUID NOT NULL,
    "delivery_no" TEXT NOT NULL,
    "sales_order_id" UUID NOT NULL,
    "customer_id" UUID NOT NULL,
    "request_type" "robot_manager"."DeliveryRequestType" NOT NULL,
    "expected_date" DATE NOT NULL,
    "status" "robot_manager"."DeliveryRequestStatus" NOT NULL DEFAULT 'REQUESTED',
    "notes" TEXT,
    "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 "delivery_requests_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."delivery_fulfillments" (
    "id" UUID NOT NULL,
    "delivery_request_id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "sap_pgi_doc_no" TEXT,
    "delivered_at" TIMESTAMPTZ(3) NOT NULL,
    "signed_at" TIMESTAMPTZ(3),
    "signed_form_status" "robot_manager"."DeliveryFormStatus",
    "acceptance_form_status" "robot_manager"."DeliveryFormStatus",
    "acceptance_form_pre_del" "robot_manager"."DeliveryFormStatus",
    "acceptance_form_attachment_id" UUID,
    "note_pre_del" TEXT,
    "additional_shipping_notes" TEXT,
    "cost" DECIMAL(14,2),
    "gross_margin" DECIMAL(14,2),
    "revenue_recognition" "robot_manager"."RevenueRecognitionStatus",
    "invoice_status" "robot_manager"."InvoiceStatus",
    "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 "delivery_fulfillments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."payment_records" (
    "id" UUID NOT NULL,
    "payment_no" TEXT NOT NULL,
    "related_type" "robot_manager"."PaymentRelatedType" NOT NULL,
    "related_id" UUID NOT NULL,
    "robot_unit_id" UUID,
    "direction" "robot_manager"."PaymentDirection" NOT NULL,
    "sap_clearing_doc_no" TEXT,
    "payment_method" "robot_manager"."PaymentMethod",
    "payment_status" "robot_manager"."PaymentStatus",
    "pre_del_contract_status" "robot_manager"."PreDelContractStatus",
    "amount" DECIMAL(14,2) NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "paid_at" TIMESTAMPTZ(3),
    "deposit_transfer_form_attachment_id" UUID,
    "deposit_transfer_contract_attachment_id" UUID,
    "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 "payment_records_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."quality_label_records" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "label_type_code" TEXT NOT NULL,
    "status" "robot_manager"."QualityLabelStatus" NOT NULL DEFAULT 'PENDING',
    "applied_at" TIMESTAMPTZ(3),
    "verified_at" TIMESTAMPTZ(3),
    "applied_by_id" UUID,
    "verified_by_id" UUID,
    "photo_attachment_id" UUID,
    "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,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_package_readiness" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "specialist_id" UUID,
    "physical_product_status" "robot_manager"."RobotPhysicalStatus",
    "ready_at" TIMESTAMPTZ(3),
    "has_robot" BOOLEAN NOT NULL DEFAULT false,
    "has_battery" BOOLEAN NOT NULL DEFAULT false,
    "has_remote" BOOLEAN NOT NULL DEFAULT false,
    "has_usa_power_cable" BOOLEAN NOT NULL DEFAULT false,
    "has_cn_cable_with_adapter" BOOLEAN NOT NULL DEFAULT false,
    "has_power_supply" BOOLEAN NOT NULL DEFAULT false,
    "has_charging_dock" BOOLEAN NOT NULL DEFAULT false,
    "has_foot_pads" BOOLEAN NOT NULL DEFAULT false,
    "has_tools_kit" BOOLEAN NOT NULL DEFAULT false,
    "has_paperwork" BOOLEAN NOT NULL DEFAULT false,
    "completed_at" TIMESTAMPTZ(3),
    "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,

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

-- CreateTable
CREATE TABLE "robot_manager"."inspection_records" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "inspection_no" INTEGER NOT NULL,
    "inspected_at" TIMESTAMPTZ(3) NOT NULL,
    "inspector_id" UUID,
    "issue" TEXT,
    "issue_tag_code" TEXT,
    "resolved_at" TIMESTAMPTZ(3),
    "resolved_by_id" UUID,
    "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,

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

-- CreateTable
CREATE TABLE "robot_manager"."logistics_legs" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "leg_no" INTEGER NOT NULL,
    "from_location_id" UUID,
    "to_location_id" UUID,
    "departed_at" TIMESTAMPTZ(3),
    "arrived_at" TIMESTAMPTZ(3),
    "logistics_status" "robot_manager"."LogisticsStatus" NOT NULL,
    "import_declaration_type_code" TEXT,
    "tariff_type_code" TEXT,
    "shipping_receipt_attachment_id" UUID,
    "additional_notes" TEXT,
    "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,

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

-- CreateTable
CREATE TABLE "robot_manager"."compliance_checks" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "date_ready" DATE,
    "sticker_status" "robot_manager"."StickerStatus",
    "fcc_status" "robot_manager"."FccStatus",
    "compliance_notes" TEXT,
    "last_updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "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,

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

-- CreateTable
CREATE TABLE "robot_manager"."rental_agreements" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "customer_id" UUID NOT NULL,
    "start_at" TIMESTAMPTZ(3) NOT NULL,
    "end_at" TIMESTAMPTZ(3) NOT NULL,
    "period_months" INTEGER NOT NULL,
    "monthly_rate" DECIMAL(12,2) NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "status" "robot_manager"."RentalAgreementStatus" NOT NULL DEFAULT 'ACTIVE',
    "contract_attachment_id" UUID,
    "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 "rental_agreements_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."rental_payment_schedules" (
    "id" UUID NOT NULL,
    "rental_agreement_id" UUID NOT NULL,
    "due_date" DATE NOT NULL,
    "amount" DECIMAL(12,2) NOT NULL,
    "currency_code" VARCHAR(3) NOT NULL,
    "paid_payment_record_id" UUID,
    "paid_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "robot_manager"."service_tickets" (
    "id" UUID NOT NULL,
    "ticket_no" TEXT NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "customer_id" UUID,
    "issue_type_code" TEXT NOT NULL,
    "severity" "robot_manager"."TicketSeverity" NOT NULL DEFAULT 'P2',
    "status" "robot_manager"."ServiceTicketStatus" NOT NULL DEFAULT 'NEW',
    "service_record" TEXT,
    "customer_feedback" TEXT,
    "opened_at" TIMESTAMPTZ(3) NOT NULL,
    "opened_by_id" UUID NOT NULL,
    "closed_at" TIMESTAMPTZ(3),
    "resolved_by_id" UUID,
    "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 "service_tickets_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "robot_manager"."service_ticket_activities" (
    "id" UUID NOT NULL,
    "ticket_id" UUID NOT NULL,
    "activity_type" "robot_manager"."ServiceTicketActivityType" NOT NULL,
    "payload" JSONB NOT NULL DEFAULT '{}',
    "actor_id" UUID NOT NULL,
    "occurred_at" TIMESTAMPTZ(3) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "robot_manager"."robot_import_audits" (
    "id" UUID NOT NULL,
    "robot_unit_id" UUID NOT NULL,
    "sources" TEXT[],
    "conflict_count" INTEGER NOT NULL DEFAULT 0,
    "record_status" "robot_manager"."ImportRecordStatus" NOT NULL,
    "conflict_detail" JSONB,
    "imported_at" TIMESTAMPTZ(3) NOT NULL,
    "import_batch" TEXT,
    "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 "robot_import_audits_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "customers_code_key" ON "platform_master"."customers"("code");

-- CreateIndex
CREATE INDEX "customers_organization_id_deleted_at_idx" ON "platform_master"."customers"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "customers_organization_id_enabled_idx" ON "platform_master"."customers"("organization_id", "enabled");

-- CreateIndex
CREATE INDEX "customers_type_idx" ON "platform_master"."customers"("type");

-- CreateIndex
CREATE INDEX "customer_contacts_customer_id_idx" ON "platform_master"."customer_contacts"("customer_id");

-- CreateIndex
CREATE INDEX "customer_addresses_customer_id_idx" ON "platform_master"."customer_addresses"("customer_id");

-- CreateIndex
CREATE UNIQUE INDEX "suppliers_code_key" ON "platform_master"."suppliers"("code");

-- CreateIndex
CREATE INDEX "suppliers_organization_id_deleted_at_idx" ON "platform_master"."suppliers"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "suppliers_organization_id_enabled_idx" ON "platform_master"."suppliers"("organization_id", "enabled");

-- CreateIndex
CREATE INDEX "suppliers_type_idx" ON "platform_master"."suppliers"("type");

-- CreateIndex
CREATE INDEX "supplier_contacts_supplier_id_idx" ON "platform_master"."supplier_contacts"("supplier_id");

-- CreateIndex
CREATE UNIQUE INDEX "partners_code_key" ON "platform_master"."partners"("code");

-- CreateIndex
CREATE INDEX "partners_organization_id_deleted_at_idx" ON "platform_master"."partners"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "partners_role_idx" ON "platform_master"."partners"("role");

-- CreateIndex
CREATE INDEX "partner_contacts_partner_id_idx" ON "platform_master"."partner_contacts"("partner_id");

-- CreateIndex
CREATE UNIQUE INDEX "locations_code_key" ON "platform_master"."locations"("code");

-- CreateIndex
CREATE INDEX "locations_organization_id_deleted_at_idx" ON "platform_master"."locations"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "locations_type_idx" ON "platform_master"."locations"("type");

-- CreateIndex
CREATE INDEX "locations_customer_id_idx" ON "platform_master"."locations"("customer_id");

-- CreateIndex
CREATE INDEX "attachments_owner_type_owner_id_idx" ON "platform_master"."attachments"("owner_type", "owner_id");

-- CreateIndex
CREATE INDEX "attachments_organization_id_deleted_at_idx" ON "platform_master"."attachments"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "attachments_category_idx" ON "platform_master"."attachments"("category");

-- CreateIndex
CREATE INDEX "robot_unit_snapshots_current_stage_idx" ON "robot_manager"."robot_unit_snapshots"("current_stage");

-- CreateIndex
CREATE INDEX "robot_unit_snapshots_current_location_id_idx" ON "robot_manager"."robot_unit_snapshots"("current_location_id");

-- CreateIndex
CREATE INDEX "robot_unit_snapshots_current_customer_id_idx" ON "robot_manager"."robot_unit_snapshots"("current_customer_id");

-- CreateIndex
CREATE INDEX "robot_unit_snapshots_current_sales_order_id_idx" ON "robot_manager"."robot_unit_snapshots"("current_sales_order_id");

-- CreateIndex
CREATE INDEX "robot_lifecycle_events_robot_unit_id_occurred_at_idx" ON "robot_manager"."robot_lifecycle_events"("robot_unit_id", "occurred_at" DESC);

-- CreateIndex
CREATE INDEX "robot_lifecycle_events_event_type_occurred_at_idx" ON "robot_manager"."robot_lifecycle_events"("event_type", "occurred_at");

-- CreateIndex
CREATE INDEX "robot_lifecycle_events_related_type_related_id_idx" ON "robot_manager"."robot_lifecycle_events"("related_type", "related_id");

-- CreateIndex
CREATE INDEX "robot_lifecycle_events_organization_id_occurred_at_idx" ON "robot_manager"."robot_lifecycle_events"("organization_id", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_orders_po_no_key" ON "robot_manager"."purchase_orders"("po_no");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_orders_sap_po_no_key" ON "robot_manager"."purchase_orders"("sap_po_no");

-- CreateIndex
CREATE INDEX "purchase_orders_organization_id_deleted_at_idx" ON "robot_manager"."purchase_orders"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "purchase_orders_organization_id_status_idx" ON "robot_manager"."purchase_orders"("organization_id", "status");

-- CreateIndex
CREATE INDEX "purchase_orders_supplier_id_idx" ON "robot_manager"."purchase_orders"("supplier_id");

-- CreateIndex
CREATE INDEX "purchase_order_lines_sku_id_idx" ON "robot_manager"."purchase_order_lines"("sku_id");

-- CreateIndex
CREATE UNIQUE INDEX "purchase_order_lines_purchase_order_id_line_no_key" ON "robot_manager"."purchase_order_lines"("purchase_order_id", "line_no");

-- CreateIndex
CREATE UNIQUE INDEX "sales_orders_so_no_key" ON "robot_manager"."sales_orders"("so_no");

-- CreateIndex
CREATE UNIQUE INDEX "sales_orders_d365_so_id_key" ON "robot_manager"."sales_orders"("d365_so_id");

-- CreateIndex
CREATE INDEX "sales_orders_organization_id_deleted_at_idx" ON "robot_manager"."sales_orders"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "sales_orders_organization_id_status_idx" ON "robot_manager"."sales_orders"("organization_id", "status");

-- CreateIndex
CREATE INDEX "sales_orders_customer_id_idx" ON "robot_manager"."sales_orders"("customer_id");

-- CreateIndex
CREATE INDEX "sales_order_lines_robot_unit_id_idx" ON "robot_manager"."sales_order_lines"("robot_unit_id");

-- CreateIndex
CREATE UNIQUE INDEX "sales_order_lines_sales_order_id_line_no_key" ON "robot_manager"."sales_order_lines"("sales_order_id", "line_no");

-- CreateIndex
CREATE UNIQUE INDEX "delivery_requests_delivery_no_key" ON "robot_manager"."delivery_requests"("delivery_no");

-- CreateIndex
CREATE INDEX "delivery_requests_organization_id_deleted_at_idx" ON "robot_manager"."delivery_requests"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "delivery_requests_organization_id_status_idx" ON "robot_manager"."delivery_requests"("organization_id", "status");

-- CreateIndex
CREATE INDEX "delivery_requests_customer_id_idx" ON "robot_manager"."delivery_requests"("customer_id");

-- CreateIndex
CREATE INDEX "delivery_fulfillments_organization_id_deleted_at_idx" ON "robot_manager"."delivery_fulfillments"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "delivery_fulfillments_delivery_request_id_idx" ON "robot_manager"."delivery_fulfillments"("delivery_request_id");

-- CreateIndex
CREATE INDEX "delivery_fulfillments_robot_unit_id_idx" ON "robot_manager"."delivery_fulfillments"("robot_unit_id");

-- CreateIndex
CREATE UNIQUE INDEX "payment_records_payment_no_key" ON "robot_manager"."payment_records"("payment_no");

-- CreateIndex
CREATE INDEX "payment_records_organization_id_deleted_at_idx" ON "robot_manager"."payment_records"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "payment_records_related_type_related_id_idx" ON "robot_manager"."payment_records"("related_type", "related_id");

-- CreateIndex
CREATE INDEX "payment_records_robot_unit_id_idx" ON "robot_manager"."payment_records"("robot_unit_id");

-- CreateIndex
CREATE INDEX "payment_records_payment_status_idx" ON "robot_manager"."payment_records"("payment_status");

-- CreateIndex
CREATE INDEX "quality_label_records_organization_id_idx" ON "robot_manager"."quality_label_records"("organization_id");

-- CreateIndex
CREATE INDEX "quality_label_records_status_idx" ON "robot_manager"."quality_label_records"("status");

-- CreateIndex
CREATE UNIQUE INDEX "quality_label_records_robot_unit_id_label_type_code_key" ON "robot_manager"."quality_label_records"("robot_unit_id", "label_type_code");

-- CreateIndex
CREATE UNIQUE INDEX "robot_package_readiness_robot_unit_id_key" ON "robot_manager"."robot_package_readiness"("robot_unit_id");

-- CreateIndex
CREATE INDEX "robot_package_readiness_organization_id_idx" ON "robot_manager"."robot_package_readiness"("organization_id");

-- CreateIndex
CREATE INDEX "inspection_records_organization_id_idx" ON "robot_manager"."inspection_records"("organization_id");

-- CreateIndex
CREATE UNIQUE INDEX "inspection_records_robot_unit_id_inspection_no_key" ON "robot_manager"."inspection_records"("robot_unit_id", "inspection_no");

-- CreateIndex
CREATE INDEX "logistics_legs_organization_id_idx" ON "robot_manager"."logistics_legs"("organization_id");

-- CreateIndex
CREATE UNIQUE INDEX "logistics_legs_robot_unit_id_leg_no_key" ON "robot_manager"."logistics_legs"("robot_unit_id", "leg_no");

-- CreateIndex
CREATE UNIQUE INDEX "compliance_checks_robot_unit_id_key" ON "robot_manager"."compliance_checks"("robot_unit_id");

-- CreateIndex
CREATE INDEX "compliance_checks_organization_id_idx" ON "robot_manager"."compliance_checks"("organization_id");

-- CreateIndex
CREATE INDEX "rental_agreements_organization_id_deleted_at_idx" ON "robot_manager"."rental_agreements"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "rental_agreements_robot_unit_id_idx" ON "robot_manager"."rental_agreements"("robot_unit_id");

-- CreateIndex
CREATE INDEX "rental_agreements_customer_id_idx" ON "robot_manager"."rental_agreements"("customer_id");

-- CreateIndex
CREATE INDEX "rental_agreements_status_idx" ON "robot_manager"."rental_agreements"("status");

-- CreateIndex
CREATE INDEX "rental_payment_schedules_rental_agreement_id_due_date_idx" ON "robot_manager"."rental_payment_schedules"("rental_agreement_id", "due_date");

-- CreateIndex
CREATE UNIQUE INDEX "service_tickets_ticket_no_key" ON "robot_manager"."service_tickets"("ticket_no");

-- CreateIndex
CREATE INDEX "service_tickets_organization_id_deleted_at_idx" ON "robot_manager"."service_tickets"("organization_id", "deleted_at");

-- CreateIndex
CREATE INDEX "service_tickets_status_idx" ON "robot_manager"."service_tickets"("status");

-- CreateIndex
CREATE INDEX "service_tickets_robot_unit_id_idx" ON "robot_manager"."service_tickets"("robot_unit_id");

-- CreateIndex
CREATE INDEX "service_tickets_severity_idx" ON "robot_manager"."service_tickets"("severity");

-- CreateIndex
CREATE INDEX "service_ticket_activities_ticket_id_occurred_at_idx" ON "robot_manager"."service_ticket_activities"("ticket_id", "occurred_at");

-- CreateIndex
CREATE UNIQUE INDEX "robot_import_audits_robot_unit_id_key" ON "robot_manager"."robot_import_audits"("robot_unit_id");

-- CreateIndex
CREATE INDEX "robot_import_audits_organization_id_idx" ON "robot_manager"."robot_import_audits"("organization_id");

-- CreateIndex
CREATE INDEX "robot_import_audits_record_status_idx" ON "robot_manager"."robot_import_audits"("record_status");

-- CreateIndex
CREATE INDEX "robot_import_audits_import_batch_idx" ON "robot_manager"."robot_import_audits"("import_batch");

-- CreateIndex
CREATE INDEX "robot_units_organization_id_purchase_order_id_idx" ON "robot_manager"."robot_units"("organization_id", "purchase_order_id");

-- CreateIndex
CREATE UNIQUE INDEX "robot_units_organization_id_ffsn_key" ON "robot_manager"."robot_units"("organization_id", "ffsn");

-- AddForeignKey
ALTER TABLE "platform_master"."customer_contacts" ADD CONSTRAINT "customer_contacts_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "platform_master"."customers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_master"."customer_addresses" ADD CONSTRAINT "customer_addresses_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "platform_master"."customers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_master"."supplier_contacts" ADD CONSTRAINT "supplier_contacts_supplier_id_fkey" FOREIGN KEY ("supplier_id") REFERENCES "platform_master"."suppliers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_master"."partner_contacts" ADD CONSTRAINT "partner_contacts_partner_id_fkey" FOREIGN KEY ("partner_id") REFERENCES "platform_master"."partners"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_master"."locations" ADD CONSTRAINT "locations_parent_location_id_fkey" FOREIGN KEY ("parent_location_id") REFERENCES "platform_master"."locations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_purchase_order_id_fkey" FOREIGN KEY ("purchase_order_id") REFERENCES "robot_manager"."purchase_orders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_units" ADD CONSTRAINT "robot_units_purchase_order_line_id_fkey" FOREIGN KEY ("purchase_order_line_id") REFERENCES "robot_manager"."purchase_order_lines"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_unit_snapshots" ADD CONSTRAINT "robot_unit_snapshots_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_unit_snapshots" ADD CONSTRAINT "robot_unit_snapshots_last_event_id_fkey" FOREIGN KEY ("last_event_id") REFERENCES "robot_manager"."robot_lifecycle_events"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_unit_snapshots" ADD CONSTRAINT "robot_unit_snapshots_current_sales_order_id_fkey" FOREIGN KEY ("current_sales_order_id") REFERENCES "robot_manager"."sales_orders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_unit_snapshots" ADD CONSTRAINT "robot_unit_snapshots_current_delivery_request_id_fkey" FOREIGN KEY ("current_delivery_request_id") REFERENCES "robot_manager"."delivery_requests"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_lifecycle_events" ADD CONSTRAINT "robot_lifecycle_events_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."purchase_order_lines" ADD CONSTRAINT "purchase_order_lines_purchase_order_id_fkey" FOREIGN KEY ("purchase_order_id") REFERENCES "robot_manager"."purchase_orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."sales_order_lines" ADD CONSTRAINT "sales_order_lines_sales_order_id_fkey" FOREIGN KEY ("sales_order_id") REFERENCES "robot_manager"."sales_orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."sales_order_lines" ADD CONSTRAINT "sales_order_lines_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."delivery_requests" ADD CONSTRAINT "delivery_requests_sales_order_id_fkey" FOREIGN KEY ("sales_order_id") REFERENCES "robot_manager"."sales_orders"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."delivery_fulfillments" ADD CONSTRAINT "delivery_fulfillments_delivery_request_id_fkey" FOREIGN KEY ("delivery_request_id") REFERENCES "robot_manager"."delivery_requests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."delivery_fulfillments" ADD CONSTRAINT "delivery_fulfillments_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."payment_records" ADD CONSTRAINT "payment_records_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."quality_label_records" ADD CONSTRAINT "quality_label_records_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_package_readiness" ADD CONSTRAINT "robot_package_readiness_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."inspection_records" ADD CONSTRAINT "inspection_records_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."logistics_legs" ADD CONSTRAINT "logistics_legs_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."compliance_checks" ADD CONSTRAINT "compliance_checks_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."rental_agreements" ADD CONSTRAINT "rental_agreements_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."rental_payment_schedules" ADD CONSTRAINT "rental_payment_schedules_rental_agreement_id_fkey" FOREIGN KEY ("rental_agreement_id") REFERENCES "robot_manager"."rental_agreements"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."rental_payment_schedules" ADD CONSTRAINT "rental_payment_schedules_paid_payment_record_id_fkey" FOREIGN KEY ("paid_payment_record_id") REFERENCES "robot_manager"."payment_records"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."service_tickets" ADD CONSTRAINT "service_tickets_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."service_ticket_activities" ADD CONSTRAINT "service_ticket_activities_ticket_id_fkey" FOREIGN KEY ("ticket_id") REFERENCES "robot_manager"."service_tickets"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "robot_manager"."robot_import_audits" ADD CONSTRAINT "robot_import_audits_robot_unit_id_fkey" FOREIGN KEY ("robot_unit_id") REFERENCES "robot_manager"."robot_units"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- RenameIndex

-- RenameIndex

-- RenameIndex

-- RenameIndex

-- RenameIndex

