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.parse(json['timestamp']),
    );
  }

  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 int plannedTask;
  final int 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,
      completedTask: json['completedTask'] ?? 0,
      date: DateTime.parse(json['date']),
    );
  }

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

/// Chart-friendly model
class ChartTaskData {
  final DateTime date; // ✅ actual date for chart
  final String dateLabel; // optional: for display
  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,
      completed: data.completedTask,
    );
  }
}
