// lib/core/models/user.dart
import 'package:isar/isar.dart';

part 'user.g.dart'; // This tells build_runner to generate the Isar code

@collection
class LocalUser {
  Id id = Isar.autoIncrement;

  // Optional unique index to prevent duplicate local rows
  @Index(unique: true, replace: true)
  int? identifierId;

  // --- Shared Fields ---
  late String fullName;
  late String email;
  late String phoneNumber;
  late String role; // e.g., "student", "expert", "admin"
  
  // --- Normal User Fields (Rockview University) ---
  String? nrc;       
  String? gender;
  String? country;
  String? campus;
  String? school;
  String? program;
  
  // --- Media Fields ---
  String? profilePicture; 
  
  // --- Expert Fields (MUST BE NULLABLE) ---
  String? bio; 
  String? professionalTitle;
  double? hourlyRate;
  bool? isVerified;

  // --- ADDED: Admin Fields ---
  String? permissions; // Stores the JSON string array of granted tabs
  String? position;    // 🚀 NEW: Official administrative/staff position

  bool isSynced = false;

  // --- REQUIRED: Unnamed Constructor for Isar ---
  LocalUser();

  // =====================================================================
  // --- JSON SERIALIZATION FOR BACKEND SYNC & SESSION SWITCHING ---
  // =====================================================================

  factory LocalUser.fromJson(Map<String, dynamic> json) {
    return LocalUser()
      ..identifierId = json['id'] is int ? json['id'] : int.tryParse(json['id']?.toString() ?? '')
      ..fullName = json['fullName'] ?? json['full_name'] ?? json['name'] ?? 'Unknown User'
      ..email = json['email'] ?? ''
      ..phoneNumber = json['phoneNumber'] ?? json['phone'] ?? ''
      ..role = json['role'] ?? 'user'
      ..nrc = json['nrc']
      ..gender = json['gender']
      ..country = json['country']
      // 🚀 STRICT PARSING: Prevents empty strings from overwriting real nulls
      ..campus = (json['campus']?.toString().trim().isNotEmpty == true) ? json['campus'].toString().trim() : null
      ..school = json['school']
      ..program = json['program']
      ..profilePicture = json['profilePicture'] ?? json['profile_picture']
      ..bio = json['bio']
      ..professionalTitle = json['professionalTitle'] ?? json['professional_title']
      ..hourlyRate = json['hourlyRate'] != null ? (json['hourlyRate'] as num).toDouble() : null
      ..isVerified = json['isVerified'] ?? json['is_verified']
      ..permissions = json['permissions']
      ..position = json['position'] // 🚀 Map the position from JSON
      ..isSynced = true;
  }

  Map<String, dynamic> toJson() {
    return {
      'id': identifierId,
      'full_name': fullName,
      'email': email,
      'phone': phoneNumber,
      'role': role,
      'nrc': nrc,
      'gender': gender,
      'country': country,
      'campus': campus,
      'school': school,
      'program': program,
      'profile_picture': profilePicture,
      'bio': bio,
      'professional_title': professionalTitle,
      'hourly_rate': hourlyRate,
      'is_verified': isVerified,
      'permissions': permissions,
      'position': position, // 🚀 Include position in outgoing payload
    };
  }
}