<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\MigrationService;

class MigrateTenants extends Command
{
    protected $signature = 'migrate:tenants {--rollback} {--step=1}';
    protected $description = 'Run migrations on all tenant databases';

    public function handle(MigrationService $migrationService)
    {
        if ($this->option('rollback')) {
            $result = $migrationService->rollbackTenantMigrations($this->option('step'));
        } else {
            $result = $migrationService->runTenantMigrations();
        }

        $this->info($result['message']);

        if (!empty($result['results'])) {
            $tableData = [];
            foreach ($result['results'] as $r) {
                $tableData[] = [$r['tenant'], $r['status'], $r['message']];
            }
            $this->table(['Tenant Database', 'Status', 'Message'], $tableData);
        }
    }
}
