import 'package:intl/intl.dart';

class ExercisePlan {
  int exercisePlanId;
  int customerId;
  String name;
  String description;
  bool private;
  DateTime dateAdd;
  DateTime dateUpd;

  ExercisePlan(String name, int customerId) {
    this.customerId = customerId;
    this.name = name;
    this.dateUpd = DateTime.now();
  }

  ExercisePlan.fromJson(Map json) {
    this.exercisePlanId = json['exercisePlanId'];
    this.customerId = json['customerId'];
    this.name = json['name'];
    this.private = json['private'];
    this.description = json['description'];
    this.dateAdd = json['dateAdd'] == null ? null : DateTime.parse(json['dateAdd']);
    this.dateUpd = json['dateUpd'] == null ? null : DateTime.parse(json['dateUpd']);
  }

  Map<String, dynamic> toJson() {
    String formattedDateAdd;
    if (dateAdd != null) {
      formattedDateAdd = DateFormat('yyyy-MM-dd HH:mm').format(dateAdd);
    }
    String formattedDateUpd = DateFormat('yyyy-MM-dd HH:mm').format(dateUpd);

    if (exercisePlanId == null) {
      return {
        "customerId": customerId,
        "name": name,
        "description": description,
        "private": private,
        "dateAdd": formattedDateAdd,
        "dateUpd": formattedDateUpd,
      };
    } else {
      return {
        "exercisePlanId": exercisePlanId,
        "customerId": customerId,
        "name": name,
        "description": description,
        "private": private,
        "dateAdd": formattedDateAdd,
        "dateUpd": formattedDateUpd,
      };
    }
  }
}