<?php

namespace App\Service\System;

use Symfony\Component\Process\Process;

class GitClient
{
    public function getLastCommitInfo(string $workingDir): array
    {
        $gitCmd = sprintf(
            'git config --global --add safe.directory %s && git log -1 --format="%%s|%%ci|%%h"',
            $workingDir
        );

        $process = Process::fromShellCommandline($gitCmd);
        $process->setWorkingDirectory($workingDir);
        $process->run();

        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }

        $outputGit = explode('|', trim($process->getOutput()));
        
        return [
            'message' => $outputGit[0] ?? '',
            'date' => $outputGit[1] ?? date('Y-m-d H:i:s'),
            'hash' => $outputGit[2] ?? 'unknown'
        ];
    }
}
