<?php

use yii\db\Migration;

/**
 * Creates the virtualstock_api_log table for structured API request logging.
 *
 * When `ApiLogger` is configured with a DB log target, entries are stored here.
 * Useful for debugging, audit trails, and performance monitoring.
 */
class m260503_000002_create_virtualstock_api_log extends Migration
{
    public function up(): void
    {
        $this->createTable('{{%virtualstock_api_log}}', [
            'id'          => $this->bigPrimaryKey(),
            'method'      => $this->string(10)->notNull()->comment('HTTP method: GET, POST, PATCH, DELETE'),
            'uri'         => $this->string(500)->notNull()->comment('Request URI (relative to base URL)'),
            'status_code' => $this->smallInteger()->unsigned()->null()->comment('HTTP response status code'),
            'duration_ms' => $this->integer()->unsigned()->null()->comment('Request duration in milliseconds'),
            'request_body'  => $this->text()->null()->comment('Request body (may be null if logging is restricted)'),
            'response_body' => $this->text()->null()->comment('Response body (may be null if logging is restricted)'),
            'error_class'   => $this->string(200)->null()->comment('Exception class name if request failed'),
            'error_message' => $this->text()->null()->comment('Exception message if request failed'),
            'created_at'  => $this->integer()->unsigned()->notNull(),
        ]);

        $this->createIndex('idx_virtualstock_api_log_method_uri', '{{%virtualstock_api_log}}', ['method', 'uri']);
        $this->createIndex('idx_virtualstock_api_log_status_code', '{{%virtualstock_api_log}}', 'status_code');
        $this->createIndex('idx_virtualstock_api_log_created_at', '{{%virtualstock_api_log}}', 'created_at');
    }

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