<?php

use Licenzo\Framework\Database\Migration\Migration;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

return new class extends Migration {
    protected $table = 'licenzo_license_events';

    public function up()
    {
        return $this->create($this->table, [
            'id'                => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'license_id'        => 'BIGINT UNSIGNED NOT NULL',
            'product_id'        => 'BIGINT UNSIGNED NULL',
            'variation_id'      => 'BIGINT UNSIGNED NULL',
            'activation_id'     => 'BIGINT UNSIGNED NULL COMMENT "Reference to activation record"',

            'product_name'      => 'VARCHAR(255) NOT NULL',
            'variation_name'    => 'VARCHAR(255) NULL',

            // Event details
            'event_type'        => 'VARCHAR(100) NOT NULL',
            'event_source'      => 'VARCHAR(50) NULL',
            'event_data'        => 'LONGTEXT NULL COMMENT "JSON with event-specific data"',

            // Request details
            'instance_identifier' => 'VARCHAR(255) NULL',
            'ip_address'        => 'VARCHAR(45) NULL',
            'user_agent'        => 'TEXT NULL',
            'request_hash'      => 'VARCHAR(255) NULL COMMENT "Hash sent in request"',

            // Response details
            'response_status'   => 'VARCHAR(50) NULL',
            'response_message'  => 'TEXT NULL',
            'response_data'     => 'LONGTEXT NULL COMMENT "JSON response data"',

            // Audit
            'triggered_by'      => 'BIGINT UNSIGNED NULL COMMENT "User ID who triggered"',

            'created_at'        => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
        ], [
            ['type' => 'KEY', 'name' => 'idx_license_id', 'columns' => ['license_id']],
        ]);
    }

    public function down()
    {
        return $this->drop($this->table);
    }
};

