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

import { field, validate } from "../src/schema"

class Person {
    @field('required')
    name!: string
}

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

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

    @field('required')
    name!: string

    age?: number
}

test("required decorator validates presence", () => {
    const p = new Person()
    const [ok, errors] = validate(p)
    assert.equal(ok, false)
    assert.deepEqual(errors, { name: ["is required"] })
})

test("validate returns null when no errors", () => {
    const p = new Person()
    p.name = "Alice"
    const [ok, errors] = validate(p)
    assert.equal(ok, true)
    assert.deepEqual(errors, {})
})

test("validate returns errors when invalid", () => {
    const u = new User()
    u.email = "not an email"
    const [ok, errors] = validate(u)
    assert.equal(ok, false)
    assert.deepEqual(errors, { id: ["must be an integer"], name: ["is required"], email: ["must be a valid email address"] })
})

test("rules can stack", () => {
    const u = new User()
    u.email = ""
    u.name = ""
    const [ok, errors] = validate(u)
    assert.equal(ok, false)
    assert.deepEqual(errors, { id: ["must be an integer"], email: ["is required", "must be a valid email address"], name: ["is required"] })
})