-- CreateTable
CREATE TABLE "monitored_addresses" (
    "address_id" BIGSERIAL NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "address" VARCHAR(42) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "monitored_addresses_pkey" PRIMARY KEY ("address_id")
);

-- CreateTable
CREATE TABLE "deposit_transactions" (
    "deposit_id" BIGSERIAL NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "tx_hash" VARCHAR(66) NOT NULL,
    "from_address" VARCHAR(42) NOT NULL,
    "to_address" VARCHAR(42) NOT NULL,
    "token_contract" VARCHAR(42) NOT NULL,
    "amount" DECIMAL(36,18) NOT NULL,
    "amount_formatted" DECIMAL(20,8) NOT NULL,
    "block_number" BIGINT NOT NULL,
    "block_timestamp" TIMESTAMP(3) NOT NULL,
    "log_index" INTEGER NOT NULL,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'DETECTED',
    "address_id" BIGINT NOT NULL,
    "user_id" BIGINT NOT NULL,
    "notified_at" TIMESTAMP(3),
    "notify_attempts" INTEGER NOT NULL DEFAULT 0,
    "last_notify_error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "deposit_transactions_pkey" PRIMARY KEY ("deposit_id")
);

-- CreateTable
CREATE TABLE "block_checkpoints" (
    "checkpoint_id" BIGSERIAL NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "last_scanned_block" BIGINT NOT NULL,
    "last_scanned_at" TIMESTAMP(3) NOT NULL,
    "is_healthy" BOOLEAN NOT NULL DEFAULT true,
    "last_error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "block_checkpoints_pkey" PRIMARY KEY ("checkpoint_id")
);

-- CreateTable
CREATE TABLE "transaction_requests" (
    "request_id" BIGSERIAL NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "source_service" VARCHAR(50) NOT NULL,
    "source_order_id" VARCHAR(100) NOT NULL,
    "from_address" VARCHAR(42) NOT NULL,
    "to_address" VARCHAR(42) NOT NULL,
    "value" DECIMAL(36,18) NOT NULL,
    "data" TEXT,
    "signed_tx" TEXT,
    "tx_hash" VARCHAR(66),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "gas_limit" BIGINT,
    "gas_price" DECIMAL(36,18),
    "nonce" INTEGER,
    "error_message" TEXT,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "transaction_requests_pkey" PRIMARY KEY ("request_id")
);

-- CreateTable
CREATE TABLE "blockchain_events" (
    "event_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "event_data" JSONB NOT NULL,
    "chain_type" VARCHAR(20),
    "tx_hash" VARCHAR(66),
    "occurred_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "blockchain_events_pkey" PRIMARY KEY ("event_id")
);

-- CreateIndex
CREATE INDEX "idx_user" ON "monitored_addresses"("user_id");

-- CreateIndex
CREATE INDEX "idx_chain_active" ON "monitored_addresses"("chain_type", "is_active");

-- CreateIndex
CREATE UNIQUE INDEX "monitored_addresses_chain_type_address_key" ON "monitored_addresses"("chain_type", "address");

-- CreateIndex
CREATE UNIQUE INDEX "deposit_transactions_tx_hash_key" ON "deposit_transactions"("tx_hash");

-- CreateIndex
CREATE INDEX "idx_chain_status" ON "deposit_transactions"("chain_type", "status");

-- CreateIndex
CREATE INDEX "idx_deposit_user" ON "deposit_transactions"("user_id");

-- CreateIndex
CREATE INDEX "idx_block" ON "deposit_transactions"("block_number");

-- CreateIndex
CREATE INDEX "idx_pending_notify" ON "deposit_transactions"("status", "notified_at");

-- CreateIndex
CREATE UNIQUE INDEX "block_checkpoints_chain_type_key" ON "block_checkpoints"("chain_type");

-- CreateIndex
CREATE INDEX "idx_tx_chain_status" ON "transaction_requests"("chain_type", "status");

-- CreateIndex
CREATE INDEX "idx_tx_hash" ON "transaction_requests"("tx_hash");

-- CreateIndex
CREATE UNIQUE INDEX "transaction_requests_source_service_source_order_id_key" ON "transaction_requests"("source_service", "source_order_id");

-- CreateIndex
CREATE INDEX "idx_event_aggregate" ON "blockchain_events"("aggregate_type", "aggregate_id");

-- CreateIndex
CREATE INDEX "idx_event_type" ON "blockchain_events"("event_type");

-- CreateIndex
CREATE INDEX "idx_event_chain" ON "blockchain_events"("chain_type");

-- CreateIndex
CREATE INDEX "idx_event_occurred" ON "blockchain_events"("occurred_at");

-- AddForeignKey
ALTER TABLE "deposit_transactions" ADD CONSTRAINT "deposit_transactions_address_id_fkey" FOREIGN KEY ("address_id") REFERENCES "monitored_addresses"("address_id") ON DELETE RESTRICT ON UPDATE CASCADE;
