import 'package:flutter/foundation.dart';

@immutable
class TaxSetting {
  const TaxSetting({
    required this.id,
    required this.rate,
    required this.roundingMode,
    required this.updatedAt,
  });

  final String id;
  final double rate;
  final String roundingMode;
  final DateTime updatedAt;

  TaxSetting copyWith({
    String? id,
    double? rate,
    String? roundingMode,
    DateTime? updatedAt,
  }) {
    return TaxSetting(
      id: id ?? this.id,
      rate: rate ?? this.rate,
      roundingMode: roundingMode ?? this.roundingMode,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  factory TaxSetting.fromMap(Map<String, Object?> map) {
    return TaxSetting(
      id: map['id'] as String,
      rate: (map['rate'] as num).toDouble(),
      roundingMode: map['rounding_mode'] as String? ?? 'round',
      updatedAt: DateTime.parse(map['updated_at'] as String),
    );
  }

  Map<String, Object?> toMap() {
    return {
      'id': id,
      'rate': rate,
      'rounding_mode': roundingMode,
      'updated_at': updatedAt.toIso8601String(),
    };
  }
}
