import {
    Model,
    Table,
    Column,
    PrimaryKey,
    AutoIncrement,
    DataType,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
    ForeignKey,
    Index,
    HasMany,
    AllowNull,
} from 'sequelize-typescript';
import { InstanceDAO } from '../../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { ShiftForCreation } from '../../../business/models/shiftManagement/shift';
import { ShiftTimeSlotDAO } from './shiftTimeSlot';

@Table({
    indexes: [
        {
            fields: ['instanceId'],
        },
        {
            fields: ['code'],
        },
        {
            fields: ['lastVersion'],
        },
        {
            fields: ['code', 'version'],
            unique: true,
        },
        {
            fields: ['isTemplate'],
        },
    ],
    tableName: 'Shifts',
})
export class ShiftDAO extends Model<ShiftDAO, ShiftForCreation> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

    @Index('shifts_code_index')
    @Column
    code!: string;

    @Index('shifts_code_version_unique')
    @Column
    version!: number;

    @Index('shifts_last_version_index')
    @Column
    lastVersion!: boolean;

    @Index('shifts_instance_id_index')
    @ForeignKey(() => InstanceDAO)
    @Column
    instanceId!: number;

    @AllowNull(true)
    @Column({ type: DataType.STRING })
    name!: string;

    @AllowNull(true)
    @Column({ type: DataType.STRING })
    color!: string;

    @AllowNull(true)
    @Column({ type: DataType.TEXT })
    description!: string;

    @Column
    isRest!: boolean;

    @Column
    isTemplate!: boolean;

    @HasMany(() => ShiftTimeSlotDAO)
    timeSlots!: ShiftTimeSlotDAO[];

    @CreatedAt
    @Column(DataType.DATE)
    createdAt!: Date;

    @UpdatedAt
    @Column(DataType.DATE)
    updatedAt!: Date;

    @DeletedAt
    @Column(DataType.DATE)
    deletedAt!: Date | null;
}
