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

class RuleInfo extends Equatable {
  final int ruleId;
  final String name;
  final String description;
  final bool enabled;
  final IconData icon;

  const RuleInfo({
    this.ruleId = 0,
    this.name = '',
    this.description = '',
    this.enabled = false,
    this.icon = Icons.tune,
  });

  RuleInfo copyWith({
    int? ruleId,
    String? name,
    String? description,
    bool? enabled,
    IconData? icon,
  }) {
    return RuleInfo(
      ruleId: ruleId ?? this.ruleId,
      name: name ?? this.name,
      description: description ?? this.description,
      enabled: enabled ?? this.enabled,
      icon: icon ?? this.icon,
    );
  }

  /// 兼容 View 层 rule.id 引用，返回 ruleId 的字符串形式
  String get id => ruleId.toString();

  @override
  List<Object?> get props => [ruleId, name, description, enabled, icon];
}

class RuleManagementState extends Equatable {
  final bool isLoading;
  final String? error;
  final bool isToggling; // 某个开关操作进行中
  final int togglingRuleId; // 正在操作的规则ID
  final List<RuleInfo> rules;

  const RuleManagementState({
    this.isLoading = false,
    this.error,
    this.isToggling = false,
    this.togglingRuleId = 0,
    this.rules = const [],
  });

  RuleManagementState copyWith({
    bool? isLoading,
    String? error,
    bool? isToggling,
    int? togglingRuleId,
    List<RuleInfo>? rules,
    bool clearError = false,
  }) {
    return RuleManagementState(
      isLoading: isLoading ?? this.isLoading,
      error: clearError ? null : (error ?? this.error),
      isToggling: isToggling ?? this.isToggling,
      togglingRuleId: togglingRuleId ?? this.togglingRuleId,
      rules: rules ?? this.rules,
    );
  }

  @override
  List<Object?> get props => [
        isLoading,
        error,
        isToggling,
        togglingRuleId,
        rules,
      ];
}
