enum ChannelType {
  woocommerce,
  square,
  etsy,
}

abstract class Channel {
  String id;
  String name;
  ChannelType type;

  Channel({required this.id, required this.name, required this.type});

  Map<String, dynamic> toJson();
  
  factory Channel.fromJson(Map<String, dynamic> json) {
    // Factory filtering based on type
    final typeStr = json['type'] as String;
    if (typeStr == 'woocommerce') {
      return WooCommerceChannel.fromJson(json);
    } else if (typeStr == 'square') {
      return SquareChannel.fromJson(json);
    }
    throw Exception('Unknown channel type: $typeStr');
  }
}

class WooCommerceChannel extends Channel {
  final String url;
  final String consumerKey;
  final String consumerSecret;

  WooCommerceChannel({
    required super.id,
    required super.name,
    required this.url,
    required this.consumerKey,
    required this.consumerSecret,
  }) : super(type: ChannelType.woocommerce);

  @override
  Map<String, dynamic> toJson() => {
    'id': id,
    'name': name,
    'type': 'woocommerce',
    'url': url,
    'consumerKey': consumerKey,
    'consumerSecret': consumerSecret,
  };

  factory WooCommerceChannel.fromJson(Map<String, dynamic> json) {
    return WooCommerceChannel(
      id: json['id'],
      name: json['name'],
      url: json['url'],
      consumerKey: json['consumerKey'],
      consumerSecret: json['consumerSecret'],
    );
  }
}

class SquareChannel extends Channel {
  final String applicationId;
  final String accessToken;
  final String locationId;

  SquareChannel({
    required super.id,
    required super.name,
    required this.applicationId,
    required this.accessToken,
    required this.locationId,
  }) : super(type: ChannelType.square);

  @override
  Map<String, dynamic> toJson() => {
    'id': id,
    'name': name,
    'type': 'square',
    'applicationId': applicationId,
    'accessToken': accessToken,
    'locationId': locationId,
  };

  factory SquareChannel.fromJson(Map<String, dynamic> json) {
    return SquareChannel(
      id: json['id'],
      name: json['name'],
      applicationId: json['applicationId'],
      accessToken: json['accessToken'],
      locationId: json['locationId'] ?? '',
    );
  }
}
