<?php

namespace App\Console\Commands;

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

class VerifySecurityBackupCommand extends Command
{
    protected $signature = 'security:backup-verify {path : Absolute or relative backup bundle path}';

    protected $description = 'Verify database and private-file checksums in a security backup bundle';

    public function handle(BackupService $backups): int
    {
        $path = (string) $this->argument('path');
        if (! str_starts_with($path, DIRECTORY_SEPARATOR)) {
            $path = base_path($path);
        }

        try {
            $result = $backups->verify($path);
        } catch (\Throwable $exception) {
            $this->error('Backup verification failed: '.$exception->getMessage());

            return self::FAILURE;
        }

        $this->info('Backup verification passed.');
        $this->line('Database driver: '.$result['database']['driver']);
        $this->line('Private files: '.$result['private_files']);
        $this->line('Private bytes: '.$result['bytes']);

        return self::SUCCESS;
    }
}
