<?php

use Licenzo\Framework\Database\Migration\Migration;

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

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

    public function up()
    {
        return $this->create($this->table, [
            'id'                => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'subscription_id'   => 'BIGINT UNSIGNED NOT NULL',
            'customer_id'       => 'BIGINT UNSIGNED NOT NULL',

            // Event details
            'event_type'        => 'VARCHAR(100) NOT NULL', // status_changed, trial_started, license_created, etc.
            'event_source'      => 'VARCHAR(50) NULL', // stripe, paypal, woocommerce, system, admin, api
            'event_data'        => 'LONGTEXT NULL', // JSON with event-specific data

            // Polymorphic relationship (optional)
            'related_id'        => 'BIGINT UNSIGNED NULL', // transaction_id, webhook_id, license_id, schedule_id
            'related_type'      => 'VARCHAR(50) NULL', // transaction, webhook, license, schedule

            // Status tracking
            'status'            => 'VARCHAR(50) DEFAULT "processed"', // pending, processed, failed

            // Audit
            'triggered_by'      => 'BIGINT UNSIGNED NULL', // user_id who triggered (NULL for system)

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

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

