import 'package:flutter/material.dart';
import 'package:superport/models/address_model.dart';
import 'package:superport/models/company_model.dart';
import 'package:superport/utils/phone_utils.dart';

/// 지점(Branch) 폼 컨트롤러
///
/// 각 지점의 상태, 컨트롤러, 포커스, 드롭다운, 전화번호 등 관리를 담당
class BranchFormController {
  // 지점 데이터
  Branch branch;

  // 입력 컨트롤러
  final TextEditingController nameController;
  final TextEditingController contactNameController;
  final TextEditingController contactPositionController;
  final TextEditingController contactPhoneController;
  final TextEditingController contactEmailController;
  final TextEditingController remarkController;

  // 포커스 노드
  final FocusNode focusNode;
  // 카드 키(위젯 식별용)
  final GlobalKey cardKey;
  // 직책 드롭다운 상태
  final ValueNotifier<bool> positionDropdownNotifier;
  // 전화번호 접두사
  String selectedPhonePrefix;

  // 직책 목록(공통 상수로 관리 권장)
  final List<String> positions;
  // 전화번호 접두사 목록(공통 상수로 관리 권장)
  final List<String> phonePrefixes;

  BranchFormController({
    required this.branch,
    required this.positions,
    required this.phonePrefixes,
  }) : nameController = TextEditingController(text: branch.name),
       contactNameController = TextEditingController(
         text: branch.contactName ?? '',
       ),
       contactPositionController = TextEditingController(
         text: branch.contactPosition ?? '',
       ),
       contactPhoneController = TextEditingController(
         text: PhoneUtils.extractPhoneNumberWithoutPrefix(
           branch.contactPhone ?? '',
           phonePrefixes,
         ),
       ),
       contactEmailController = TextEditingController(
         text: branch.contactEmail ?? '',
       ),
       remarkController = TextEditingController(text: branch.remark ?? ''),
       focusNode = FocusNode(),
       cardKey = GlobalKey(),
       positionDropdownNotifier = ValueNotifier<bool>(false),
       selectedPhonePrefix = PhoneUtils.extractPhonePrefix(
         branch.contactPhone ?? '',
         phonePrefixes,
       );

  /// 주소 업데이트
  void updateAddress(Address address) {
    branch = branch.copyWith(address: address);
  }

  /// 필드별 값 업데이트
  void updateField(String fieldName, String value) {
    switch (fieldName) {
      case 'name':
        branch = branch.copyWith(name: value);
        break;
      case 'contactName':
        branch = branch.copyWith(contactName: value);
        break;
      case 'contactPosition':
        branch = branch.copyWith(contactPosition: value);
        break;
      case 'contactPhone':
        branch = branch.copyWith(
          contactPhone: PhoneUtils.getFullPhoneNumber(
            selectedPhonePrefix,
            value,
          ),
        );
        break;
      case 'contactEmail':
        branch = branch.copyWith(contactEmail: value);
        break;
      case 'remark':
        branch = branch.copyWith(remark: value);
        break;
    }
  }

  /// 전화번호 접두사 변경
  void updatePhonePrefix(String prefix) {
    selectedPhonePrefix = prefix;
    branch = branch.copyWith(
      contactPhone: PhoneUtils.getFullPhoneNumber(
        prefix,
        contactPhoneController.text,
      ),
    );
  }

  /// 리소스 해제
  void dispose() {
    nameController.dispose();
    contactNameController.dispose();
    contactPositionController.dispose();
    contactPhoneController.dispose();
    contactEmailController.dispose();
    remarkController.dispose();
    focusNode.dispose();
    positionDropdownNotifier.dispose();
    // cardKey는 위젯에서 자동 관리
  }
}
