import { AutoIncrement, Column, DataType, DeletedAt, ForeignKey, HasMany, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { InstanceDAO } from '../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { TimeTrackingPolicyAutoCloseMode } from '../../business/enum/timeTracking/policy';
import { TimeTrackingPolicyHourCategoryDAO } from './hourCategory/policyHourCategory';
import { TimeTrackingPolicyUsersDAO } from './policyUsers';

export class TimeTrackingPolicyCreationAttributes {
    name!: string;
    description!: string;
    instanceId!: number;
    clockingInAppEnabled!: boolean;
    clockingInWebEnabled!: boolean;
    clockingInKioskEnabled!: boolean;
    faceRecognitionEnabled!: boolean;
    faceRecognitionBlocking!: boolean;
    nightShift!: boolean;
    latenessTolerance?: number | null;
    notificationsEnabled!: boolean;
    autoCloseLimitHours!: number;
    autoCloseMode!: TimeTrackingPolicyAutoCloseMode;
    autoCloseFixedHoursValue!: number | null;
    autoCloseFixedMinutesValue!: number | null;
    geofencing!: boolean | null;
    enableRemoteWork!: boolean | null;
    saveRemoteWorkLocation!: boolean;
}

@Table({ tableName: 'TimeTrackingPolicies' })
export class TimeTrackingPolicyDAO extends Model<TimeTrackingPolicyDAO, TimeTrackingPolicyCreationAttributes> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

    @Column
    name!: string;

    @Column
    description!: string;

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

    @Column
    clockingInAppEnabled!: boolean;

    @Column
    clockingInWebEnabled!: boolean;

    @Column
    faceRecognitionEnabled!: boolean;

    @Column
    faceRecognitionBlocking!: boolean;

    @Column
    notificationsEnabled!: boolean;

    @Column({ defaultValue: false, type: DataType.BOOLEAN })
    clockingInKioskEnabled!: boolean;

    @Column({ type: DataType.BOOLEAN })
    geofencing!: boolean | null;

    @Column({ type: DataType.BOOLEAN })
    enableRemoteWork!: boolean | null;

    @Column({ allowNull: false, defaultValue: false, type: DataType.BOOLEAN })
    saveRemoteWorkLocation!: boolean;

    @Column
    allUsers!: boolean;

    @Column
    nightShift!: boolean;

    @Column
    latenessTolerance?: number;

    @Column
    generatePolicyHistoryFromDate?: Date;

    @Column({ allowNull: false, type: DataType.INTEGER })
    autoCloseLimitHours!: number;

    @Column(DataType.ENUM(...Object.values(TimeTrackingPolicyAutoCloseMode)))
    autoCloseMode!: string;

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

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

    @HasMany(() => TimeTrackingPolicyUsersDAO)
    users?: TimeTrackingPolicyUsersDAO[];

    @HasMany(() => TimeTrackingPolicyHourCategoryDAO)
    policyHourCategories?: TimeTrackingPolicyHourCategoryDAO[];

    @DeletedAt
    @Column({ field: 'deleted_at', type: DataType.DATE })
    deletedAt!: Date | null;
}
