<?php

namespace Deployer;

require_once 'recipe/common.php';

// Project name
set('application', 'snips');

// Project repository
set('repository', 'git@git.loken.nl:ardent/Snips.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);

// Shared files/dirs between deploys 
set('shared_dirs', ['var/log', 'var/sessions', 'var/snips']);
set('shared_files', ['.env.local']);
//set('writable_dirs', ['var']);
set('migrations_config', '');
set('allow_anonymous_stats', false);

// Hosts
host('snips.loken.nl')
    ->setRemoteUser('www-data')
    ->set('branch', function () {
        return input()->getOption('branch') ?: 'main';
    })
    ->set('deploy_path', '~/snips.loken.nl');

set('bin/console', function () {
    return parse('{{release_path}}/bin/console');
});

set('console_options', function () {
    return '--no-interaction';
});

desc('Clear cache');
task('cache:clear', function () {
    run('{{bin/php}} {{bin/console}} cache:clear {{console_options}} --no-warmup');
});

desc('Warm up cache');
task('cache:warmup', function () {
    run('{{bin/php}} {{bin/console}} cache:warmup {{console_options}}');
});

desc('Migrate database');
task('database:migrate', function () {
//    $options = '--allow-no-migration';
//    if (get('migrations_config') !== '') {
//        $options = sprintf('%s --configuration={{release_path}}/{{migrations_config}}', $options);
//    }
//
//    run(sprintf('{{bin/php}} {{bin/console}} doctrine:migrations:migrate %s {{console_options}}', $options));
    run('{{bin/php}} {{bin/console}} doctrine:schema:update --force');
});

task('deployment:log', function () { //https://stackoverflow.com/questions/59686270/how-to-log-deployments-in-deployer
    $branch = parse('{{branch}}');
    $date = date('Y-m-d H:i:s');
    $commitHashShort = runLocally('git rev-parse --short HEAD');
//    $commitHash = runLocally('git rev-parse HEAD');
    $commit = explode(PHP_EOL, runLocally('git log -1 --pretty="%H%n%ci"'));
    $commitHash = $commit[0];
    $commitDate = $commit[1];

//    $line = sprintf('%s %s branch="%s" hash="%s"', $date, $commitHashShort, $branch, $commitHash);
    $projectUrlBase = 'https://git.loken.nl/ardent/Snips';
    $array = [
        'branch' => $branch,
        'branchUrl' => sprintf('%s/src/branch/%s', $projectUrlBase, $branch),
        'date' => $date,
        'commitHashShort' => $commitHashShort,
        'commitHashLong' => $commitHash,
        'commitDate' => $commitDate,
        'commitUrl' => sprintf('%s/commit/%s', $projectUrlBase, $commitHash),
        'projectUrl' => $projectUrlBase,
    ];
    $json = json_encode($array, JSON_PRETTY_PRINT);

    runLocally("echo '$json' > release.json");
    upload('release.json', '{{release_path}}/release.json');
});

//desc('Deploy project');
//task('deploy', [
//    'deployment:log',
//]);

desc('Deploy project');
task('deploy', [
    'deploy:prepare',
    'deploy:vendors',
    'cache:clear',
    'cache:warmup',
    'database:migrate',
    'deployment:log',
    'deploy:symlink',
    'deploy:unlock',
    'deploy:cleanup',
]);

after('deploy', 'deploy:success');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');