import 'package:intl/intl.dart';

class ExercisePlan {
  int? exercisePlanId;
  late int customerId;
  late String name;
  String? description;
  late bool private;
  late DateTime? dateAdd;
  late DateTime dateUpd;
  String? type;
  int? exercisePlanTemplateId;

  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']))!;
    this.type = json['type'];
    this.exercisePlanTemplateId = json['exercisePlanTemplateId'];
  }

  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,
        "type": type,
        "exercisePlanTemplateId": exercisePlanTemplateId
      };
    } else {
      return {
        "exercisePlanId": exercisePlanId,
        "customerId": customerId,
        "name": name,
        "description": description,
        "private": private,
        "dateAdd": formattedDateAdd,
        "dateUpd": formattedDateUpd,
        "type": type,
        "exercisePlanTemplateId": exercisePlanTemplateId
      };
    }
  }

  @override
  String toString() {
    Map<String, dynamic> json = toJson();
    return json.toString();
  }
}
