<?php

namespace App\Services\Installer;

use App\DTOs\Installer\WizardStateData;
use Illuminate\Support\Facades\Artisan;
use RuntimeException;
use Throwable;

class InstallMigrationAction
{
    public function __construct(
        private readonly InstallerDatabaseConfigurator $databaseConfigurator,
    ) {}

    public function execute(WizardStateData $state): void
    {
        $this->databaseConfigurator->apply($state);

        try {
            Artisan::call('migrate', ['--force' => true]);
        } catch (Throwable $e) {
            throw new RuntimeException('Migration failed: '.$e->getMessage(), 0, $e);
        }
    }
}
