import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:superport/screens/common/theme_shadcn.dart';
import 'package:superport/screens/common/templates/form_layout_template.dart';
import 'package:superport/utils/constants.dart';
import 'package:superport/utils/currency_formatter.dart';
import 'package:superport/screens/common/widgets/remark_input.dart';
import 'package:superport/core/widgets/category_cascade_form_field.dart';
import 'controllers/equipment_in_form_controller.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;

  @override
  void initState() {
    super.initState();
    _controller = EquipmentInFormController(equipmentInId: widget.equipmentInId);
    _controller.addListener(_onControllerUpdated);
    
    // 수정 모드일 때 데이터 로드
    if (_controller.isEditMode) {
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        await _controller.initializeForEdit();
      });
    }
  }

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

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

  // 유효한 제조사 값 반환 (드롭다운 assertion 오류 방지)
  String? _getValidManufacturer() {
    if (_controller.manufacturer.isEmpty) return null;
    
    final isValid = _controller.manufacturers.contains(_controller.manufacturer);
    print('DEBUG [_getValidManufacturer] manufacturer: "${_controller.manufacturer}", isValid: $isValid, available: ${_controller.manufacturers.take(5).toList()}');
    
    return isValid ? _controller.manufacturer : null;
  }

  // 유효한 모델명 값 반환 (드롭다운 assertion 오류 방지)
  String? _getValidModelName() {
    if (_controller.modelName.isEmpty) return null;
    
    final isValid = _controller.equipmentNames.contains(_controller.modelName);
    print('DEBUG [_getValidModelName] modelName: "${_controller.modelName}", isValid: $isValid, available: ${_controller.equipmentNames.take(5).toList()}');
    
    return isValid ? _controller.modelName : null;
  }

  // 유효한 구매처 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) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(_controller.error!),
          backgroundColor: Colors.red,
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // 간소화된 디버깅
    print('🎯 [UI] canSave: ${_controller.canSave} | 장비번호: "${_controller.equipmentNumber}" | 제조사: "${_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: CircularProgressIndicator())
          : 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 Card(
      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),
            
            // 장비 번호 (필수)
            TextFormField(
              initialValue: _controller.equipmentNumber,
              readOnly: _controller.isFieldReadOnly('equipmentNumber'),
              decoration: InputDecoration(
                labelText: _controller.isFieldReadOnly('equipmentNumber') 
                    ? '장비 번호 * 🔒' : '장비 번호 *',
                // 🔧 [UI FIX] ReadOnly 필드에서도 의미 있는 hintText 표시
                hintText: _controller.isFieldReadOnly('equipmentNumber') 
                    ? (_controller.equipmentNumber.isNotEmpty ? null : '장비 번호 없음')
                    : '장비 번호를 입력하세요',
                border: const OutlineInputBorder(),
                filled: _controller.isFieldReadOnly('equipmentNumber'),
                fillColor: _controller.isFieldReadOnly('equipmentNumber') 
                    ? Colors.grey[100] : null,
              ),
              style: TextStyle(
                color: _controller.isFieldReadOnly('equipmentNumber') 
                    ? Colors.grey[600] : null,
              ),
              validator: (value) {
                if (value?.trim().isEmpty ?? true) {
                  return '장비 번호는 필수입니다';
                }
                return null;
              },
              onChanged: _controller.isFieldReadOnly('equipmentNumber') ? null : (value) {
                _controller.equipmentNumber = value?.trim() ?? '';
                setState(() {});
                print('DEBUG [장비번호 입력] value: "$value", controller.equipmentNumber: "${_controller.equipmentNumber}"');
              },
              onSaved: (value) {
                _controller.equipmentNumber = value?.trim() ?? '';
              },
            ),
            const SizedBox(height: 16),
            
            // 제조사 (필수, Dropdown)
            DropdownButtonFormField<String>(
              value: _getValidManufacturer(),
              items: _controller.manufacturers.map((String manufacturer) {
                return DropdownMenuItem<String>(
                  value: manufacturer,
                  child: Text(
                    manufacturer,
                    style: TextStyle(
                      color: _controller.isFieldReadOnly('manufacturer') 
                          ? Colors.grey[600] : null,
                    ),
                  ),
                );
              }).toList(),
              decoration: InputDecoration(
                labelText: _controller.isFieldReadOnly('manufacturer') 
                    ? '제조사 * 🔒' : '제조사 *',
                hintText: _controller.isFieldReadOnly('manufacturer') 
                    ? '수정불가' : '제조사를 선택하세요',
                border: const OutlineInputBorder(),
                filled: _controller.isFieldReadOnly('manufacturer'),
                fillColor: _controller.isFieldReadOnly('manufacturer') 
                    ? Colors.grey[100] : null,
              ),
              validator: (value) {
                if (value?.trim().isEmpty ?? true) {
                  return '제조사는 필수입니다';
                }
                return null;
              },
              onChanged: _controller.isFieldReadOnly('manufacturer') ? null : (value) {
                setState(() {
                  _controller.manufacturer = value?.trim() ?? '';
                });
                print('🔧 DEBUG [제조사 선택] value: "$value", controller.manufacturer: "${_controller.manufacturer}", canSave: ${_controller.canSave}');
              },
            ),
            const SizedBox(height: 16),
            
            // 모델명 (선택, Dropdown)
            DropdownButtonFormField<String>(
              value: _getValidModelName(),
              items: _controller.equipmentNames.map((String equipmentName) {
                return DropdownMenuItem<String>(
                  value: equipmentName,
                  child: Text(
                    equipmentName,
                    style: TextStyle(
                      color: _controller.isFieldReadOnly('modelName') 
                          ? Colors.grey[600] : null,
                    ),
                  ),
                );
              }).toList(),
              decoration: InputDecoration(
                labelText: _controller.isFieldReadOnly('modelName') 
                    ? '모델명 🔒' : '모델명',
                hintText: _controller.isFieldReadOnly('modelName') 
                    ? '수정불가' : '모델명을 선택하세요',
                border: const OutlineInputBorder(),
                filled: _controller.isFieldReadOnly('modelName'),
                fillColor: _controller.isFieldReadOnly('modelName') 
                    ? Colors.grey[100] : null,
              ),
              onChanged: _controller.isFieldReadOnly('modelName') ? null : (value) {
                setState(() {
                  _controller.modelName = value?.trim() ?? '';
                });
                print('DEBUG [모델명 선택] value: "$value", controller.modelName: "${_controller.modelName}"');
              },
            ),
            const SizedBox(height: 16),
            
            // 시리얼 번호 (선택)
            TextFormField(
              initialValue: _controller.serialNumber,
              readOnly: _controller.isFieldReadOnly('serialNumber'),
              decoration: InputDecoration(
                labelText: _controller.isFieldReadOnly('serialNumber') 
                    ? '시리얼 번호 🔒' : '시리얼 번호',
                hintText: _controller.isFieldReadOnly('serialNumber') 
                    ? '수정불가' : '시리얼 번호를 입력하세요',
                border: const OutlineInputBorder(),
                filled: _controller.isFieldReadOnly('serialNumber'),
                fillColor: _controller.isFieldReadOnly('serialNumber') 
                    ? Colors.grey[100] : null,
              ),
              style: TextStyle(
                color: _controller.isFieldReadOnly('serialNumber') 
                    ? Colors.grey[600] : null,
              ),
              onChanged: _controller.isFieldReadOnly('serialNumber') ? null : (value) {
                _controller.serialNumber = value?.trim() ?? '';
                setState(() {});
                print('DEBUG [시리얼번호 입력] value: "$value", controller.serialNumber: "${_controller.serialNumber}"');
              },
              onSaved: (value) {
                _controller.serialNumber = value?.trim() ?? '';
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCategorySection() {
    return Card(
      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 Card(
      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),
            
            // 구매처 (드롭다운 전용)
            DropdownButtonFormField<int>(
              value: _getValidCompanyId(),
              items: _controller.companies.entries.map((entry) {
                return DropdownMenuItem<int>(
                  value: entry.key,
                  child: Text(entry.value),
                );
              }).toList(),
              decoration: const InputDecoration(
                labelText: '구매처',
                hintText: '구매처를 선택하세요',
                border: OutlineInputBorder(),
              ),
              onChanged: (value) {
                setState(() {
                  _controller.selectedCompanyId = value;
                });
                print('DEBUG [구매처 선택] value: $value, companies: ${_controller.companies.length}');
              },
              onSaved: (value) {
                _controller.selectedCompanyId = value;
              },
            ),
            const SizedBox(height: 16),
            
            // 입고지 (드롭다운 전용)
            DropdownButtonFormField<int>(
              value: _getValidWarehouseId(),
              items: _controller.warehouses.entries.map((entry) {
                return DropdownMenuItem<int>(
                  value: entry.key,
                  child: Text(entry.value),
                );
              }).toList(),
              decoration: const InputDecoration(
                labelText: '입고지',
                hintText: '입고지를 선택하세요',
                border: OutlineInputBorder(),
              ),
              onChanged: (value) {
                setState(() {
                  _controller.selectedWarehouseId = value;
                });
                print('DEBUG [입고지 선택] value: $value, warehouses: ${_controller.warehouses.length}');
              },
              onSaved: (value) {
                _controller.selectedWarehouseId = value;
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildPurchaseSection() {
    return Card(
      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: InputDecorator(
                      decoration: InputDecoration(
                        labelText: _controller.isFieldReadOnly('purchaseDate') 
                            ? '구매일 🔒' : '구매일',
                        suffixIcon: Icon(
                          Icons.calendar_today,
                          color: _controller.isFieldReadOnly('purchaseDate') 
                              ? Colors.grey[600] : null,
                        ),
                        border: const OutlineInputBorder(),
                        filled: _controller.isFieldReadOnly('purchaseDate'),
                        fillColor: _controller.isFieldReadOnly('purchaseDate') 
                            ? Colors.grey[100] : null,
                      ),
                      child: Text(
                        // 🔧 [UI FIX] ReadOnly 필드에서도 의미 있는 텍스트 표시
                        _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,
                        ),
                      ),
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                
                // 구매 가격
                Expanded(
                  child: TextFormField(
                    initialValue: _controller.purchasePrice != null 
                        ? CurrencyFormatter.formatKRW(_controller.purchasePrice)
                        : '',
                    readOnly: _controller.isFieldReadOnly('purchasePrice'),
                    decoration: InputDecoration(
                      labelText: _controller.isFieldReadOnly('purchasePrice') 
                          ? '구매 가격 🔒' : '구매 가격',
                      hintText: _controller.isFieldReadOnly('purchasePrice') 
                          ? '수정불가' : '₩2,000,000',
                      border: const OutlineInputBorder(),
                      filled: _controller.isFieldReadOnly('purchasePrice'),
                      fillColor: _controller.isFieldReadOnly('purchasePrice') 
                          ? Colors.grey[100] : null,
                    ),
                    style: TextStyle(
                      color: _controller.isFieldReadOnly('purchasePrice') 
                          ? Colors.grey[600] : null,
                    ),
                    keyboardType: _controller.isFieldReadOnly('purchasePrice') 
                        ? null : TextInputType.number,
                    inputFormatters: _controller.isFieldReadOnly('purchasePrice') 
                        ? null : [KRWTextInputFormatter()],
                    onSaved: (value) {
                      _controller.purchasePrice = CurrencyFormatter.parseKRW(value);
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildRemarkSection() {
    return Card(
      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),
            
            TextFormField(
              controller: _controller.remarkController,
              decoration: const InputDecoration(
                labelText: '비고',
                hintText: '비고사항을 입력하세요',
                border: OutlineInputBorder(),
              ),
              maxLines: 3,
            ),
          ],
        ),
      ),
    );
  }

}