<?php

namespace App\Console\Commands;

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

class SecurityBackupCommand extends Command
{
    protected $signature = 'security:backup
        {--path-only : Output only the created backup bundle path for automation}';

    protected $description = 'Create and verify a checksummed database and private-document backup';

    public function handle(BackupService $backups): int
    {
        try {
            $result = $backups->create();
        } catch (\Throwable $exception) {
            $this->error('Backup failed: '.$exception->getMessage());

            return self::FAILURE;
        }

        if ($this->option('path-only')) {
            $this->line($result['path']);

            return self::SUCCESS;
        }

        $this->info('Backup created and verified.');
        $this->line('Path: '.$result['path']);
        $this->line('Database: '.$result['manifest']['database']['driver'].' · '.$result['manifest']['database']['bytes'].' bytes');
        $this->line('Private files: '.count($result['manifest']['private_files']));

        return self::SUCCESS;
    }
}
