import { test } from "bun:test"
import assert from "assert"

import { field } from "../src/schema"
import { openDB } from "../src/db"

class Person {
  @field('primary key')
  id!: number

  @field('required')
  name!: string
}

class User {
  @field('primary key')
  id!: number

  @field('required', 'unique', 'email')
  email!: string

  @field('required')
  name!: string

  @field()
  age?: number
}

const db = (await openDB({ sqlite: ":memory:" }))
  .model(Person, 'people')
  .model(User, 'users')

test("basic sqlite schema generation", () => {
  assert.equal(db.people.schema(),
    "CREATE TABLE IF NOT EXISTS people (" +
    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    "name TEXT NOT NULL" +
    ")"
  )
  assert.equal(db.users.schema(),
    "CREATE TABLE IF NOT EXISTS users (" +
    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    "email TEXT NOT NULL UNIQUE, " +
    "name TEXT NOT NULL, " +
    "age INTEGER" +
    ")"
  )
})