import {
    Model,
    Table,
    Column,
    PrimaryKey,
    AutoIncrement,
    DataType,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
    ForeignKey,
    HasMany,
    Index,
} from 'sequelize-typescript';
import { InstanceDAO } from '../../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { ShiftAssignedDayForCreation } from '../../../business/models/shiftManagement/shiftAssignedDay';
import { ShiftAssignedDayStatus } from '../../../business/types/shiftAssignedDayStatus';
import { ShiftAssignedShiftDAO } from './shiftAssignedShifts';

@Table({
    indexes: [
        {
            fields: ['userId', 'dateString', 'status'],
            unique: true,
            where: {
                deletedAt: null,
            },
        },
    ],
    tableName: 'ShiftAssignedDays',
})
export class ShiftAssignedDayDAO extends Model<ShiftAssignedDayDAO, ShiftAssignedDayForCreation> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

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

    @Index('shift_assigned_days_user_date_status_unique')
    @Column
    userId!: number;

    @Index('shift_assigned_days_user_date_status_unique')
    @Column
    dateString!: string;

    @Index('shift_assigned_days_user_date_status_unique')
    @Column(DataType.ENUM(...Object.values(ShiftAssignedDayStatus)))
    status!: ShiftAssignedDayStatus;

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

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

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

    @HasMany(() => ShiftAssignedShiftDAO, 'assignedDayId')
    shiftAssociations!: ShiftAssignedShiftDAO[];
}
