import {Field, FieldType, Model} from '@extollo/lib'

export class WorkItem extends Model<WorkItem> {
    protected static table = 'work_items'
    protected static key = 'work_item_id'

    @Field(FieldType.serial, 'work_item_id')
    public readonly workItemId!: number

    @Field(FieldType.bool)
    public visible: boolean = false

    @Field(FieldType.varchar)
    public name!: string

    @Field(FieldType.text)
    public description: string = ''

    @Field(FieldType.timestamp, 'start_date')
    public startDate!: Date

    @Field(FieldType.timestamp, 'end_date')
    public endDate?: Date

    public startDisplay(): string {
        return `${this.getMonth(this.startDate.getMonth())} ${this.startDate.getFullYear()}`
    }

    public endDisplay(): string {
        if ( !this.endDate ) {
            return 'Present'
        }

        return `${this.getMonth(this.endDate.getMonth())} ${this.endDate.getFullYear()}`
    }

    public rangeDisplay(): string {
        return `${this.startDisplay()} - ${this.endDisplay()}`
    }

    protected getMonth(index: number): string {
        return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][index]
    }
}
