import 'package:intl/intl.dart';

class ProjectResponse {
  final bool success;
  final String message;
  final List<ProjectData> data;
  final dynamic errors;
  final int statusCode;
  final DateTime timestamp;

  ProjectResponse({
    required this.success,
    required this.message,
    required this.data,
    this.errors,
    required this.statusCode,
    required this.timestamp,
  });

  factory ProjectResponse.fromJson(Map<String, dynamic> json) {
    return ProjectResponse(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: (json['data'] as List<dynamic>?)
              ?.map((e) => ProjectData.fromJson(e))
              .toList() ??
          [],
      errors: json['errors'],
      statusCode: json['statusCode'] ?? 0,
      timestamp: DateTime.tryParse(json['timestamp'] ?? '') ?? DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data.map((e) => e.toJson()).toList(),
      'errors': errors,
      'statusCode': statusCode,
      'timestamp': timestamp.toIso8601String(),
    };
  }
}

class ProjectData {
  final String projectId;
  final String projectName;
  final double plannedTask;
  final double completedTask;
  final DateTime date;

  ProjectData({
    required this.projectId,
    required this.projectName,
    required this.plannedTask,
    required this.completedTask,
    required this.date,
  });

  factory ProjectData.fromJson(Map<String, dynamic> json) {
    return ProjectData(
      projectId: json['projectId'] ?? '',
      projectName: json['projectName'] ?? '',
      plannedTask: (json['plannedTask'] ?? 0).toDouble(),
      completedTask: (json['completedTask'] ?? 0).toDouble(),
      date: DateTime.tryParse(json['date'] ?? '') ?? DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'projectId': projectId,
      'projectName': projectName,
      'plannedTask': plannedTask,
      'completedTask': completedTask,
      'date': date.toIso8601String(),
    };
  }
}

/// Chart-friendly model
class ChartTaskData {
  final DateTime date;
  final String dateLabel;
  final int planned;
  final int completed;

  ChartTaskData({
    required this.date,
    required this.dateLabel,
    required this.planned,
    required this.completed,
  });

  factory ChartTaskData.fromProjectData(ProjectData data) {
    return ChartTaskData(
      date: data.date,
      dateLabel: DateFormat('dd-MM').format(data.date),
      planned: data.plannedTask.round(), 
      completed: data.completedTask.round(),
    );
  }
}
