<?php

use Licenzo\Framework\Database\Migration\Migration;

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

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

    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 NOT NULL',
            'variation_id'          => 'BIGINT UNSIGNED NOT NULL',

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

            // Instance identification
            'instance_identifier'   => 'VARCHAR(255) NOT NULL',

            // Activation hash
            'activation_hash'       => 'VARCHAR(191) NOT NULL UNIQUE',

            // Status
            'status'                => 'VARCHAR(50) NOT NULL DEFAULT "active"',

            // Request details
            'ip_address'            => 'VARCHAR(45) NULL',
            'user_agent'            => 'TEXT NULL',

            // Timestamps
            'activated_at'          => 'TIMESTAMP NOT NULL',
            'deactivated_at'        => 'TIMESTAMP NULL',
            'last_validated_at'     => 'TIMESTAMP NULL',

            // Method and metadata
            'activation_method'     => 'VARCHAR(100) NULL DEFAULT "key_based"',
            'expire_date'           => 'TIMESTAMP NULL',
            'meta'                  => 'LONGTEXT NULL',

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

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