import 'package:flutter/cupertino.dart';
import 'package:test_sa/extensions/context_extension.dart';

class SearchAllRequestsModel {
  SearchByRequestNumberModel? requestNumber; // Now nullable
  SearchByAssetNameModel? assetName; // Now nullable
  SearchByAssetNoModel? assetNo; // Now nullable
  SearchByManufactureModel? manufacture; // Now nullable
  SearchBySNModel? sn; // Now nullable
  SearchByRequestModel? model; // Now nullable
  SearchAllRequestsBaseModel? searchBySelectedValue; // Now nullable
  List<int>? statuses; // Now nullable
  List<int>? typeTransaction; // Now nullable
  DateTime? startDate, endDate; // Now nullable
  bool isArchived = false;
  String? selectedSortOption;

  SearchAllRequestsModel({
    this.requestNumber,
    this.assetName,
    this.assetNo,
    this.manufacture,
    this.sn,
    this.model,
    this.statuses,
    this.typeTransaction,
    this.startDate,
    this.endDate,
    this.isArchived = false,
    this.selectedSortOption,
  });

//older function
  // void resetSearchValues() {
  //   requestNumber?.controller?.text = "";
  //   assetName?.controller?.text = "";
  //   assetNo?.controller?.text = "";
  //   manufacture?.controller?.text = "";
  //   sn?.controller?.text = "";
  //   model?.controller?.text = "";
  //   isArchived = false;
  // }

  void resetSearchValuesAndController({String? except}) {
    final field = except?.toLowerCase();
    debugPrint("selected  tag is $field");

    if (field != 'requestnumber') {
      requestNumber?.controller?.text = "";
      requestNumber?.value = null;
    }

    if (field != 'assetname') {
      assetName?.controller?.text = "";
      assetName?.value = null;
    }

    if (field != 'assetno') {
      assetNo?.controller?.text = "";
      assetNo?.value = null;
    }

    if (field != 'manufacture') {
      manufacture?.controller?.text = "";
      manufacture?.value = null;
    }

    if (field != 'sn') {
      sn?.controller?.text = "";
      sn?.value = null;
    }

    if (field != 'model') {
      model?.controller?.text = "";
      model?.value = null;
    }

    isArchived = false;
  }

  Map<String, dynamic> toJson({
    List<int>? typeTransaction,
    List<int>? statusTransaction,
    List<int>? priority,
    List<int>? displayData,
  }) {
    Map<String, dynamic> json = {};

    // Add computed fields if provided
    if (typeTransaction != null) {
      json["typeTransaction"] = typeTransaction;
    } else if (this.typeTransaction != null && this.typeTransaction!.isNotEmpty) {
      json["typeTransaction"] = this.typeTransaction;
    }

    if (statusTransaction != null) {
      json["statusTransaction"] = statusTransaction;
    } else if (statuses != null && statuses!.isNotEmpty) {
      json["statusTransaction"] = statuses;
    }

    if (priority != null) {
      json["priority"] = priority;
    } else {
      json["priority"] = [];
    }

    if (displayData != null) {
      json["displayData"] = displayData;
    } else {
      json["displayData"] = [];
    }

    // Add search fields
    if (requestNumber?.value?.isNotEmpty ?? false) {
      json["requestNumber"] = requestNumber!.value;
    }

    if (assetName?.value?.isNotEmpty ?? false) {
      json["assetName"] = assetName!.value;
    }

    if (assetNo?.value?.isNotEmpty ?? false) {
      json["assetNumber"] = assetNo!.value;
    }

    if (sn?.value?.isNotEmpty ?? false) {
      json["assetSerialNo"] = sn!.value;
    }

    if (model?.value?.isNotEmpty ?? false) {
      json["model"] = model!.value;
    }

    if (manufacture?.value?.isNotEmpty ?? false) {
      json["manufacturer"] = manufacture!.value;
    }

    if (startDate != null) {
      json["from"] = startDate!.toIso8601String();
    }

    if (endDate != null) {
      json["to"] = endDate!.toIso8601String();
    }

    return json;
  }
}

abstract class SearchAllRequestsBaseModel {
  String? label; // Now nullable
  String? value; // Now nullable
  BuildContext? context; // Now nullable
  TextInputType inputType;
  TextEditingController? controller; // Now nullable

  SearchAllRequestsBaseModel(this.context, {this.controller, this.label, this.value, this.inputType = TextInputType.text});
}

class SearchByRequestNumberModel extends SearchAllRequestsBaseModel {
  SearchByRequestNumberModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.woNumber, value: value);
}

class SearchByAssetNameModel extends SearchAllRequestsBaseModel {
  SearchByAssetNameModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.assetName, value: value);
}

class SearchByAssetNoModel extends SearchAllRequestsBaseModel {
  SearchByAssetNoModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.assetNo, value: value);
}

class SearchByManufactureModel extends SearchAllRequestsBaseModel {
  SearchByManufactureModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.manufacture, value: value);
}

class SearchBySNModel extends SearchAllRequestsBaseModel {
  SearchBySNModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.sn, value: value);
}

class SearchByRequestModel extends SearchAllRequestsBaseModel {
  SearchByRequestModel(BuildContext context, {TextEditingController? controller, String? value}) // Parameters now nullable
      : super(context, controller: controller, label: context.translation.model, value: value);
}
