import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lunchpick/core/constants/app_colors.dart';
import 'package:lunchpick/core/constants/app_typography.dart';
import 'package:lunchpick/presentation/providers/debug_test_data_provider.dart';

class DebugTestDataBanner extends ConsumerWidget {
  final EdgeInsetsGeometry? margin;

  const DebugTestDataBanner({super.key, this.margin});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    if (!kDebugMode) {
      return const SizedBox.shrink();
    }

    final isDark = Theme.of(context).brightness == Brightness.dark;
    final state = ref.watch(debugTestDataNotifierProvider);
    final notifier = ref.read(debugTestDataNotifierProvider.notifier);

    return Card(
      margin: margin ?? const EdgeInsets.all(16),
      color: isDark ? AppColors.darkSurface : AppColors.lightSurface,
      elevation: 1,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                const Icon(
                  Icons.science_outlined,
                  color: AppColors.lightPrimary,
                  size: 20,
                ),
                const SizedBox(width: 8),
                Text(
                  '테스트 데이터 미리보기',
                  style: AppTypography.body1(
                    isDark,
                  ).copyWith(fontWeight: FontWeight.w600),
                ),
                const Spacer(),
                if (state.isProcessing)
                  const SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  ),
                const SizedBox(width: 8),
                Switch.adaptive(
                  value: state.isEnabled,
                  onChanged: state.isProcessing
                      ? null
                      : (value) async {
                          if (value) {
                            await notifier.enableTestData();
                          } else {
                            await notifier.disableTestData();
                          }
                        },
                  activeColor: AppColors.lightPrimary,
                ),
              ],
            ),
            const SizedBox(height: 8),
            Text(
              state.isEnabled
                  ? '디버그 빌드에서만 적용됩니다. 기록/통계 UI를 테스트용 데이터로 확인하세요.'
                  : '디버그 빌드에서만 사용 가능합니다. 스위치를 켜면 추천·방문 기록이 자동으로 채워집니다.',
              style: AppTypography.caption(isDark),
            ),
            if (state.errorMessage != null) ...[
              const SizedBox(height: 6),
              Text(
                state.errorMessage!,
                style: AppTypography.caption(isDark).copyWith(
                  color: AppColors.lightError,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
