import Dexie, { type Table } from 'dexie';

export interface DBSite {
  id: string;
  data: string; // JSON serialized Site
  createdAt: number;
  updatedAt: number;
}

export interface DBProjectConfig {
  id: string;
  name: string;
  data: string; // JSON serialized ProjectConfig
  createdAt: number;
  updatedAt: number;
  syncedAt?: number;
}

export class RFCPDatabase extends Dexie {
  sites!: Table<DBSite, string>;
  projects!: Table<DBProjectConfig, string>;

  constructor() {
    super('rfcp-db');
    this.version(1).stores({
      sites: 'id, updatedAt',
      projects: 'id, updatedAt, syncedAt',
    });
  }
}

export const db = new RFCPDatabase();
