import {
    AutoIncrement,
    BelongsTo,
    Column,
    CreatedAt,
    DataType,
    DeletedAt,
    ForeignKey,
    Model,
    PrimaryKey,
    Table,
    UpdatedAt,
} from 'sequelize-typescript';
import { HourCategorizationRuleForCreation } from '../../../business/models/hourCategorization';
import { HourCategorizationValidValues } from '../../../business/types/hourCategorization/hourCategorizationValidValues';
import { TimeTrackingHourCategoryDAO } from './hourCategory';

@Table({
    indexes: [
        {
            fields: ['hourCategoryId'],
        },
    ],
    tableName: 'TimeTrackingHourCategorizationRules',
})
export class TimeTrackingHourCategorizationRuleDAO extends Model<TimeTrackingHourCategorizationRuleDAO, Partial<HourCategorizationRuleForCreation>> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

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

    @BelongsTo(() => TimeTrackingHourCategoryDAO)
    hourCategory?: TimeTrackingHourCategoryDAO;

    @Column({
        allowNull: false,
        comment: '{ operator: "AND" | "OR", conditions: [{ field, valueType, operator, value }] }',
        type: DataType.JSONB,
    })
    logic!: {
        operator: 'AND' | 'OR';
        conditions: { field: string; valueType: string; operator: string; value: HourCategorizationValidValues }[];
    };

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

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

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