<?php

namespace App\Actions\Init;

use MirazMac\DotEnv\Writer as EnvWriter;

class WriteEnv
{
    public function handle(string $projectDir, array $values): void
    {
        $envExamplePath = $projectDir . '/.env.example';
        $envPath = $projectDir . '/.env';

        if (!file_exists($envExamplePath)) {
            throw new \RuntimeException('Unable to find `.env.example` in the starter project.');
        }

        $writer = new EnvWriter($envExamplePath);

        foreach ($values as $key => $value) {
            $writer->set($key, (string) $value);
        }

        $writer->write(true, $envPath);
    }
}
