<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Helpers\InseoHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class UpdateController extends Controller
{
    public function index()
    {
        return view('webprofile.backend.update.index');
    }

    public function update(Request $request)
    {
        $username = env('GIT_USERNAME', 'lorem');
        $password = InseoHelper::inseodecrypt(env('GIT_PASSWORD', 'lorem'), env('ENCRYPT_KEY', 'lorem'));
        $repository = env('GIT_REPOSITORY', 'lorem');

        $command = 'git pull http://'.$username.':'.$password.'@'.$repository.' master';
        $data = $request->tokenkey;
        $line = 'Token Tidak Valid';
        if ($data == env('TOKEN_KEY', 'lorem')) {
            $lines = null;
            $result = array();
            exec($command, $result);
            foreach ($result as $line) {
                $lines = $lines.$line.'<br>';
            }

            if ($line == null) {
                $line = 'Whoops something when wrong!! Check your username and password';
            }

            Artisan::call('migrate',
            array('--force' => true));

            // $command = 'cd .. && php artisan migrate';
            // exec($command, $resultMigrate);

            // foreach ($resultMigrate as $lineMigration) {
            //     $lines = $lines.$lineMigration.'<br>';
            // }
        }

        $data = [
            'data' => $lines,
        ];

        return view('webprofile.backend.update.result', $data);
    }
}
