import {
    IsInt,
    IsNotEmpty,
    IsPositive,
    IsString
} from "class-validator";
import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryColumn } from "typeorm";

/**
 * Defines the ConfigFlag entity.
 * This entity can be used to set some flags on db init.
*/
@Entity()
export class ConfigFlag {
    /**
     * The flag's name (primary).
     */
    @PrimaryColumn()
    @IsString()
    option: string;

    /**
     * The flag's value.
     */
    @Column()
    @IsString()
    @IsNotEmpty()
    value: string;

    @Column({ type: 'bigint', nullable: true, readonly: true })
    @IsInt()
    @IsPositive()
    created_at: number;

    @Column({ type: 'bigint', nullable: true })
    @IsInt()
    @IsPositive()
    updated_at: number;

    @BeforeInsert()
    public setCreatedAt() {
        this.created_at = Math.floor(Date.now() / 1000);
        this.updated_at = Math.floor(Date.now() / 1000);
    }

    @BeforeUpdate()
    public setUpdatedAt() {
        this.updated_at = Math.floor(Date.now() / 1000);
    }
}
