/// Data Model: Price Document Model
///
/// Data layer model for price policy documents.
/// Handles JSON serialization and conversion to/from domain entity.
library;

import 'package:worker/features/price_policy/domain/entities/price_document.dart';

/// Price Document Model
///
/// Used in the data layer for:
/// - JSON serialization/deserialization from API
/// - Conversion to domain entity
/// - Local storage (if needed)
class PriceDocumentModel {
  /// Unique document ID
  final String id;

  /// Document title
  final String title;

  /// Document description
  final String description;

  /// Date the document was published (ISO 8601 string)
  final String publishedDate;

  /// Type of document (pdf or excel)
  final String documentType;

  /// Category (policy or priceList)
  final String category;

  /// URL to download the document
  final String downloadUrl;

  /// Optional file size display string
  final String? fileSize;

  /// Constructor
  const PriceDocumentModel({
    required this.id,
    required this.title,
    required this.description,
    required this.publishedDate,
    required this.documentType,
    required this.category,
    required this.downloadUrl,
    this.fileSize,
  });

  /// Create model from JSON
  factory PriceDocumentModel.fromJson(Map<String, dynamic> json) {
    return PriceDocumentModel(
      id: json['id'] as String,
      title: json['title'] as String,
      description: json['description'] as String,
      publishedDate: json['published_date'] as String,
      documentType: json['document_type'] as String,
      category: json['category'] as String,
      downloadUrl: json['download_url'] as String,
      fileSize: json['file_size'] as String?,
    );
  }

  /// Convert model to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'published_date': publishedDate,
      'document_type': documentType,
      'category': category,
      'download_url': downloadUrl,
      'file_size': fileSize,
    };
  }

  /// Convert model to domain entity
  PriceDocument toEntity() {
    return PriceDocument(
      id: id,
      title: title,
      description: description,
      publishedDate: DateTime.parse(publishedDate),
      documentType: _parseDocumentType(documentType),
      category: _parseCategory(category),
      downloadUrl: downloadUrl,
      fileSize: fileSize,
    );
  }

  /// Create model from domain entity
  factory PriceDocumentModel.fromEntity(PriceDocument entity) {
    return PriceDocumentModel(
      id: entity.id,
      title: entity.title,
      description: entity.description,
      publishedDate: entity.publishedDate.toIso8601String(),
      documentType: _documentTypeToString(entity.documentType),
      category: _categoryToString(entity.category),
      downloadUrl: entity.downloadUrl,
      fileSize: entity.fileSize,
    );
  }

  /// Parse document type from string
  static DocumentType _parseDocumentType(String type) {
    switch (type.toLowerCase()) {
      case 'pdf':
        return DocumentType.pdf;
      case 'excel':
        return DocumentType.excel;
      default:
        return DocumentType.pdf;
    }
  }

  /// Parse category from string
  static DocumentCategory _parseCategory(String category) {
    switch (category.toLowerCase()) {
      case 'policy':
        return DocumentCategory.policy;
      case 'pricelist':
      case 'price_list':
        return DocumentCategory.priceList;
      default:
        return DocumentCategory.policy;
    }
  }

  /// Convert document type to string
  static String _documentTypeToString(DocumentType type) {
    switch (type) {
      case DocumentType.pdf:
        return 'pdf';
      case DocumentType.excel:
        return 'excel';
    }
  }

  /// Convert category to string
  static String _categoryToString(DocumentCategory category) {
    switch (category) {
      case DocumentCategory.policy:
        return 'policy';
      case DocumentCategory.priceList:
        return 'priceList';
    }
  }

  @override
  String toString() {
    return 'PriceDocumentModel(id: $id, title: $title, category: $category, '
        'documentType: $documentType, publishedDate: $publishedDate)';
  }
}
