/// Domain Entity: Design Request
///
/// Represents a design request/ticket submitted by user.
/// Based on API response from building_material.building_material.api.design_request
library;

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

/// Design Request Status
///
/// Maps from API status_color field:
/// - "Success" -> completed
/// - "Warning" -> pending
/// - "Danger" -> rejected
/// - Other -> designing
enum DesignRequestStatus {
  pending,
  designing,
  completed,
  rejected,
}

/// Design Request Entity
///
/// Contains information about a design request ticket.
/// API field mapping:
/// - name -> id
/// - subject -> subject
/// - description -> description (HTML content)
/// - dateline -> dateline
/// - status -> statusText
/// - status_color -> statusColor (mapped to enum)
/// - files_list -> filesList (detail only)
class DesignRequest extends Equatable {
  /// Unique request identifier (API: name)
  final String id;

  /// 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 statusText;

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

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

  const DesignRequest({
    required this.id,
    required this.subject,
    this.description,
    this.dateline,
    required this.statusText,
    required this.statusColor,
    this.filesList = const [],
  });

  /// Get status enum from statusColor
  DesignRequestStatus get status {
    switch (statusColor.toLowerCase()) {
      case 'success':
        return DesignRequestStatus.completed;
      case 'warning':
        return DesignRequestStatus.pending;
      case 'danger':
        return DesignRequestStatus.rejected;
      default:
        return DesignRequestStatus.designing;
    }
  }

  /// Check if request is completed
  bool get isCompleted => status == DesignRequestStatus.completed;

  /// Check if request is pending
  bool get isPending => status == DesignRequestStatus.pending;

  /// Check if request is rejected
  bool get isRejected => status == DesignRequestStatus.rejected;

  /// Get plain text description (strips HTML tags)
  String get plainDescription {
    if (description == null) return '';
    // Simple HTML tag removal
    return description!
        .replaceAll(RegExp(r'<[^>]*>'), '')
        .replaceAll('&nbsp;', ' ')
        .trim();
  }

  /// Get all file URLs
  List<String> get fileUrls => filesList.map((f) => f.fileUrl).toList();

  @override
  List<Object?> get props => [
        id,
        subject,
        description,
        dateline,
        statusText,
        statusColor,
        filesList,
      ];

  @override
  String toString() {
    return 'DesignRequest(id: $id, subject: $subject, status: $statusText, filesCount: ${filesList.length})';
  }
}
