<?php

namespace App\Console\Commands;

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

class DropBlogTablesForAllClients extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'drop:blogs';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
{
    $clients = DB::connection('master')
        ->table('clients')
        ->pluck('id')
        ->toArray();

    $tables = ['blogs', 'blog_tag', 'industries', 'tags'];

    foreach ($clients as $clientId) {

        $connectionName = "mysql_" . $clientId;

        if (!config("database.connections.$connectionName")) {
            $this->info("Skipping missing DB: $connectionName");
            continue;
        }

        try {
            foreach ($tables as $table) {
                Schema::connection($connectionName)->dropIfExists($table);
            }

            $this->info("Dropped tables for: $connectionName");

        } catch (\Exception $e) {
            $this->error("Error in $connectionName: " . $e->getMessage());
        }
    }

    $this->info("✔ All client blog tables dropped successfully.");
    return Command::SUCCESS;
}

}
