import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/schedule_model.dart';

class ScheduleEntry {
  final String category;
  final String time;
  final Status function;
  final List<String> days;
  final String? scheduleId;

  ScheduleEntry({
    required this.category,
    required this.time,
    required this.function,
    required this.days,
    this.scheduleId,
  });

  @override
  String toString() {
    return 'ScheduleEntry(category: $category, time: $time, function: $function, days: $days)';
  }

  ScheduleEntry copyWith({
    String? category,
    String? time,
    Status? function,
    List<String>? days,
  }) {
    return ScheduleEntry(
      category: category ?? this.category,
      time: time ?? this.time,
      function: function ?? this.function,
      days: days ?? this.days,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'scheduleId': scheduleId,
      'category': category,
      'time': time,
      'function': function.toMap(),
      'days': days,
    };
  }

  factory ScheduleEntry.fromMap(Map<String, dynamic> map) {
    return ScheduleEntry(
      category: map['category'] ?? '',
      time: map['time'] ?? '',
      function: Status.fromMap(map['function']),
      days: List<String>.from(map['days']),
    );
  }

  String toJson() => json.encode(toMap());

  factory ScheduleEntry.fromJson(String source) =>
      ScheduleEntry.fromMap(json.decode(source));

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is ScheduleEntry &&
        other.category == category &&
        other.time == time &&
        other.function == function &&
        listEquals(other.days, days);
  }

  @override
  int get hashCode {
    return category.hashCode ^
        time.hashCode ^
        function.hashCode ^
        days.hashCode;
  }

  // Existing properties and methods

  // Add the fromScheduleModel method

  static ScheduleEntry fromScheduleModel(ScheduleModel scheduleModel) {
    return ScheduleEntry(
      days: scheduleModel.days,
      time: scheduleModel.time,
      function: scheduleModel.function,
      category: scheduleModel.category,
      scheduleId: scheduleModel.scheduleId,
    );
  }
}
