import { Op } from 'sequelize';
import { Model, Table, Column, PrimaryKey, AutoIncrement, DataType, CreatedAt, UpdatedAt, DeletedAt, ForeignKey } from 'sequelize-typescript';
import { InstanceDAO } from '../../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { TimeTrackingUserSettingForCreation } from '../../../business/models/timeTracking/timeTrackingUserSetting';

@Table({
    indexes: [
        {
            fields: ['instanceId', 'userId'],
            name: 'time_tracking_user_settings_instance_id_user_id_unique',
            unique: true,
            where: {
                deletedAt: null,
            },
        },
        {
            fields: ['instanceId', 'kioskPin'],
            name: 'time_tracking_user_settings_instance_id_kiosk_pin_unique',
            unique: true,
            where: {
                deletedAt: null,
                kioskPin: {
                    [Op.not]: null,
                },
            },
        },
    ],
    tableName: 'TimeTrackingUserSettings',
})
export class TimeTrackingUserSettingDAO extends Model<TimeTrackingUserSettingDAO, TimeTrackingUserSettingForCreation> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

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

    @Column
    userId!: number;

    @Column({ allowNull: true, type: DataType.STRING })
    kioskPin?: string | null;

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

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

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