<?php

use yii\db\Migration;

/**
 * Creates the virtualstock_orders table for local order tracking.
 *
 * Stores a lightweight record per VirtualStock order so the host application
 * can track import state, status changes, and sync timestamps without
 * re-polling the API.
 */
class m260503_000001_create_virtualstock_orders extends Migration
{
    public function up(): void
    {
        $this->createTable('{{%virtualstock_orders}}', [
            'id'              => $this->primaryKey(),
            'order_no'        => $this->string(100)->notNull()->unique()->comment('VirtualStock order number (e.g. VS-12345678901234)'),
            'vs_order_id'     => $this->string(100)->null()->comment('VirtualStock internal order ID'),
            'status'          => $this->string(50)->notNull()->defaultValue('ORDER')->comment('Current VirtualStock order status'),
            'last_sync_at'    => $this->dateTime()->null()->comment('Timestamp of last successful sync from API'),
            'updated_at_source' => $this->string(50)->null()->comment('updatedAt cursor value from VirtualStock API'),
            'shipments_json'  => $this->text()->null()->comment('Serialised shipment data from last API response'),
            'raw_json'        => $this->text()->null()->comment('Full raw order JSON from last API response'),
            'created_at'      => $this->integer()->unsigned()->notNull(),
            'updated_at'      => $this->integer()->unsigned()->notNull(),
        ]);

        $this->createIndex('idx_virtualstock_orders_status', '{{%virtualstock_orders}}', 'status');
        $this->createIndex('idx_virtualstock_orders_updated_at_source', '{{%virtualstock_orders}}', 'updated_at_source');
    }

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