<?php

use yii\db\Migration;

/**
 * Creates the virtualstock_webhook_events table for inbound webhook logging.
 *
 * Stores a record for each received webhook push notification so events can
 * be audited, replayed, or processed idempotently.
 */
class m260503_000003_create_virtualstock_webhook_events extends Migration
{
    public function up(): void
    {
        $this->createTable('{{%virtualstock_webhook_events}}', [
            'id'            => $this->bigPrimaryKey(),
            'event'         => $this->string(100)->notNull()->comment('Event name, e.g. order.updated'),
            'payload_json'  => $this->text()->notNull()->comment('Full raw payload JSON from the webhook request'),
            'signature'     => $this->string(100)->null()->comment('X-Virtualstock-Signature header value'),
            'verified'      => $this->boolean()->notNull()->defaultValue(false)->comment('Whether the signature was successfully verified'),
            'processed'     => $this->boolean()->notNull()->defaultValue(false)->comment('Whether the event has been handled by the application'),
            'error_message' => $this->text()->null()->comment('Processing error message if handled=false'),
            'created_at'    => $this->integer()->unsigned()->notNull(),
            'processed_at'  => $this->integer()->unsigned()->null(),
        ]);

        $this->createIndex('idx_virtualstock_webhook_events_event', '{{%virtualstock_webhook_events}}', 'event');
        $this->createIndex('idx_virtualstock_webhook_events_processed', '{{%virtualstock_webhook_events}}', 'processed');
        $this->createIndex('idx_virtualstock_webhook_events_created_at', '{{%virtualstock_webhook_events}}', 'created_at');
    }

    public function down(): void
    {
        $this->dropTable('{{%virtualstock_webhook_events}}');
    }
}
