import 'package:flutter/cupertino.dart';
import 'package:iconsax/iconsax.dart';

class RoleModel {
  final String id;
  final String name;
  final String? icon;
  final String? description;
  final DateTime createdAt;
  final DateTime updatedAt;

  RoleModel({
    required this.id,
    required this.name,
    this.icon,
    this.description,
    required this.createdAt,
    required this.updatedAt,
  });

  // Create a RoleModel instance from a JSON object
  factory RoleModel.fromJson(Map<String, dynamic> json) {
    return RoleModel(
      id: json['id'] as String,
      name: json['name'] as String,
      icon: json['icon'] as String?,
      description: json['description'] as String?,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
    );
  }

  // Convert a RoleModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'icon': icon,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  // Create a copy of the RoleModel with updated fields
  RoleModel copyWith({
    String? id,
    String? name,
    String? icon,
    String? description,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return RoleModel(
      id: id ?? this.id,
      name: name ?? this.name,
      icon: icon ?? this.icon,
      description: description ?? this.description,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  // Check if the role is an officer role
  bool get isOfficer => name.toLowerCase() == 'officer';

  // Check if the role is a viewer role
  bool get isViewer => name.toLowerCase() == 'viewer';

  // Convert the instance method to a static method for easier access
  static IconData iconRole(String? iconName) {
    if (iconName != null) {
      // Map icon string to IconData
      switch (iconName) {
        case 'user':
          return Iconsax.user;
        case 'eye':
          return CupertinoIcons.eye;
        // Add more mappings as needed
        default:
          return Iconsax.user; // fallback
      }
    }
    return Iconsax.user; // default icon
  }

  // Keep the instance getter for convenience
  IconData get iconData {
    if (icon != null) {
      return RoleModel.iconRole(icon);
    } else if (isOfficer) {
      return Iconsax.user;
    } else if (isViewer) {
      return CupertinoIcons.person;
    }
    return Iconsax.user; // default icon
  }

  @override
  String toString() {
    return 'RoleModel(id: $id, name: $name)';
  }
}
