<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasColumn('email_bounced', 'user_id')) {
            DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX user_id ON email_bounced (user_id);', '0', NULL);");
        }
        if (Schema::hasColumn('email_clicked', 'user_id')) {
           DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX user_id ON email_clicked (user_id);', '0', NULL);");
        }
        if (Schema::hasColumn('email_bounced', 'user_id')) {
            try {
                DB::statement("ALTER TABLE `email_unsubscribed`  ADD KEY `user_id` (`user_id`);");
            } catch (\Exception $e) {
                
            }
        }
        try {
            DB::statement("ALTER TABLE `email_unsubscribed`  ADD KEY `contact_id` (`contact_id`);");
        } catch (\Exception $e) {
            
        }

        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX user_id ON subscribers (user_id);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX supervisor_id ON subscribers (supervisor_id);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX md5 ON subscribers (md5);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'CREATE INDEX suppression_id ON subscribers (suppression_id);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `subscriber_additional_data`  ADD KEY `user_id` (`user_id`);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `subscriber_additional_data`  ADD KEY `value` (`value`);', '0', NULL);");

        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `suppression_emails`  ADD KEY `user_id` (`user_id`);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `suppression_emails`  ADD KEY `list_id` (`list_id`);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `suppression_emails`  ADD KEY `import_id` (`import_id`);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `suppression_emails`  ADD KEY `md5` (`md5`);', '0', NULL);");
        DB::statement("INSERT INTO `alter_tables` (`id`, `name`, `query`, `status`, `fields` ) VALUES (NULL, 'indexes', 'ALTER TABLE `suppression_emails`  ADD KEY `supervisor` (`supervisor`);', '0', NULL);");

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
};
