
/// 사용자 역할 정의
enum UserRole {
  member,     // 일반 회원
  manager,    // 매니저 (일부 관리 권한)
  admin,      // 관리자 (대부분의 관리 권한)
  owner       // 소유자 (모든 권한)
}

/// 관리 권한 정의
class AdminPermission {
  final bool canManageMembers;      // 회원 관리 권한
  final bool canManageEvents;       // 이벤트 관리 권한
  final bool canManageScores;       // 점수 관리 권한
  final bool canManageSettings;     // 설정 관리 권한
  final bool canManageSubscription; // 구독 관리 권한
  final bool canManageRoles;        // 역할 관리 권한
  final bool canViewFinancials;     // 재정 정보 조회 권한
  final bool canExportData;         // 데이터 내보내기 권한

  const AdminPermission({
    this.canManageMembers = false,
    this.canManageEvents = false,
    this.canManageScores = false,
    this.canManageSettings = false,
    this.canManageSubscription = false,
    this.canManageRoles = false,
    this.canViewFinancials = false,
    this.canExportData = false,
  });

  /// 역할에 따른 기본 권한 생성
  factory AdminPermission.fromRole(UserRole role) {
    switch (role) {
      case UserRole.member:
        return const AdminPermission();
      case UserRole.manager:
        return const AdminPermission(
          canManageMembers: true,
          canManageEvents: true,
          canManageScores: true,
          canExportData: true,
        );
      case UserRole.admin:
        return const AdminPermission(
          canManageMembers: true,
          canManageEvents: true,
          canManageScores: true,
          canManageSettings: true,
          canViewFinancials: true,
          canExportData: true,
        );
      case UserRole.owner:
        return const AdminPermission(
          canManageMembers: true,
          canManageEvents: true,
          canManageScores: true,
          canManageSettings: true,
          canManageSubscription: true,
          canManageRoles: true,
          canViewFinancials: true,
          canExportData: true,
        );
    }
  }

  /// JSON에서 권한 객체 생성
  factory AdminPermission.fromJson(Map<String, dynamic> json) {
    return AdminPermission(
      canManageMembers: json['canManageMembers'] ?? false,
      canManageEvents: json['canManageEvents'] ?? false,
      canManageScores: json['canManageScores'] ?? false,
      canManageSettings: json['canManageSettings'] ?? false,
      canManageSubscription: json['canManageSubscription'] ?? false,
      canManageRoles: json['canManageRoles'] ?? false,
      canViewFinancials: json['canViewFinancials'] ?? false,
      canExportData: json['canExportData'] ?? false,
    );
  }

  /// 권한 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'canManageMembers': canManageMembers,
      'canManageEvents': canManageEvents,
      'canManageScores': canManageScores,
      'canManageSettings': canManageSettings,
      'canManageSubscription': canManageSubscription,
      'canManageRoles': canManageRoles,
      'canViewFinancials': canViewFinancials,
      'canExportData': canExportData,
    };
  }
}

/// 클럽 회원 역할 정보
class MemberRole {
  final String userId;
  final String memberId;
  final String clubId;
  final UserRole role;
  final AdminPermission permissions;
  final DateTime assignedAt;
  final String? assignedBy;

  MemberRole({
    required this.userId,
    required this.memberId,
    required this.clubId,
    required this.role,
    required this.permissions,
    required this.assignedAt,
    this.assignedBy,
  });

  /// JSON에서 회원 역할 객체 생성
  factory MemberRole.fromJson(Map<String, dynamic> json) {
    return MemberRole(
      userId: json['userId'],
      memberId: json['memberId'],
      clubId: json['clubId'],
      role: UserRole.values.firstWhere(
        (e) => e.toString().split('.').last == json['role'],
        orElse: () => UserRole.member,
      ),
      permissions: AdminPermission.fromJson(json['permissions'] ?? {}),
      assignedAt: DateTime.parse(json['assignedAt']),
      assignedBy: json['assignedBy'],
    );
  }

  /// 회원 역할 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'memberId': memberId,
      'clubId': clubId,
      'role': role.toString().split('.').last,
      'permissions': permissions.toJson(),
      'assignedAt': assignedAt.toIso8601String(),
      'assignedBy': assignedBy,
    };
  }

  /// 역할 이름 (한글)
  String get roleName {
    switch (role) {
      case UserRole.member:
        return '일반 회원';
      case UserRole.manager:
        return '매니저';
      case UserRole.admin:
        return '관리자';
      case UserRole.owner:
        return '소유자';
    }
  }
}

/// 클럽 설정 모델
class ClubSettings {
  final String clubId;
  final String clubName;
  final String? logoUrl;
  final String? bannerUrl;
  final String? description;
  final String? contactEmail;
  final String? contactPhone;
  final String? address;
  final String? website;
  final Map<String, dynamic>? customFields;
  final Map<String, dynamic>? notificationSettings;
  final Map<String, dynamic>? privacySettings;
  final DateTime createdAt;
  final DateTime updatedAt;

  ClubSettings({
    required this.clubId,
    required this.clubName,
    this.logoUrl,
    this.bannerUrl,
    this.description,
    this.contactEmail,
    this.contactPhone,
    this.address,
    this.website,
    this.customFields,
    this.notificationSettings,
    this.privacySettings,
    required this.createdAt,
    required this.updatedAt,
  });

  /// JSON에서 클럽 설정 객체 생성
  factory ClubSettings.fromJson(Map<String, dynamic> json) {
    return ClubSettings(
      clubId: json['clubId'],
      clubName: json['clubName'],
      logoUrl: json['logoUrl'],
      bannerUrl: json['bannerUrl'],
      description: json['description'],
      contactEmail: json['contactEmail'],
      contactPhone: json['contactPhone'],
      address: json['address'],
      website: json['website'],
      customFields: json['customFields'],
      notificationSettings: json['notificationSettings'],
      privacySettings: json['privacySettings'],
      createdAt: DateTime.parse(json['createdAt']),
      updatedAt: DateTime.parse(json['updatedAt']),
    );
  }

  /// 클럽 설정 객체를 JSON으로 변환
  Map<String, dynamic> toJson() {
    return {
      'clubId': clubId,
      'clubName': clubName,
      'logoUrl': logoUrl,
      'bannerUrl': bannerUrl,
      'description': description,
      'contactEmail': contactEmail,
      'contactPhone': contactPhone,
      'address': address,
      'website': website,
      'customFields': customFields,
      'notificationSettings': notificationSettings,
      'privacySettings': privacySettings,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt.toIso8601String(),
    };
  }

  /// 복사본 생성 (일부 필드 업데이트)
  ClubSettings copyWith({
    String? clubName,
    String? logoUrl,
    String? bannerUrl,
    String? description,
    String? contactEmail,
    String? contactPhone,
    String? address,
    String? website,
    Map<String, dynamic>? customFields,
    Map<String, dynamic>? notificationSettings,
    Map<String, dynamic>? privacySettings,
  }) {
    return ClubSettings(
      clubId: clubId,
      clubName: clubName ?? this.clubName,
      logoUrl: logoUrl ?? this.logoUrl,
      bannerUrl: bannerUrl ?? this.bannerUrl,
      description: description ?? this.description,
      contactEmail: contactEmail ?? this.contactEmail,
      contactPhone: contactPhone ?? this.contactPhone,
      address: address ?? this.address,
      website: website ?? this.website,
      customFields: customFields ?? this.customFields,
      notificationSettings: notificationSettings ?? this.notificationSettings,
      privacySettings: privacySettings ?? this.privacySettings,
      createdAt: createdAt,
      updatedAt: DateTime.now(),
    );
  }
}
