import {
    Model,
    Column,
    Table,
    PrimaryKey,
    AutoIncrement,
    DataType,
    HasMany,
    BelongsToMany,
    DeletedAt,
    UpdatedAt,
    CreatedAt,
} from 'sequelize-typescript';
import { UserDAO } from '../../../users/infrastructure/dataAccessObjects/userDAO';
import { ScheduledDaysDAO } from './scheduledDays';
import { WorkScheduleUsersDAO } from './workScheduleUsers';

interface WorkScheduleDAOCreationAttributes {
    name: string;
    description: string;
    instanceId: number;
    createdBy: number;
    version: number;
    code: string;
    lastVersion: boolean;
    autoCreated?: boolean;
    hash?: string;
    generalTimeSlots?: boolean;
}

@Table({
    scopes: {
        ofInstance: (instanceId: number) => ({
            where: {
                instanceId,
            },
        }),
    },
    tableName: 'WorkSchedules',
})
export class WorkScheduleDAO extends Model<WorkScheduleDAO, WorkScheduleDAOCreationAttributes> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

    @Column
    code!: string;

    @Column
    lastVersion!: boolean;

    @Column
    name!: string;

    @Column
    description!: string;

    @Column
    instanceId!: number;

    @Column
    createdBy!: number;

    @Column
    version!: number;

    @Column
    generalTimeSlots!: boolean;

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

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

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

    @Column
    hash?: string;

    @Column
    autoCreated?: boolean;

    @HasMany(() => ScheduledDaysDAO, 'workScheduleId')
    scheduledDays?: ScheduledDaysDAO[];

    @BelongsToMany(() => UserDAO, () => WorkScheduleUsersDAO, 'workScheduleId', 'userId')
    collaborators?: UserDAO[];

    @HasMany(() => WorkScheduleUsersDAO)
    workScheduleUsers?: WorkScheduleUsersDAO[];
}
