/**
 * IndexedDB Schema with Dexie.js
 *
 * Manages offline storage for large assets (videos > 5MB) and project metadata.
 */

import Dexie, { type EntityTable } from 'dexie';
import { OFFLINE_CONFIG } from '../../config/offline.config';
import type {
  OfflineAsset,
  OfflineProject,
  DownloadQueueItem,
} from '../../types/offline';

// Dexie database class
class OfflineDatabase extends Dexie {
  assets!: EntityTable<OfflineAsset, 'id'>;
  projects!: EntityTable<OfflineProject, 'id'>;
  downloadQueue!: EntityTable<DownloadQueueItem, 'id'>;

  constructor() {
    super(OFFLINE_CONFIG.dbName);

    this.version(OFFLINE_CONFIG.dbVersion).stores({
      // Assets table - stores large files (videos > 5MB)
      assets: 'id, projectId, url, variantType, assetType, downloadedAt',

      // Projects table - tracks offline project status
      projects: 'id, slug, status, lastSyncedAt',

      // Download queue - persists download state for resume capability
      downloadQueue: 'id, projectId, status, priority, addedAt',
    });
  }
}

// Singleton instance
export const offlineDb = new OfflineDatabase();

// Export types for convenience
export type { OfflineAsset, OfflineProject, DownloadQueueItem };
