import {
    AutoIncrement,
    BelongsTo,
    Column,
    CreatedAt,
    DataType,
    DeletedAt,
    ForeignKey,
    Model,
    PrimaryKey,
    Table,
    UpdatedAt,
    HasMany,
} from 'sequelize-typescript';
import { InstanceDAO } from '../../../../instances/infrastructure/dataAccessObjects/instanceDAO';
import { HourCategoryForCreation } from '../../../business/models/hourCategorization';
import { TimeTrackingHourCategorizationRuleDAO } from './hourCategorizationRule';
import { TimeTrackingPolicyHourCategoryDAO } from './policyHourCategory';

@Table({
    indexes: [
        { fields: ['instanceId'] },
        { fields: ['name', 'instanceId'] },
        { fields: ['effectiveFrom', 'instanceId'] },
        { fields: ['effectiveTo', 'instanceId'] },
        { fields: ['instanceId', 'isActive'] },
    ],
    tableName: 'TimeTrackingHourCategories',
})
export class TimeTrackingHourCategoryDAO extends Model<TimeTrackingHourCategoryDAO, Partial<HourCategoryForCreation>> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

    @Column({
        allowNull: false,
        type: DataType.STRING,
    })
    name!: string;

    @Column({
        allowNull: true,
        type: DataType.TEXT,
    })
    description?: string;

    @ForeignKey(() => InstanceDAO)
    @Column({
        allowNull: false,
    })
    instanceId!: number;

    @BelongsTo(() => InstanceDAO)
    instance?: InstanceDAO;

    @HasMany(() => TimeTrackingHourCategorizationRuleDAO)
    rules!: TimeTrackingHourCategorizationRuleDAO[];

    @Column({
        allowNull: false,
        type: DataType.STRING,
    })
    effectiveFrom!: string;

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

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

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

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

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

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