import 'package:equatable/equatable.dart';

enum TrackingType {
  statusChange,
  manualNote,
  systemAlert,
  customerContact,
  assignment;

  static TrackingType fromString(String value) {
    return TrackingType.values.firstWhere(
      (e) => e.name == value,
      orElse: () => TrackingType.manualNote,
    );
  }
}

enum TrackingParentType {
  ticket,
  operation;

  String get value => name;

  static TrackingParentType fromString(String val) {
    return TrackingParentType.values.firstWhere(
      (e) => e.name == val,
      orElse: () => TrackingParentType.ticket, // Default di sicurezza
    );
  }
}

class TrackingModel extends Equatable {
  final String? id;
  final DateTime createdAt;
  final String companyId;
  final String? staffId;
  final String? staffName; // Per non fare mille join, lo prendiamo dal repo
  final String parentId;
  final TrackingParentType parentType;
  final TrackingType eventType;
  final bool isInternal;
  final String message;

  const TrackingModel({
    this.id,
    required this.createdAt,
    required this.companyId,
    this.staffId,
    this.staffName,
    required this.parentId,
    required this.parentType,
    required this.eventType,
    required this.isInternal,
    required this.message,
  });

  TrackingModel copyWith({
    String? id,
    DateTime? createdAt,
    String? companyId,
    String? staffId,
    String? staffName,
    TrackingParentType? parentType,
    String? parentId,
    TrackingType? eventType,
    bool? isInternal,
    String? message,
  }) {
    return TrackingModel(
      id: id ?? this.id,
      createdAt: createdAt ?? this.createdAt,
      companyId: companyId ?? this.companyId,
      staffId: staffId ?? this.staffId,
      staffName: staffName ?? this.staffName,
      parentId: parentId ?? this.parentId,
      parentType: parentType ?? this.parentType,
      eventType: eventType ?? this.eventType,
      isInternal: isInternal ?? this.isInternal,
      message: message ?? this.message,
    );
  }

  factory TrackingModel.fromMap(Map<String, dynamic> map) {
    return TrackingModel(
      id: map['id'],
      createdAt: DateTime.parse(map['created_at']),
      companyId: map['company_id'],
      staffId: map['staff_id'],
      staffName: map['staff_member']?['name'], // Se fai la join su staff_member
      parentId: map['parent_id'] as String,
      parentType: TrackingParentType.fromString(map['parent_type']),
      eventType: TrackingType.fromString(map['event_type']),
      isInternal: map['is_internal'] ?? true,
      message: map['message'],
    );
  }

  Map<String, dynamic> toMap() {
    final map = <String, dynamic>{
      'company_id': companyId,
      'staff_id': staffId,
      'parent_id': parentId,
      'parent_type': parentType.name,
      'event_type': eventType.name,
      'is_internal': isInternal,
      'message': message,
    };

    // Aggiungiamo id e data SOLO se stiamo aggiornando un record esistente.
    // In fase di creazione (insert), li omettiamo così Supabase usa i valori di default (gen_random_uuid e now()).
    if (id != null) {
      map['id'] = id;
      map['created_at'] = createdAt.toIso8601String();
    }

    return map;
  }

  @override
  List<Object?> get props => [
    id,
    createdAt,
    companyId,
    staffId,
    staffName,
    parentId,
    parentType,
    eventType,
    isInternal,
    message,
  ];
}
