import 'package:flutter/material.dart';

import '../models/member_model.dart';
import '../utils/enum_mappings.dart';

class OwnerDropdown extends StatelessWidget {
  const OwnerDropdown({
    super.key,
    required this.members,
    required this.value,
    required this.onChanged,
    this.enabled = true,
    this.onItemsBuilt,
  });

  final List<Member> members;
  final String? value;
  final ValueChanged<String?> onChanged;
  final bool enabled;

  /// 테스트 전용: 빌드된 항목 수를 알리기 위한 콜백(프로덕션에서는 사용하지 않음)
  final ValueChanged<int>? onItemsBuilt;

  @override
  Widget build(BuildContext context) {
    final items = members.map((member) {
      return DropdownMenuItem<String>(
        value: member.userId,
        child: Text(
          '${member.name} (${getMemberTypeLabel(member.memberType)})',
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }).toList();

    // 테스트에서만 사용
    if (onItemsBuilt != null) {
      // scheduleMicrotask로 프레임 안전하게 호출
      Future.microtask(() => onItemsBuilt!(items.length));
    }

    return DropdownButtonFormField<String>(
      decoration: const InputDecoration(
        labelText: '모임장 설정',
        border: OutlineInputBorder(),
      ),
      isExpanded: true,
      value: value,
      items: items,
      onChanged: enabled ? onChanged : null,
    );
  }
}
