import 'package:flutter/material.dart';

import 'package:im_app/core/ui/components/app_button.dart';

/// # AppDialog — 业务确认弹窗（L3 Composite）
///
/// 封装 showDialog，统一弹窗交互规范（标题 + 内容 + 确认/取消）。
/// 内部使用 AppButton，展示 L3 → L2 → L1 的完整组合链路。
///
/// ## 数据流位置
///
/// ```
/// View 层调用
///   → AppDialog.show() ← 你在这里（静态入口）
///     → showDialog<bool>
///       → AppDialog widget（AlertDialog 布局）
///         → AppButton.text（取消）
///         → AppButton.primary（确认）
///     ← Future<bool?> → true=确认, false=取消, null=点背景关闭
/// ```
///
/// ## 使用
///
/// ```dart
/// // View 层
/// final confirmed = await AppDialog.show(
///   context,
///   title: '删除联系人',
///   content: '确定要删除该联系人吗？此操作不可恢复。',
///   confirmLabel: '删除',
/// );
/// if (confirmed == true) {
///   ref.read(contactViewModelProvider.notifier).deleteContact(id);
/// }
/// ```
class AppDialog extends StatelessWidget {
  const AppDialog._({
    required this.title,
    required this.content,
    required this.confirmLabel,
    this.cancelLabel,
  });

  final String title;
  final String content;
  final String confirmLabel;
  final String? cancelLabel;

  /// 显示确认弹窗
  ///
  /// 返回：`true` = 确认，`false` = 取消，`null` = 点背景关闭
  static Future<bool?> show(
    BuildContext context, {
    required String title,
    required String content,
    String confirmLabel = '确定', // TODO: 接入国际化
    String? cancelLabel = '取消', // TODO: 接入国际化
    bool barrierDismissible = true,
  }) =>
      showDialog<bool>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (_) => AppDialog._(
          title: title,
          content: content,
          confirmLabel: confirmLabel,
          cancelLabel: cancelLabel,
        ),
      );

  @override
  Widget build(BuildContext context) => AlertDialog(
        title: Text(title),
        content: Text(content),
        actions: [
          if (cancelLabel != null)
            AppButton.text(
              label: cancelLabel!,
              fullWidth: false,
              onPressed: () => Navigator.of(context).pop(false),
            ),
          AppButton.primary(
            label: confirmLabel,
            fullWidth: false,
            onPressed: () => Navigator.of(context).pop(true),
          ),
        ],
      );
}
