<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class OptimizeTables extends Command
{
    protected $signature = 'db:optimize-tables';

    protected $description = 'Run OPTIMIZE TABLE on media, media_uses and vehicles tables';

    public function handle(): int
    {
        if (tenant('type') !== 'listing') {
            return self::SUCCESS;
        }

        $tables = ['media', 'media_uses', 'vehicles'];

        foreach ($tables as $table) {
            $this->info("Optimizing {$table}...");
            DB::statement("OPTIMIZE TABLE `{$table}`");
        }

        $this->info('Table optimization complete.');

        return self::SUCCESS;
    }
}
