/// Data Model: Design Request Model
///
/// JSON serialization model for design request API responses.
library;

import 'package:worker/features/showrooms/data/models/sample_project_model.dart';
import 'package:worker/features/showrooms/domain/entities/design_request.dart';

/// Design Request Model
///
/// Handles JSON serialization/deserialization for API communication.
class DesignRequestModel {
  /// Unique request identifier (API: name)
  final String name;

  /// Request subject/title (API: subject)
  final String subject;

  /// Request description - may contain HTML (API: description)
  final String? description;

  /// Deadline date string (API: dateline)
  final String? dateline;

  /// Status display text (API: status)
  final String status;

  /// Status color code (API: status_color)
  final String statusColor;

  /// List of attached files (API: files_list) - available in detail
  final List<ProjectFileModel> filesList;

  const DesignRequestModel({
    required this.name,
    required this.subject,
    this.description,
    this.dateline,
    required this.status,
    required this.statusColor,
    this.filesList = const [],
  });

  /// Create model from JSON map
  factory DesignRequestModel.fromJson(Map<String, dynamic> json) {
    final filesListJson = json['files_list'] as List<dynamic>?;

    return DesignRequestModel(
      name: json['name'] as String? ?? '',
      subject: json['subject'] as String? ?? '',
      description: json['description'] as String?,
      dateline: json['dateline'] as String?,
      status: json['status'] as String? ?? '',
      statusColor: json['status_color'] as String? ?? '',
      filesList: filesListJson != null
          ? filesListJson
              .map((f) => ProjectFileModel.fromJson(f as Map<String, dynamic>))
              .toList()
          : [],
    );
  }

  /// Convert model to JSON map
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'subject': subject,
      'description': description,
      'dateline': dateline,
      'status': status,
      'status_color': statusColor,
      'files_list': filesList.map((f) => f.toJson()).toList(),
    };
  }

  /// Convert to domain entity
  DesignRequest toEntity() {
    return DesignRequest(
      id: name,
      subject: subject,
      description: description,
      dateline: dateline,
      statusText: status,
      statusColor: statusColor,
      filesList: filesList.map((f) => f.toEntity()).toList(),
    );
  }

  /// Create model from domain entity
  factory DesignRequestModel.fromEntity(DesignRequest entity) {
    return DesignRequestModel(
      name: entity.id,
      subject: entity.subject,
      description: entity.description,
      dateline: entity.dateline,
      status: entity.statusText,
      statusColor: entity.statusColor,
      filesList: entity.filesList
          .map((f) => ProjectFileModel(name: f.id, fileUrl: f.fileUrl))
          .toList(),
    );
  }
}
