/// Data Model: Sample Project Model
///
/// JSON serialization model for sample project API responses.
library;

import 'package:worker/features/showrooms/domain/entities/sample_project.dart';

/// Project File Model
///
/// Shared model for file attachments used by:
/// - SampleProjectModel (model houses)
/// - DesignRequestModel (design requests)
class ProjectFileModel {
  /// Unique file identifier (API: name)
  final String name;

  /// Full URL to the file (API: file_url)
  final String fileUrl;

  const ProjectFileModel({
    required this.name,
    required this.fileUrl,
  });

  /// Create model from JSON map
  factory ProjectFileModel.fromJson(Map<String, dynamic> json) {
    return ProjectFileModel(
      name: json['name'] as String? ?? '',
      fileUrl: json['file_url'] as String? ?? '',
    );
  }

  /// Convert model to JSON map
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'file_url': fileUrl,
    };
  }

  /// Convert to domain entity
  ProjectFile toEntity() {
    return ProjectFile(
      id: name,
      fileUrl: fileUrl,
    );
  }
}

/// Sample Project Model
///
/// Handles JSON serialization/deserialization for API communication.
class SampleProjectModel {
  /// Unique project identifier (API: name)
  final String name;

  /// Project display name (API: project_name)
  final String projectName;

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

  /// URL to 360° view (API: link)
  final String? link;

  /// Thumbnail image URL (API: thumbnail)
  final String? thumbnail;

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

  const SampleProjectModel({
    required this.name,
    required this.projectName,
    this.notes,
    this.link,
    this.thumbnail,
    this.filesList = const [],
  });

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

    return SampleProjectModel(
      name: json['name'] as String? ?? '',
      projectName: json['project_name'] as String? ?? '',
      notes: json['notes'] as String?,
      link: json['link'] as String?,
      thumbnail: json['thumbnail'] 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,
      'project_name': projectName,
      'notes': notes,
      'link': link,
      'thumbnail': thumbnail,
      'files_list': filesList.map((f) => f.toJson()).toList(),
    };
  }

  /// Convert to domain entity
  SampleProject toEntity() {
    return SampleProject(
      id: name,
      projectName: projectName,
      description: notes,
      viewUrl: link,
      thumbnailUrl: thumbnail,
      filesList: filesList.map((f) => f.toEntity()).toList(),
    );
  }

  /// Create model from domain entity
  factory SampleProjectModel.fromEntity(SampleProject entity) {
    return SampleProjectModel(
      name: entity.id,
      projectName: entity.projectName,
      notes: entity.description,
      link: entity.viewUrl,
      thumbnail: entity.thumbnailUrl,
      filesList: entity.filesList
          .map((f) => ProjectFileModel(name: f.id, fileUrl: f.fileUrl))
          .toList(),
    );
  }
}
