<?php

use ExpressionEngine\Service\Migration\Migration;

/**
 * Migration to add missing cache_log_key index to jcogs_img_pro_cache_log table
 * 
 * This migration fixes Issue #99 where the composite index was not properly created
 * by the original migration due to table_exists() caching issues after table creation.
 * 
 * The index dramatically improves performance for UPDATE/SELECT/DELETE queries that use
 * WHERE clauses with site_id, adapter_name, adapter_type, path, and image_name columns.
 * 
 * @see https://github.com/jcogs-design/jcogs_img_pro/issues/99
 */
class AddCacheLogIndexes extends Migration
{
    /**
     * Execute the migration
     * @return void
     */
    public function up()
    {
        ee()->load->library('smartforge');

        // Drop the index if it exists (in case it was manually added)
        ee()->smartforge->drop_key('jcogs_img_pro_cache_log', 'cache_log_key');
        
        // Add the composite index with all five WHERE clause columns
        ee()->smartforge->add_key(
            'jcogs_img_pro_cache_log', 
            ['site_id', 'adapter_name', 'adapter_type', 'path', 'image_name'], 
            'cache_log_key'
        );
    }

    /**
     * Undo the migration
     * @return void
     */
    public function down()
    {
        ee()->load->library('smartforge');
        ee()->smartforge->drop_key('jcogs_img_pro_cache_log', 'cache_log_key');
    }
}
