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/visit_provider.dart';

class VisitConfirmationDialog extends ConsumerWidget {
  final String restaurantId;
  final String restaurantName;
  final DateTime recommendationTime;

  const VisitConfirmationDialog({
    super.key,
    required this.restaurantId,
    required this.restaurantName,
    required this.recommendationTime,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return AlertDialog(
      backgroundColor: isDark ? AppColors.darkSurface : AppColors.lightSurface,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      title: Column(
        children: [
          Icon(Icons.restaurant, size: 48, color: AppColors.lightPrimary),
          const SizedBox(height: 8),
          Text(
            '다녀왔음? 🍴',
            style: AppTypography.heading2(isDark),
            textAlign: TextAlign.center,
          ),
        ],
      ),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            restaurantName,
            style: AppTypography.heading2(
              isDark,
            ).copyWith(color: AppColors.lightPrimary),
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 8),
          Text(
            '어땠어요? 방문 기록을 남겨주세요!',
            style: AppTypography.body2(isDark),
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 16),
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: (isDark
                  ? AppColors.darkBackground
                  : AppColors.lightBackground),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.access_time,
                  size: 16,
                  color: isDark
                      ? AppColors.darkTextSecondary
                      : AppColors.lightTextSecondary,
                ),
                const SizedBox(width: 4),
                Text(
                  '추천 시간: ${_formatTime(recommendationTime)}',
                  style: AppTypography.caption(isDark),
                ),
              ],
            ),
          ),
        ],
      ),
      actions: [
        Row(
          children: [
            Expanded(
              child: TextButton(
                onPressed: () => Navigator.of(context).pop(false),
                style: TextButton.styleFrom(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                ),
                child: Text(
                  '안 갔어요',
                  style: AppTypography.body1(isDark).copyWith(
                    color: isDark
                        ? AppColors.darkTextSecondary
                        : AppColors.lightTextSecondary,
                  ),
                ),
              ),
            ),
            const SizedBox(width: 8),
            Expanded(
              child: ElevatedButton(
                onPressed: () async {
                  // 방문 기록 추가
                  await ref
                      .read(visitNotifierProvider.notifier)
                      .addVisitRecord(
                        restaurantId: restaurantId,
                        visitDate: DateTime.now(),
                        isConfirmed: true,
                      );

                  if (context.mounted) {
                    Navigator.of(context).pop(true);

                    // 성공 메시지 표시
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: const Text('방문 기록이 저장되었습니다! 👍'),
                        backgroundColor: AppColors.lightPrimary,
                        behavior: SnackBarBehavior.floating,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                    );
                  }
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.lightPrimary,
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                child: const Text('갔다왔어요!'),
              ),
            ),
          ],
        ),
      ],
    );
  }

  String _formatTime(DateTime dateTime) {
    final hour = dateTime.hour.toString().padLeft(2, '0');
    final minute = dateTime.minute.toString().padLeft(2, '0');
    return '$hour:$minute';
  }

  static Future<bool?> show({
    required BuildContext context,
    required String restaurantId,
    required String restaurantName,
    required DateTime recommendationTime,
  }) {
    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (context) => VisitConfirmationDialog(
        restaurantId: restaurantId,
        restaurantName: restaurantName,
        recommendationTime: recommendationTime,
      ),
    );
  }
}
