import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../model/dropdown_option.dart';
import '../model/enquiry_model.dart';
import '../widgets/searchable_dropdown.dart';

class EnquiryRow extends StatefulWidget {
  final EnquiryModel enquiry;
  final Function(EnquiryModel) onUpdate;
  final List<DropdownOption> partners;
  final List<DropdownOption> insurers;
  final List<DropdownOption> assignees;

  const EnquiryRow({
    super.key,
    required this.enquiry,
    required this.onUpdate,
    required this.partners,
    required this.insurers,
    required this.assignees,
  });

  @override
  State<EnquiryRow> createState() => _EnquiryRowState();
}

class _EnquiryRowState extends State<EnquiryRow> {
  bool _isEditMode = false;
  late EnquiryModel _editedEnquiry;

  @override
  void initState() {
    super.initState();
    _editedEnquiry = widget.enquiry;
  }

  void _toggleEditMode() {
    setState(() {
      if (_isEditMode) {
        widget.onUpdate(_editedEnquiry);
      }
      _isEditMode = !_isEditMode;
    });
  }

  Widget _buildCell(String value, {Function(String)? onChanged}) {
    if (_isEditMode && onChanged != null) {
      return Container(
        padding: const EdgeInsets.all(4),
        decoration: BoxDecoration(
          color: const Color(0xFFe3f2fd),
          border: Border.all(color: const Color(0xFF4a90e2), width: 1),
          borderRadius: BorderRadius.circular(3),
        ),
        child: TextFormField(
          initialValue: value,
          style: const TextStyle(fontSize: 12),
          decoration: const InputDecoration(
            border: InputBorder.none,
            isDense: true,
            contentPadding: EdgeInsets.all(4),
          ),
          onChanged: onChanged,
        ),
      );
    }
    return Text(value, style: const TextStyle(fontSize: 12));
  }

  Widget _buildDropdownCell(
    String value,
    List<DropdownOption> options,
    Function(String?) onChanged,
  ) {
    if (_isEditMode) {
      return Container(
        constraints: const BoxConstraints(minHeight: 36),
        child: SearchableDropdown(
          label: '',
          value: value,
          items: options,
          onChanged: onChanged,
        ),
      );
    }
    return Text(value, style: const TextStyle(fontSize: 12));
  }

  @override
  Widget build(BuildContext context) {
    final dateFormat = DateFormat('dd-MM-yyyy\nhh:mm a');

    return SelectionArea(
        child:Container(
      decoration: BoxDecoration(
        color: _isEditMode ? const Color(0xFFf8f9fa) : Colors.white,
        border: const Border(bottom: BorderSide(color: Color(0xFFf0f0f0))),
      ),
      child: IntrinsicHeight(
        child: Row(
          children: [
            SizedBox(
              width: 120,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: Text(
                  dateFormat.format(widget.enquiry.receivedDate),
                  style: const TextStyle(fontSize: 12),
                ),
              ),
            ),
            SizedBox(
              width: 100,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildDropdownCell(
                  _editedEnquiry.partner,
                  widget.partners,
                  (v) => setState(
                    () => _editedEnquiry = _editedEnquiry.copyWith(partner: v),
                  ),
                ),
              ),
            ),
            SizedBox(
              width: 100,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildDropdownCell(
                  _editedEnquiry.assignedTo,
                  widget.assignees,
                  (v) => setState(
                    () =>
                        _editedEnquiry = _editedEnquiry.copyWith(assignedTo: v),
                  ),
                ),
              ),
            ),
            SizedBox(
              width: 100,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(_editedEnquiry.broker),
              ),
            ),
            SizedBox(
              width: 100,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildDropdownCell(
                  _editedEnquiry.insurer,
                  widget.insurers,
                  (v) => setState(
                    () => _editedEnquiry = _editedEnquiry.copyWith(insurer: v),
                  ),
                ),
              ),
            ),
            SizedBox(
              width: 150,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.insuredName,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(insuredName: v),
                ),
              ),
            ),
            SizedBox(
              width: 120,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.vehicleNo,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(vehicleNo: v),
                ),
              ),
            ),
            SizedBox(
              width: 120,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.vehicleType,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(vehicleType: v),
                ),
              ),
            ),
            SizedBox(
              width: 150,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.email,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(email: v),
                ),
              ),
            ),
            SizedBox(
              width: 100,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.mobile,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(mobile: v),
                ),
              ),
            ),
            SizedBox(
              width: 150,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: _buildCell(
                  _editedEnquiry.remarks,
                  onChanged: (v) =>
                      _editedEnquiry = _editedEnquiry.copyWith(remarks: v),
                ),
              ),
            ),
            SizedBox(
              width: 80,
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: Row(
                  children: [
                    IconButton(
                      icon: Text(_isEditMode ? '💾' : '✏️'),
                      onPressed: _toggleEditMode,
                      tooltip: _isEditMode ? 'Save' : 'Edit',
                      padding: EdgeInsets.zero,
                      constraints: const BoxConstraints(),
                    ),
                    const SizedBox(width: 8),
                    IconButton(
                      icon: const Text('📄'),
                      onPressed: () {},
                      tooltip: 'Documents',
                      padding: EdgeInsets.zero,
                      constraints: const BoxConstraints(),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ));
  }
}
