// Recovery affordance shown when Studio cannot locate the `fai`
// binary on the machine. Replaces the old CLI-jargon dead-end
// ("set FAI_BIN to its full path") with two acts a non-CLI
// operator can actually take:
//
//   1. Locate the binary with a native file picker, persisting
//      the choice via SystemActions.setFaiBinaryPath.
//   2. Open the install guide in the OS browser.

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

import '../data/system_actions.dart';
import '../l10n/app_localizations.dart';
import '../theme/tokens.dart';

class FaiBinaryRecovery extends StatelessWidget {
  /// Fired after the operator successfully picks a `fai` binary,
  /// so the host can retry whatever action hit the missing
  /// binary (e.g. re-run the daemon start / status probe).
  final VoidCallback? onLocated;

  const FaiBinaryRecovery({super.key, this.onLocated});

  Future<void> _locate(BuildContext context) async {
    final l = AppLocalizations.of(context)!;
    final messenger = ScaffoldMessenger.of(context);
    final picked = await FilePicker.pickFiles(
      dialogTitle: l.faiBinaryPickerTitle,
    );
    final path = picked?.files.single.path;
    if (path == null) return;
    final ok = await SystemActions.setFaiBinaryPath(path);
    messenger.showSnackBar(
      SnackBar(
        content: Text(ok ? l.faiBinarySetOk(path) : l.faiBinaryNotFound),
      ),
    );
    if (ok) onLocated?.call();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final l = AppLocalizations.of(context)!;
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(FaiSpace.md),
      decoration: BoxDecoration(
        color: theme.colorScheme.errorContainer.withValues(alpha: 0.4),
        borderRadius: BorderRadius.circular(FaiRadius.sm),
        border: Border.all(
          color: theme.colorScheme.error.withValues(alpha: 0.4),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            l.faiBinaryNotFound,
            style: theme.textTheme.bodyMedium?.copyWith(
              fontWeight: FontWeight.w600,
              color: theme.colorScheme.error,
            ),
          ),
          const SizedBox(height: FaiSpace.xs),
          Text(
            l.faiBinaryNotFoundHint,
            style: theme.textTheme.bodySmall?.copyWith(
              color: theme.colorScheme.onSurfaceVariant,
            ),
          ),
          const SizedBox(height: FaiSpace.md),
          Wrap(
            spacing: FaiSpace.sm,
            runSpacing: FaiSpace.sm,
            children: [
              FilledButton.tonalIcon(
                onPressed: () => _locate(context),
                icon: const Icon(Icons.folder_open, size: 16),
                label: Text(l.faiBinaryLocateButton),
              ),
              OutlinedButton.icon(
                onPressed: () =>
                    SystemActions.openInOs(kFaiInstallDocsUrl),
                icon: const Icon(Icons.open_in_new, size: 16),
                label: Text(l.faiBinaryInstallDocsButton),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
