<?php

use Licenzo\Framework\Database\Migration\Migration;

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

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

    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',

            // Action details
            'action_type'       => 'VARCHAR(50) NOT NULL', // cancel, upgrade, downgrade, price_change, pause, resume
            'action_data'       => 'LONGTEXT NULL COMMENT "JSON data for the action"',

            // Scheduling
            'scheduled_at'      => 'TIMESTAMP NOT NULL',
            'executed_at'       => 'TIMESTAMP NULL',

            // Status
            'status'            => 'VARCHAR(50) NOT NULL DEFAULT "pending"', // pending, executed, canceled, failed

            // Execution details
            'execution_result'  => 'LONGTEXT NULL COMMENT "JSON result of execution"',
            'failure_reason'    => 'TEXT NULL',

            // Metadata
            'triggered_by'      => 'BIGINT UNSIGNED NULL COMMENT "User ID who created the schedule"',
            'notes'             => 'TEXT NULL',

            'created_at'        => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'updated_at'        => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
        ], [
            ['type' => 'KEY', 'name' => 'idx_subscription_id', 'columns' => ['subscription_id']],
            ['type' => 'KEY', 'name' => 'idx_status_scheduled', 'columns' => ['status', 'scheduled_at']],
        ]);
    }

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

