<?php

namespace App\Console\Commands;

use App\Services\DatabaseBackup;
use Illuminate\Console\Command;

class BackupRun extends Command
{
    protected $signature = 'app:backup {--force : Run even if the schedule is disabled}';

    protected $description = 'Create a database backup (honours the configured schedule) and prune old ones.';

    public function handle(DatabaseBackup $backup): int
    {
        $settings = $backup->settings();

        if (! ($settings['enabled'] ?? false) && ! $this->option('force')) {
            $this->info('Automatic backups are disabled — skipping. Use --force to run anyway.');

            return self::SUCCESS;
        }

        $name = $backup->create();
        $deleted = $backup->prune((int) ($settings['keep'] ?? 14));

        $this->info("Backup created: {$name}".($deleted ? " (pruned {$deleted} old backup(s))" : ''));

        return self::SUCCESS;
    }
}
