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

import '../theme/app_theme.dart';

/// D6: when `status.sh` polling fails, the last good snapshot is kept and
/// this banner surfaces staleness instead of the dashboard silently
/// blanking out or crashing.
class StaleBanner extends StatelessWidget {
  final SessionsPoll poll;
  const StaleBanner({super.key, required this.poll});

  @override
  Widget build(BuildContext context) {
    final shouldShow = poll.stale || (poll.snapshot == null && poll.error != null);
    if (!shouldShow) return const SizedBox.shrink();

    final lastOk = poll.lastOkAt;
    final lastOkText = lastOk == null
        ? 'never'
        : '${lastOk.hour.toString().padLeft(2, '0')}:'
              '${lastOk.minute.toString().padLeft(2, '0')}:'
              '${lastOk.second.toString().padLeft(2, '0')}';

    return Container(
      width: double.infinity,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      color: AppColors.warning.withValues(alpha: 0.15),
      child: Row(
        children: [
          const Icon(
            Icons.warning_amber_rounded,
            color: AppColors.warning,
            size: 18,
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Text(
              '⚠ status snapshot stale (last ok: $lastOkText)${poll.error != null ? ' — Error: ${poll.error}' : ''}',
              style: const TextStyle(
                color: AppColors.warning,
                fontWeight: FontWeight.w600,
              ),
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }
}
