import {
    Model,
    Table,
    Column,
    PrimaryKey,
    AutoIncrement,
    DataType,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
    ForeignKey,
    HasMany,
    BelongsTo,
} from 'sequelize-typescript';
import { InstanceDAO } from '../../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { ShiftAssignedShiftForCreation } from '../../../business/models/shiftManagement/shiftAssignedShift';
import { ShiftAssignedShiftStatus } from '../../../business/types/shiftManagement/assignedShiftStatus';
import { ShiftAssignedShiftType } from '../../../business/types/shiftManagement/assignedShiftType';
import { ShiftAssignedShiftSource } from '../../../business/types/shiftManagement/source';
import { ShiftDAO } from './shift';
import { ShiftAssignedDayDAO } from './shiftAssignedDay';
import { ShiftAssignedTimeSlotDAO } from './shiftAssignedTimeSlot';

@Table({ tableName: 'ShiftAssignedShifts' })
export class ShiftAssignedShiftDAO extends Model<ShiftAssignedShiftDAO, ShiftAssignedShiftForCreation> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

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

    @ForeignKey(() => ShiftAssignedDayDAO)
    @Column
    assignedDayId!: number;

    @Column({ allowNull: true })
    shiftCode!: string;

    @Column({ allowNull: true })
    shiftVersion!: number;

    @Column({ allowNull: true, type: DataType.UUID })
    shiftRotationId!: string | null;

    @Column({ allowNull: true, type: DataType.INTEGER })
    shiftRotationDayPosition!: number | null;

    @Column(DataType.ENUM(...Object.values(ShiftAssignedShiftSource)))
    source!: ShiftAssignedShiftSource;

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

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

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

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

    @BelongsTo(() => ShiftAssignedDayDAO)
    assignedDay!: ShiftAssignedDayDAO;

    @BelongsTo(() => ShiftDAO, {
        as: 'shift',
        constraints: false,
        foreignKey: 'shiftCode',
        targetKey: 'code',
    })
    shift?: ShiftDAO;

    @Column(DataType.ENUM(...Object.values(ShiftAssignedShiftType)))
    type!: ShiftAssignedShiftType;

    @Column(DataType.ENUM(...Object.values(ShiftAssignedShiftStatus)))
    status!: ShiftAssignedShiftStatus;
}
