/// Storage constants for Hive CE (Community Edition) database
///
/// This file contains all box names, keys, and type IDs used throughout the app
/// for local data persistence and offline-first functionality.
library;

/// Hive Box Names
///
/// These are the names of Hive boxes used in the application.
/// Each box stores a specific type of data for organized storage.
class HiveBoxNames {
  // Private constructor to prevent instantiation
  HiveBoxNames._();

  /// User authentication and profile data
  static const String userBox = 'user_box';

  /// Product catalog and details cache
  static const String productBox = 'product_box';

  /// Shopping cart items
  static const String cartBox = 'cart_box';

  /// Order history and details
  static const String orderBox = 'order_box';

  /// Construction projects
  static const String projectBox = 'project_box';

  /// Quote requests and quotations
  static const String quotes = 'quotes_box';

  /// Loyalty program transactions and points history
  static const String loyaltyBox = 'loyalty_box';

  /// Rewards and gifts catalog
  static const String rewardsBox = 'rewards_box';

  /// User settings and preferences
  static const String settingsBox = 'settings_box';

  /// API response cache for offline access
  static const String cacheBox = 'cache_box';

  /// Data sync state tracking
  static const String syncStateBox = 'sync_state_box';

  /// Notifications
  static const String notificationBox = 'notification_box';

  /// Address book
  static const String addressBox = 'address_box';

  /// Favorite products data (cached from wishlist API)
  static const String favoriteProductsBox = 'favorite_products_box';

  /// Offline request queue for failed API calls
  static const String offlineQueueBox = 'offline_queue_box';

  /// City and Ward boxes for location data
  static const String cityBox = 'city_box';
  static const String wardBox = 'ward_box';

  /// Order status list cache
  static const String orderStatusBox = 'order_status_box';

  /// Project status list cache
  static const String projectStatusBox = 'project_status_box';

  /// Project progress list cache (construction stages)
  static const String projectProgressBox = 'project_progress_box';

  /// Get all box names for initialization
  static List<String> get allBoxes => [
    userBox,
    productBox,
    cartBox,
    orderBox,
    projectBox,
    quotes,
    loyaltyBox,
    rewardsBox,
    cityBox,
    wardBox,
    orderStatusBox,
    projectStatusBox,
    projectProgressBox,
    settingsBox,
    cacheBox,
    syncStateBox,
    notificationBox,
    addressBox,
    favoriteProductsBox,
    offlineQueueBox,
  ];
}

/// Hive Type Adapter IDs
///
/// Type IDs must be unique across the application.
/// Range 0-223 is reserved for user-defined types.
/// IMPORTANT: Never change these IDs once assigned, as it will break existing data.
class HiveTypeIds {
  // Private constructor to prevent instantiation
  HiveTypeIds._();

  // Core Models (0-9)
  static const int userModel = 0;
  static const int userSessionModel = 1;
  static const int productModel = 2;
  static const int stockLevelModel = 3;
  static const int cartModel = 4;
  static const int cartItemModel = 5;
  static const int orderModel = 6;
  static const int orderItemModel = 7;
  static const int invoiceModel = 8;
  static const int paymentLineModel = 9;

  // Loyalty Models (10-19)
  static const int loyaltyPointEntryModel = 10;
  static const int giftCatalogModel = 11;
  static const int redeemedGiftModel = 12;
  static const int pointsRecordModel = 13;

  // Project & Quote Models (14-17)
  static const int projectSubmissionModel = 14;
  static const int designRequestModel = 15;
  static const int quoteModel = 16;
  static const int quoteItemModel = 17;

  // Chat Models (18-19)
  static const int chatRoomModel = 18;
  static const int messageModel = 19;

  // Extended Models (20-30)
  static const int notificationModel = 20;
  static const int showroomModel = 21;
  static const int showroomProductModel = 22;
  static const int paymentReminderModel = 23;
  static const int auditLogModel = 24;
  static const int memberCardModel = 25;
  static const int promotionModel = 26;
  static const int categoryModel = 27;
  static const int favoriteModel = 28;
  static const int businessUnitModel = 29;
  static const int addressModel = 30;
  static const int cityModel = 31;
  static const int wardModel = 32;
  static const int orderStatusModel = 62;
  static const int projectStatusModel = 63;
  static const int projectProgressModel = 64;

  // Enums (33-61)
  static const int userRole = 33;
  static const int userStatus = 34;
  static const int loyaltyTier = 35;
  static const int orderStatus = 36;
  static const int invoiceType = 37;
  static const int invoiceStatus = 38;
  static const int paymentMethod = 39;
  static const int paymentStatus = 40;
  static const int entryType = 41;
  static const int entrySource = 42;
  static const int complaintStatus = 43;
  static const int giftCategory = 44;
  static const int giftStatus = 45;
  static const int pointsStatus = 46;
  static const int projectType = 47;
  static const int submissionStatus = 48;
  static const int designStatus = 49;
  static const int quoteStatus = 50;
  static const int roomType = 51;
  static const int contentType = 52;
  static const int reminderType = 53;
  static const int notificationType = 54;

  // Aliases for backward compatibility and clarity
  static const int memberTier = loyaltyTier; // Alias for loyaltyTier
  static const int userType = userRole; // Alias for userRole
  static const int projectStatus =
      submissionStatus; // Alias for submissionStatus
  static const int transactionType = entryType; // Alias for entryType

  // Cache & Sync Models (60-69)
  static const int cachedData = 60;
  static const int syncState = 61;
  static const int offlineRequest = 62;
}

/// Hive Storage Keys
///
/// Keys used to store and retrieve data from Hive boxes.
class HiveKeys {
  // Private constructor to prevent instantiation
  HiveKeys._();

  // User Box Keys
  static const String currentUser = 'current_user';
  static const String authToken = 'auth_token';
  static const String refreshToken = 'refresh_token';
  static const String isLoggedIn = 'is_logged_in';

  // Settings Box Keys
  static const String languageCode = 'language_code';
  static const String themeMode = 'theme_mode';
  static const String notificationsEnabled = 'notifications_enabled';
  static const String lastSyncTime = 'last_sync_time';
  static const String schemaVersion = 'schema_version';
  static const String encryptionEnabled = 'encryption_enabled';
}

/// Order Status Indices
///
/// Index values for order statuses stored in Hive.
/// These correspond to the index field in OrderStatusModel.
/// Use these constants to compare order status by index instead of hardcoded strings.
///
/// API Response Structure:
/// - status: "Pending approval" (English status name)
/// - label: "Chờ phê duyệt" (Vietnamese display label)
/// - color: "Warning" (Status color indicator)
/// - index: 1 (Unique identifier)
class OrderStatusIndex {
  // Private constructor to prevent instantiation
  OrderStatusIndex._();

  /// Pending approval - "Chờ phê duyệt"
  /// Color: Warning
  static const int pendingApproval = 1;

  /// Manager Review - "Manager Review"
  /// Color: Warning
  static const int managerReview = 2;

  /// Processing - "Đang xử lý"
  /// Color: Info
  static const int processing = 3;

  /// Completed - "Hoàn thành"
  /// Color: Success
  static const int completed = 4;

  /// Rejected - "Từ chối"
  /// Color: Danger
  static const int rejected = 5;

  /// Cancelled - "HỦY BỎ"
  /// Color: Danger
  static const int cancelled = 6;
}

/// Project Status Indices
///
/// Index values for project statuses stored in Hive.
/// These correspond to the index field in ProjectStatusModel.
///
/// API Response Structure:
/// - status: "Pending approval" (English status name)
/// - label: "Chờ phê duyệt" (Vietnamese display label)
/// - color: "Warning" (Status color indicator)
/// - index: 1 (Unique identifier)
class ProjectStatusIndex {
  // Private constructor to prevent instantiation
  ProjectStatusIndex._();

  /// Pending approval - "Chờ phê duyệt"
  /// Color: Warning
  static const int pendingApproval = 1;

  /// Approved - "Đã được phê duyệt"
  /// Color: Success
  static const int approved = 2;

  /// Rejected - "Từ chối"
  /// Color: Danger
  static const int rejected = 3;

  /// Cancelled - "HỦY BỎ"
  /// Color: Danger
  static const int cancelled = 4;
}

/// Hive Keys (continued)
extension HiveKeysContinued on HiveKeys {
  // Cache Box Keys
  static const String productsCacheKey = 'products_cache';
  static const String categoriesCacheKey = 'categories_cache';
  static const String promotionsCacheKey = 'promotions_cache';
  static const String loyaltyPointsCacheKey = 'loyalty_points_cache';
  static const String rewardsCacheKey = 'rewards_cache';

  // Sync State Box Keys
  static const String productsSyncTime = 'products_sync_time';
  static const String ordersSyncTime = 'orders_sync_time';
  static const String projectsSyncTime = 'projects_sync_time';
  static const String loyaltySyncTime = 'loyalty_sync_time';
  static const String lastFullSyncTime = 'last_full_sync_time';

  // App State Keys
  static const String firstLaunch = 'first_launch';
  static const String onboardingCompleted = 'onboarding_completed';
  static const String appVersion = 'app_version';
}

/// Cache Duration Constants
///
/// Default cache expiration durations for different data types.
class CacheDuration {
  // Private constructor to prevent instantiation
  CacheDuration._();

  /// Product data cache (6 hours)
  static const Duration products = Duration(hours: 6);

  /// Category data cache (24 hours)
  static const Duration categories = Duration(hours: 24);

  /// Loyalty points cache (1 hour)
  static const Duration loyaltyPoints = Duration(hours: 1);

  /// Rewards cache (12 hours)
  static const Duration rewards = Duration(hours: 12);

  /// Promotions cache (2 hours)
  static const Duration promotions = Duration(hours: 2);

  /// User profile cache (30 minutes)
  static const Duration userProfile = Duration(minutes: 30);

  /// Order history cache (5 minutes)
  static const Duration orderHistory = Duration(minutes: 5);

  /// Projects cache (10 minutes)
  static const Duration projects = Duration(minutes: 10);
}

/// Database Configuration
class HiveDatabaseConfig {
  // Private constructor to prevent instantiation
  HiveDatabaseConfig._();

  /// Current schema version for migrations
  static const int currentSchemaVersion = 1;

  /// Maximum cache size in MB
  static const int maxCacheSizeMB = 100;

  /// Maximum number of items in offline queue
  static const int maxOfflineQueueSize = 100;

  /// Enable encryption for sensitive data
  static const bool enableEncryption = false; // Set to true in production

  /// Compaction threshold (compact when box size grows by this percentage)
  static const double compactionThreshold = 0.3; // 30%
}
