import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:superport/screens/common/templates/form_layout_template.dart';
import 'package:superport/utils/currency_formatter.dart';
import 'package:superport/core/widgets/category_cascade_form_field.dart';
import 'controllers/equipment_in_form_controller.dart';
import 'widgets/equipment_vendor_model_selector.dart';
import 'package:superport/utils/formatters/number_formatter.dart';

/// 새로운 Equipment 입고 폼 (Lookup API 기반)
class EquipmentInFormScreen extends StatefulWidget {
  final int? equipmentInId;

  const EquipmentInFormScreen({super.key, this.equipmentInId});

  @override
  State<EquipmentInFormScreen> createState() => _EquipmentInFormScreenState();
}

class _EquipmentInFormScreenState extends State<EquipmentInFormScreen> {
  late EquipmentInFormController _controller;
  late TextEditingController _serialNumberController;
  late TextEditingController _initialStockController;
  late TextEditingController _purchasePriceController;

  @override
  void initState() {
    super.initState();
    _controller = EquipmentInFormController(equipmentInId: widget.equipmentInId);
    _controller.addListener(_onControllerUpdated);
    
    // TextEditingController 초기화
    _serialNumberController = TextEditingController(text: _controller.serialNumber);
    _serialNumberController = TextEditingController(text: _controller.serialNumber);
    _initialStockController = TextEditingController(text: _controller.initialStock.toString());
    _purchasePriceController = TextEditingController(
      text: _controller.purchasePrice != null 
          ? CurrencyFormatter.formatKRW(_controller.purchasePrice)
          : ''
    );
    
    // 수정 모드일 때 데이터 로드
    if (_controller.isEditMode) {
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        await _controller.initializeForEdit();
        // 데이터 로드 후 컨트롤러 업데이트
        _serialNumberController.text = _controller.serialNumber;
        _serialNumberController.text = _controller.serialNumber;
        _purchasePriceController.text = _controller.purchasePrice != null 
            ? CurrencyFormatter.formatKRW(_controller.purchasePrice)
            : '';
      });
    }
  }

  @override
  void dispose() {
    _controller.removeListener(_onControllerUpdated);
    _controller.dispose();
    _serialNumberController.dispose();
    _serialNumberController.dispose();
    _initialStockController.dispose();
    _purchasePriceController.dispose();
    super.dispose();
  }

  void _onControllerUpdated() {
    if (mounted) setState(() {});
  }

  // Legacy 필드 제거 - Vendor/Model cascade selector 사용

  // 유효한 구매처 ID 반환 (드롭다운 assertion 오류 방지)
  int? _getValidCompanyId() {
    if (_controller.selectedCompanyId == null) return null;
    
    final isValid = _controller.companies.containsKey(_controller.selectedCompanyId);
    print('DEBUG [_getValidCompanyId] selectedCompanyId: ${_controller.selectedCompanyId}, isValid: $isValid, available companies: ${_controller.companies.length}');
    
    return isValid ? _controller.selectedCompanyId : null;
  }

  // 유효한 창고 ID 반환 (드롭다운 assertion 오류 방지)
  int? _getValidWarehouseId() {
    if (_controller.selectedWarehouseId == null) return null;
    
    final isValid = _controller.warehouses.containsKey(_controller.selectedWarehouseId);
    print('DEBUG [_getValidWarehouseId] selectedWarehouseId: ${_controller.selectedWarehouseId}, isValid: $isValid, available warehouses: ${_controller.warehouses.length}');
    
    return isValid ? _controller.selectedWarehouseId : null;
  }

  Future<void> _onSave() async {
    if (_controller.isSaving) return;
    
    final success = await _controller.save();
    if (success && mounted) {
      Navigator.pop(context, true);
    } else if (_controller.error != null && mounted) {
      ShadToaster.of(context).show(
        ShadToast.destructive(
          title: const Text('오류'),
          description: Text(_controller.error!),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // 간소화된 디버깅
    print('🎯 [UI] canSave: ${_controller.canSave} | 장비번호: "${_controller.serialNumber}" | 제조사: "${_controller.manufacturer}"');
    
    return FormLayoutTemplate(
      title: _controller.isEditMode ? '장비 수정' : '장비 입고',
      onSave: _controller.canSave && !_controller.isSaving ? _onSave : null,
      onCancel: () => Navigator.of(context).pop(),
      isLoading: _controller.isSaving,
      child: _controller.isLoading 
          ? const Center(child: ShadProgress())
          : Form(
              key: _controller.formKey,
              child: SingleChildScrollView(
                padding: const EdgeInsets.only(bottom: 24),
                child: Column(
                  children: [
                    _buildBasicFields(),
                    const SizedBox(height: 24),
                    _buildCategorySection(),
                    const SizedBox(height: 24),
                    _buildLocationSection(),
                    const SizedBox(height: 24),
                    _buildPurchaseSection(),
                    const SizedBox(height: 24),
                    _buildRemarkSection(),
                  ],
                ),
              ),
            ),
    );
  }

  Widget _buildBasicFields() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '기본 정보',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            // 장비 번호 (필수)
            ShadInputFormField(
              controller: _serialNumberController,
              readOnly: _controller.isFieldReadOnly('serialNumber'),
              placeholder: Text(_controller.isFieldReadOnly('serialNumber') 
                  ? (_controller.serialNumber.isNotEmpty ? _controller.serialNumber : '장비 번호 없음')
                  : '장비 번호를 입력하세요'),
              label: Text(_controller.isFieldReadOnly('serialNumber') 
                  ? '장비 번호 * 🔒' : '장비 번호 *'),
              validator: (value) {
                if (value.trim().isEmpty ?? true) {
                  return '장비 번호는 필수입니다';
                }
                return null;
              },
              onChanged: _controller.isFieldReadOnly('serialNumber') ? null : (value) {
                _controller.serialNumber = value.trim() ?? '';
                setState(() {});
                print('DEBUG [장비번호 입력] value: "$value", controller.serialNumber: "${_controller.serialNumber}"');
              },
            ),
            const SizedBox(height: 16),
            
            // Vendor→Model cascade 선택기
            EquipmentVendorModelSelector(
              initialVendorId: _controller.vendorId,
              initialModelId: _controller.modelsId,
              onChanged: _controller.onVendorModelChanged,
              isReadOnly: _controller.isFieldReadOnly('modelsId'),
            ),
            const SizedBox(height: 16),
            
            // 시리얼 번호 (선택)
            ShadInputFormField(
              controller: _serialNumberController,
              readOnly: _controller.isFieldReadOnly('serialNumber'),
              placeholder: Text(_controller.isFieldReadOnly('serialNumber') 
                  ? '수정불가' : '시리얼 번호를 입력하세요'),
              label: Text(_controller.isFieldReadOnly('serialNumber') 
                  ? '시리얼 번호 🔒' : '시리얼 번호'),
              onChanged: _controller.isFieldReadOnly('serialNumber') ? null : (value) {
                _controller.serialNumber = value.trim() ?? '';
                setState(() {});
                print('DEBUG [시리얼번호 입력] value: "$value", controller.serialNumber: "${_controller.serialNumber}"');
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCategorySection() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '장비 분류',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            CategoryCascadeFormField(
              category1: _controller.category1.isEmpty ? null : _controller.category1,
              category2: _controller.category2.isEmpty ? null : _controller.category2,
              category3: _controller.category3.isEmpty ? null : _controller.category3,
              onChanged: (cat1, cat2, cat3) {
                _controller.category1 = cat1?.trim() ?? '';
                _controller.category2 = cat2?.trim() ?? '';
                _controller.category3 = cat3?.trim() ?? '';
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildLocationSection() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '위치 정보',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            // 구매처 (드롭다운 전용)
            ShadSelect<int>(
              initialValue: _getValidCompanyId(),
              placeholder: const Text('구매처를 선택하세요'),
              options: _controller.companies.entries.map((entry) => 
                ShadOption(
                  value: entry.key,
                  child: Text(entry.value),
                )
              ).toList(),
              selectedOptionBuilder: (context, value) => 
                Text(_controller.companies[value] ?? '선택하세요'),
              onChanged: (value) {
                setState(() {
                  _controller.selectedCompanyId = value;
                });
                print('DEBUG [구매처 선택] value: $value, companies: ${_controller.companies.length}');
              },
            ),
            const SizedBox(height: 16),
            
            // 입고지 (드롭다운 전용)
            ShadSelect<int>(
              initialValue: _getValidWarehouseId(),
              placeholder: const Text('입고지를 선택하세요'),
              options: _controller.warehouses.entries.map((entry) => 
                ShadOption(
                  value: entry.key,
                  child: Text(entry.value),
                )
              ).toList(),
              selectedOptionBuilder: (context, value) => 
                Text(_controller.warehouses[value] ?? '선택하세요'),
              onChanged: (value) {
                setState(() {
                  _controller.selectedWarehouseId = value;
                });
                print('DEBUG [입고지 선택] value: $value, warehouses: ${_controller.warehouses.length}');
              },
            ),
            const SizedBox(height: 16),
            
            // 초기 재고 수량 (신규 등록 시에만 표시)
            if (!_controller.isEditMode)
              ShadInputFormField(
                controller: _initialStockController,
                label: const Text('초기 재고 수량 *'),
                placeholder: const Text('입고할 수량을 입력하세요'),
                description: const Text('장비 등록 시 자동으로 입고 처리됩니다'),
                keyboardType: TextInputType.number,
                inputFormatters: [
                  FilteringTextInputFormatter.digitsOnly,
                ],
                validator: (value) {
                  if (value.isEmpty) {
                    return '재고 수량은 필수입니다';
                  }
                  final quantity = int.tryParse(value);
                  if (quantity == null || quantity <= 0) {
                    return '1개 이상의 수량을 입력하세요';
                  }
                  return null;
                },
                onChanged: (value) {
                  final quantity = int.tryParse(value) ?? 1;
                  _controller.initialStock = quantity;
                },
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildPurchaseSection() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '구매 정보',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            Row(
              children: [
                // 구매일
                Expanded(
                  child: InkWell(
                    onTap: _controller.isFieldReadOnly('purchaseDate') ? null : () async {
                      final date = await showDatePicker(
                        context: context,
                        initialDate: _controller.purchaseDate ?? DateTime.now(),
                        firstDate: DateTime(2000),
                        lastDate: DateTime(2100),
                      );
                      if (date != null) {
                        setState(() {
                          _controller.purchaseDate = date;
                        });
                      }
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                      decoration: BoxDecoration(
                        border: Border.all(
                          color: _controller.isFieldReadOnly('purchaseDate') 
                              ? Colors.grey[300]! 
                              : Theme.of(context).dividerColor,
                        ),
                        borderRadius: BorderRadius.circular(6),
                        color: _controller.isFieldReadOnly('purchaseDate')
                            ? Colors.grey[50]
                            : null,
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            _controller.purchaseDate != null
                                ? '${_controller.purchaseDate!.year}-${_controller.purchaseDate!.month.toString().padLeft(2, '0')}-${_controller.purchaseDate!.day.toString().padLeft(2, '0')}'
                                : _controller.isFieldReadOnly('purchaseDate') ? '구매일 미설정' : '날짜 선택',
                            style: TextStyle(
                              color: _controller.isFieldReadOnly('purchaseDate') 
                                  ? Colors.grey[600] 
                                  : null,
                            ),
                          ),
                          Icon(
                            Icons.calendar_today,
                            size: 16,
                            color: _controller.isFieldReadOnly('purchaseDate') 
                                ? Colors.grey[600] : null,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                
                // 구매 가격
                Expanded(
                  child: ShadInputFormField(
                    controller: _purchasePriceController,
                    readOnly: _controller.isFieldReadOnly('purchasePrice'),
                    label: Text(_controller.isFieldReadOnly('purchasePrice') 
                        ? '구매 가격 🔒' : '구매 가격'),
                    placeholder: Text(_controller.isFieldReadOnly('purchasePrice') 
                        ? '수정불가' : '₩2,000,000'),
                    keyboardType: _controller.isFieldReadOnly('purchasePrice') 
                        ? null : TextInputType.number,
                    inputFormatters: _controller.isFieldReadOnly('purchasePrice') 
                        ? null : [CurrencyInputFormatter()], // 새로운 통화 포맷터
                    onChanged: (value) {
                      // 숫자만 추출하여 저장
                      final digitsOnly = value.replaceAll(RegExp(r'[^\d]'), '');
                      _controller.purchasePrice = int.tryParse(digitsOnly)?.toDouble();
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildRemarkSection() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '비고',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            ShadInputFormField(
              controller: _controller.remarkController,
              label: const Text('비고'),
              placeholder: const Text('비고사항을 입력하세요'),
              maxLines: 3,
            ),
          ],
        ),
      ),
    );
  }

}