<?php

use ZenoChatbot\Framework\Database\Migration\Migration;

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

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

    public function up()
    {
        return $this->create($this->table, [
            'id' => 'BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY',
            'entity_type' => 'VARCHAR(200) NOT NULL',
            'entity_id' => 'BIGINT UNSIGNED NOT NULL',
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
            'meta_key' => 'VARCHAR(255) NOT NULL',
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
            'meta_value' => 'LONGTEXT NULL',
            'created_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
            'updated_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
        ], [
            'INDEX idx_entity_type (entity_type)',
            'INDEX idx_entity_id (entity_id)',
            'INDEX idx_meta_key (meta_key)',
            'INDEX idx_entity_type_id (entity_type, entity_id)',
            'INDEX idx_entity_key (entity_type, entity_id, meta_key)',
            'UNIQUE KEY unique_entity_meta (entity_type, entity_id, meta_key)'
        ]);
    }

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