<?php

namespace Modules\VetStaffPortal\Console;

use Illuminate\Console\Command;
use Modules\VetStaffPortal\Services\StaffPwaBuilder;

/**
 * Canonical rebuild of the staff PWA.
 *
 *   php artisan staff-portal:build-pwa
 *
 * Always builds  pwa-staff/  →  public/staff-app/  (safe temp-then-swap).
 */
class BuildStaffPwaCommand extends Command
{
    protected $signature   = 'staff-portal:build-pwa';
    protected $description = 'Rebuild the staff PWA from pwa-staff/ into public/staff-app (safe temp-then-swap)';

    public function handle(): int
    {
        $this->info('Building the staff PWA from pwa-staff/ → public/staff-app …');

        $result = StaffPwaBuilder::build();

        if (! empty($result['output'])) {
            $this->newLine();
            $this->line($result['output']);
        }

        $this->newLine();
        if ($result['ok']) {
            $this->info('✓ Staff PWA rebuilt successfully' . (isset($result['duration']) ? " in {$result['duration']}s." : '.'));
            return self::SUCCESS;
        }

        $this->error('✗ Staff PWA build failed.');
        if (! empty($result['hint'])) {
            $this->warn($result['hint']);
        }
        return self::FAILURE;
    }
}
