import db, { type HuDraftRecord } from '@/services/db'
export type { HuDraftRecord }

export async function getDrafts(projectId: number): Promise<HuDraftRecord[]> {
  return db.hu_drafts.where('projectId').equals(projectId).toArray()
}

export async function getDraft(id: string): Promise<HuDraftRecord | undefined> {
  return db.hu_drafts.get(id)
}

export async function saveDraft(d: HuDraftRecord) {
  await db.hu_drafts.put(d)
}

export async function deleteDraft(id: string) {
  await db.hu_drafts.delete(id)
}

export async function getUnpushedDrafts(projectId: number): Promise<HuDraftRecord[]> {
  return db.hu_drafts.where({ projectId, syncStatus: 'draft' }).toArray()
}

export function createDraftId(): string {
  return crypto.randomUUID()
}
