// Recovery affordance shown when Studio cannot locate the `fai`
// binary on the machine. Replaces the old CLI-jargon dead-end
// ("set CHAIN_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 ChainBinaryRecovery 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 ChainBinaryRecovery({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.chainBinaryPickerTitle,
    );
    final path = picked?.files.single.path;
    if (path == null) return;
    final ok = await SystemActions.setFaiBinaryPath(path);
    messenger.showSnackBar(
      SnackBar(
        content: Text(ok ? l.chainBinarySetOk(path) : l.chainBinaryNotFound),
      ),
    );
    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(ChainSpace.md),
      decoration: BoxDecoration(
        color: theme.colorScheme.errorContainer.withValues(alpha: 0.4),
        borderRadius: BorderRadius.circular(ChainRadius.sm),
        border: Border.all(
          color: theme.colorScheme.error.withValues(alpha: 0.4),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            l.chainBinaryNotFound,
            style: theme.textTheme.bodyMedium?.copyWith(
              fontWeight: FontWeight.w600,
              color: theme.colorScheme.error,
            ),
          ),
          const SizedBox(height: ChainSpace.xs),
          Text(
            l.chainBinaryNotFoundHint,
            style: theme.textTheme.bodySmall?.copyWith(
              color: theme.colorScheme.onSurfaceVariant,
            ),
          ),
          const SizedBox(height: ChainSpace.md),
          Wrap(
            spacing: ChainSpace.sm,
            runSpacing: ChainSpace.sm,
            children: [
              FilledButton.tonalIcon(
                onPressed: () => _locate(context),
                icon: const Icon(Icons.folder_open, size: 16),
                label: Text(l.chainBinaryLocateButton),
              ),
              OutlinedButton.icon(
                onPressed: () =>
                    SystemActions.openInOs(kFaiInstallDocsUrl),
                icon: const Icon(Icons.open_in_new, size: 16),
                label: Text(l.chainBinaryInstallDocsButton),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
