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 '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;
  final Map<String, dynamic>? preloadedData; // 사전 로드된 데이터

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

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

class _EquipmentInFormScreenState extends State<EquipmentInFormScreen> {
  late EquipmentInFormController _controller;
  late TextEditingController _serialNumberController;
  late TextEditingController _barcodeController;
  late TextEditingController _initialStockController;
  late TextEditingController _purchasePriceController;
  Future<void>? _initFuture;

  @override
  void initState() {
    super.initState();
    
    // preloadedData가 있으면 전달, 없으면 일반 초기화
    if (widget.preloadedData != null) {
      _controller = EquipmentInFormController.withPreloadedData(
        preloadedData: widget.preloadedData!,
      );
      _initFuture = Future.value(); // 데이터가 이미 있으므로 즉시 완료
    } else {
      _controller = EquipmentInFormController(equipmentInId: widget.equipmentInId);
      // 수정 모드일 때 데이터 로드를 Future로 처리
      if (_controller.isEditMode) {
        _initFuture = _initializeEditMode();
      } else {
        _initFuture = Future.value(); // 신규 모드는 즉시 완료
      }
    }
    
    _controller.addListener(_onControllerUpdated);
    
    // TextEditingController 초기화
    _serialNumberController = TextEditingController(text: _controller.serialNumber);
    _barcodeController = TextEditingController(text: _controller.barcode);
    _initialStockController = TextEditingController(text: _controller.initialStock.toString());
    _purchasePriceController = TextEditingController(
      text: _controller.purchasePrice != null 
          ? CurrencyFormatter.formatKRW(_controller.purchasePrice)
          : ''
    );
  }
  
  Future<void> _initializeEditMode() async {
    await _controller.initializeForEdit();
    // 데이터 로드 후 컨트롤러 업데이트
    setState(() {
      _serialNumberController.text = _controller.serialNumber;
      _barcodeController.text = _controller.barcode;
      _purchasePriceController.text = _controller.purchasePrice != null 
          ? CurrencyFormatter.formatKRW(_controller.purchasePrice)
          : '';
    });
  }

  @override
  void dispose() {
    _controller.removeListener(_onControllerUpdated);
    _controller.dispose();
    _serialNumberController.dispose();
    _barcodeController.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;
    
    // 데이터 로딩 중이면 선택한 값을 유지 (validation 스킵)
    if (_controller.warehouses.isEmpty) {
      print('DEBUG [_getValidWarehouseId] 데이터 로딩 중 - 선택한 값 유지: ${_controller.selectedWarehouseId}');
      return _controller.selectedWarehouseId;
    }
    
    final isValid = _controller.warehouses.containsKey(_controller.selectedWarehouseId);
    print('DEBUG [_getValidWarehouseId] selectedWarehouseId: ${_controller.selectedWarehouseId}, isValid: $isValid, available warehouses: ${_controller.warehouses.length}');
    
    // 유효하지 않더라도 선택한 값을 유지 (사용자 선택 존중)
    if (!isValid) {
      print('WARNING [_getValidWarehouseId] 선택한 창고가 목록에 없음 - 그래도 사용자 선택 유지');
    }
    return _controller.selectedWarehouseId;
  }

  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 FutureBuilder<void>(
      future: _initFuture,
      builder: (context, snapshot) {
        // 수정 모드에서 데이터 로딩 중일 때 로딩 화면 표시
        if (_controller.isEditMode && snapshot.connectionState != ConnectionState.done) {
          return FormLayoutTemplate(
            title: '장비 정보 로딩 중...',
            onSave: null,
            onCancel: () => Navigator.of(context).pop(),
            isLoading: false,
            child: const Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ShadProgress(),
                  SizedBox(height: 16),
                  Text('장비 정보를 불러오는 중입니다...'),
                ],
              ),
            ),
          );
        }
        
        // 데이터 로드 완료 또는 신규 모드
        return FormLayoutTemplate(
          title: _controller.isEditMode ? '장비 수정' : '장비 입고',
          onSave: _controller.canSave && !_controller.isSaving ? _onSave : null,
          onCancel: () => Navigator.of(context).pop(),
          isLoading: _controller.isSaving,
          child: Form(
            key: _controller.formKey,
            child: SingleChildScrollView(
              padding: const EdgeInsets.only(bottom: 24),
              child: Column(
                children: [
                  _buildBasicFields(),
                  const SizedBox(height: 24),
                  _buildLocationSection(),
                  const SizedBox(height: 24),
                  _buildPurchaseSection(),
                  const SizedBox(height: 24),
                  _buildWarrantySection(),
                  const SizedBox(height: 24),
                  _buildRemarkSection(),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildBasicFields() {
    return ShadCard(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              '기본 정보',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            
            // 1. 제조사/모델 정보 (수정 모드: 읽기 전용, 생성 모드: 선택)
            if (_controller.isEditMode)
              ..._buildReadOnlyVendorModel()
            else
              Container(
                width: double.infinity,
                child: EquipmentVendorModelSelector(
                  initialVendorId: _controller.vendorId,
                  initialModelId: _controller.modelsId,
                  onChanged: _controller.onVendorModelChanged,
                  isReadOnly: false,
                ),
              ),
            const SizedBox(height: 16),
            
            // 2. 장비 번호 (필수)
            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) {
                  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),
            
            // 3. 바코드 (선택사항)
            ShadInputFormField(
              controller: _barcodeController,
              placeholder: const Text('바코드를 입력하세요'),
              label: const Text('바코드'),
              onChanged: (value) {
                _controller.barcode = value.trim();
                print('DEBUG [바코드 입력] value: "$value", controller.barcode: "${_controller.barcode}"');
              },
            ),
          ],
        ),
      ),
    );
  }


  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),
            
            // 구매처 (수정 모드: 읽기 전용, 생성 모드: 선택)
            if (_controller.isEditMode)
              _buildReadOnlyCompany()
            else
              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) {
                  if (_controller.companies.isEmpty) {
                    return const Text('로딩중...');
                  }
                  return Text(_controller.companies[value] ?? '선택하세요');
                },
                onChanged: (value) {
                  setState(() {
                    _controller.selectedCompanyId = value;
                  });
                  print('DEBUG [구매처 선택] value: $value, companies: ${_controller.companies.length}');
                },
              ),
            const SizedBox(height: 16),
            
            // 입고지 (수정 모드: 읽기 전용, 생성 모드: 선택 가능)
            if (_controller.isEditMode)
              // 수정 모드: 현재 창고 정보만 표시 (변경 불가)
              ShadInputFormField(
                readOnly: true,
                placeholder: Text(_controller.warehouses.isNotEmpty && _controller.selectedWarehouseId != null
                    ? '${_controller.warehouses[_controller.selectedWarehouseId!] ?? "창고 정보 없음"} 🔒'
                    : '창고 정보 로딩중... 🔒'),
                label: const Text('입고지 * (수정 불가)'),
              )
            else
              // 생성 모드: 창고 선택 가능
              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) {
                  // warehouses가 비어있거나 해당 value가 없는 경우 처리
                  if (_controller.warehouses.isEmpty) {
                    return const Text('로딩중...');
                  }
                  return Text(_controller.warehouses[value] ?? '선택하세요');
                },
                onChanged: (value) {
                  setState(() {
                    _controller.selectedWarehouseId = value;
                  });
                  print('✅ [입고지 선택] 선택한 값: $value');
                  print('📦 [입고지 선택] 사용 가능한 창고 수: ${_controller.warehouses.length}');
                  print('🔍 [입고지 선택] 최종 저장될 값: ${_controller.selectedWarehouseId}');
                  
                  // 선택한 창고 이름도 출력
                  if (_controller.warehouses.isNotEmpty && value != null) {
                    final warehouseName = _controller.warehouses[value] ?? '알 수 없음';
                    print('🏪 [입고지 선택] 선택한 창고 이름: $warehouseName');
                  }
                },
              ),
            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 _buildWarrantySection() {
    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.warrantyNumberController,
              label: const Text('워런티 번호 *'),
              placeholder: const Text('워런티 번호를 입력하세요'),
              validator: (value) {
                if (value.trim().isEmpty) {
                  return '워런티 번호는 필수입니다';
                }
                return null;
              },
            ),
            const SizedBox(height: 16),
            
            Row(
              children: [
                // 워런티 시작일 (필수)
                Expanded(
                  child: InkWell(
                    onTap: () async {
                      final date = await showDatePicker(
                        context: context,
                        initialDate: _controller.warrantyStartDate,
                        firstDate: DateTime(2000),
                        lastDate: DateTime(2100),
                      );
                      if (date != null) {
                        setState(() {
                          _controller.warrantyStartDate = date;
                        });
                      }
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                      decoration: BoxDecoration(
                        border: Border.all(color: Theme.of(context).dividerColor),
                        borderRadius: BorderRadius.circular(6),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '${_controller.warrantyStartDate.year}-${_controller.warrantyStartDate.month.toString().padLeft(2, '0')}-${_controller.warrantyStartDate.day.toString().padLeft(2, '0')}',
                          ),
                          const Icon(Icons.calendar_today, size: 16),
                        ],
                      ),
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                
                // 워런티 만료일 (필수)
                Expanded(
                  child: InkWell(
                    onTap: () async {
                      final date = await showDatePicker(
                        context: context,
                        initialDate: _controller.warrantyEndDate,
                        firstDate: _controller.warrantyStartDate,
                        lastDate: DateTime(2100),
                      );
                      if (date != null) {
                        setState(() {
                          _controller.warrantyEndDate = date;
                        });
                      }
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                      decoration: BoxDecoration(
                        border: Border.all(color: Theme.of(context).dividerColor),
                        borderRadius: BorderRadius.circular(6),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '${_controller.warrantyEndDate.year}-${_controller.warrantyEndDate.month.toString().padLeft(2, '0')}-${_controller.warrantyEndDate.day.toString().padLeft(2, '0')}',
                          ),
                          const Icon(Icons.calendar_today, size: 16),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),
            
            // 워런티 기간 표시
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.surface,
                borderRadius: BorderRadius.circular(6),
                border: Border.all(color: Theme.of(context).dividerColor),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.info_outline,
                    size: 16,
                    color: Theme.of(context).colorScheme.primary,
                  ),
                  const SizedBox(width: 8),
                  Text(
                    '워런티 기간: ${_controller.getWarrantyPeriodSummary()}',
                    style: Theme.of(context).textTheme.bodySmall,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  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,
            ),
          ],
        ),
      ),
    );
  }

  /// 읽기 전용 구매처 정보 표시 (백엔드 JOIN 데이터 활용)
  Widget _buildReadOnlyCompany() {
    // preloadedEquipment가 있으면 companyName 사용, 없으면 기본값
    final companyName = _controller.preloadedEquipment?.companyName ?? 
                       (_controller.companies.isNotEmpty && _controller.selectedCompanyId != null
                        ? _controller.companies[_controller.selectedCompanyId] 
                        : '구매처 정보 없음');
    
    return ShadInputFormField(
      readOnly: true,
      label: const Text('구매처 🔒'),
      initialValue: companyName,
    );
  }

  /// 읽기 전용 제조사/모델 정보 표시 (백엔드 JOIN 데이터 활용)
  List<Widget> _buildReadOnlyVendorModel() {
    return [
      // 제조사 (읽기 전용)
      ShadInputFormField(
        readOnly: true,
        label: const Text('제조사 * 🔒'),
        initialValue: _controller.manufacturer.isNotEmpty 
            ? _controller.manufacturer 
            : '제조사 정보 없음',
      ),
      const SizedBox(height: 16),
      
      // 모델 (읽기 전용)
      ShadInputFormField(
        readOnly: true,
        label: const Text('모델 * 🔒'),
        initialValue: _controller.name.isNotEmpty 
            ? _controller.name 
            : '모델 정보 없음',
      ),
    ];
  }

}
