import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

class NoteModel extends Equatable {
  final String? id;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String companyId;
  final String createdBy;
  final String? title;
  final String? content;
  final String color;
  final bool isPinned;
  final bool isSharedAll;
  final List<String> collaboratorIds;

  // Campi di utilità per la UI e le relazioni
  //final List<StaffMemberModel> collaborators;

  const NoteModel({
    this.id,
    this.createdAt,
    this.updatedAt,
    required this.companyId,
    required this.createdBy,
    this.title,
    this.content,
    this.color = '#FFF59D',
    this.isPinned = false,
    this.isSharedAll = false,
    required this.collaboratorIds,
  });

  /// Trasforma il colore Hex String in un oggetto Color di Flutter
  Color get flutterColor {
    final hexCode = color.replaceAll('#', '');
    return Color(int.parse('FF$hexCode', radix: 16));
  }

  NoteModel copyWith({
    String? id,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? companyId,
    String? createdBy,
    String? title,
    String? content,
    String? color,
    bool? isPinned,
    bool? isSharedAll,
    List<String>? collaboratorIds,
  }) {
    return NoteModel(
      id: id ?? this.id,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      companyId: companyId ?? this.companyId,
      createdBy: createdBy ?? this.createdBy,
      title: title ?? this.title,
      content: content ?? this.content,
      color: color ?? this.color,
      isPinned: isPinned ?? this.isPinned,
      isSharedAll: isSharedAll ?? this.isSharedAll,
      collaboratorIds: collaboratorIds ?? this.collaboratorIds,
    );
  }

  factory NoteModel.empty({
    required String createdBy,
    required String companyId,
  }) {
    return NoteModel(
      createdBy: createdBy,
      companyId: companyId,
      collaboratorIds: [],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      if (id != null) 'id': id,
      'created_by': createdBy,
      'title': title,
      'content': content,
      'color': color,
      'is_pinned': isPinned,
      'is_shared_all': isSharedAll,
      'company_id': companyId,
      // I collaboratori vanno in una tabella separata, quindi non li inseriamo qui
    };
  }

  factory NoteModel.fromMap(Map<String, dynamic> map) {
    return NoteModel(
      id: map['id'] as String?,
      createdAt: map['created_at'] != null
          ? DateTime.parse(map['created_at'] as String)
          : null,
      updatedAt: map['updated_at'] != null
          ? DateTime.parse(map['updated_at'] as String)
          : null,
      companyId: map['company_id'] as String,
      createdBy: map['created_by'] as String,
      title: map['title'] as String?,
      content: map['content'] as String?,
      color: map['color'] as String? ?? '#FFF59D',
      isPinned: map['is_pinned'] as bool? ?? false,
      isSharedAll: map['is_shared_all'] as bool? ?? false,
      // TRUCCO NINJA: Castiamo l'array di Postgres in una List<String> pulita
      collaboratorIds: map['collaborator_ids'] != null
          ? List<String>.from(map['collaborator_ids'] as List)
          : [],
    );
  }

  @override
  List<Object?> get props => [
    id,
    createdAt,
    updatedAt,
    createdBy,
    title,
    content,
    color,
    isPinned,
    isSharedAll,
    companyId,
    collaboratorIds,
    //collaborators,
  ];
}
