package com.trevjonez.ktor_playground.people

import com.trevjonez.ktor_playground.Validator
import io.requery.Column
import io.requery.Entity
import io.requery.Key
import java.time.ZonedDateTime
import java.util.UUID

@Entity
data class PersonEntity(
        @get:Key
        @get:Column(nullable = false, unique = true)
        override val id: UUID,

        @get:Column(nullable = false)
        override val createdOn: ZonedDateTime,

        @get:Column(nullable = false)
        override val updatedOn: ZonedDateTime,

        @get:Column(nullable = true)
        override val deletedOn: ZonedDateTime?,

        @get:Column(nullable = false)
        override val age: Int,

        @get:Column(nullable = false)
        override val name: String
) : ValidatedPerson {

    companion object PostValidator : Validator.Post<Person, PersonEntity> {
        override fun validate(input: Person): PersonEntity {
            val now = ZonedDateTime.now()
            return with(input) {
                PersonEntity(id ?: UUID.randomUUID(),
                             createdOn ?: now,
                             now,
                             deletedOn,
                             age ?: throw NullPointerException("age"),
                             name ?: throw NullPointerException("name"))
            }
        }
    }
}