import { BelongsTo, Column, CreatedAt, DataType, DeletedAt, ForeignKey, HasMany, Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript';
import { TimeTrackingCategorizedHourApprovalStepState } from '../../constants/timeTracking/categorizedHourApprovalStepStates';
import { TimeTrackingCategorizedHourDAO } from '../hourCategory/categorizedHour';
import { TimeTrackingPolicyApprovalStepDAO } from '../policies/approvalSteps/timeTrackingPolicyApprovalStepDAO';
import { TimeTrackingCategorizedHourApprovalStepPotentialApproverDAO } from './timeTrackingCategorizedHourApprovalStepPotentialApproverDAO';

export interface TimeTrackingCategorizedHourApprovalStepCreationAttributes {
    categorizedHourId: string;
    policyApprovalStepId: string;
    state: string;
    position: number;
    approverId: number | null;
    resolutionDate: Date | null;
    comment: string | null;
}

@Table({ tableName: 'TimeTrackingCategorizedHourApprovalSteps' })
export class TimeTrackingCategorizedHourApprovalStepDAO extends Model<
    TimeTrackingCategorizedHourApprovalStepDAO,
    TimeTrackingCategorizedHourApprovalStepCreationAttributes
> {
    @PrimaryKey
    @Column(DataType.UUID)
    id!: string;

    @ForeignKey(() => TimeTrackingCategorizedHourDAO)
    @Column(DataType.UUID)
    categorizedHourId!: string;

    @BelongsTo(() => TimeTrackingCategorizedHourDAO)
    categorizedHour?: TimeTrackingCategorizedHourDAO;

    @ForeignKey(() => TimeTrackingPolicyApprovalStepDAO)
    @Column({ allowNull: false, type: DataType.UUID })
    policyApprovalStepId!: string;

    @BelongsTo(() => TimeTrackingPolicyApprovalStepDAO)
    policyApprovalStep?: TimeTrackingPolicyApprovalStepDAO;

    @Column({
        allowNull: false,
        type: DataType.ENUM(...Object.values(TimeTrackingCategorizedHourApprovalStepState)),
    })
    state!: TimeTrackingCategorizedHourApprovalStepState;

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

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

    @Column({ allowNull: true, type: DataType.DATE })
    resolutionDate!: Date | null;

    @Column({ allowNull: true, type: DataType.TEXT })
    comment!: string | null;

    @HasMany(() => TimeTrackingCategorizedHourApprovalStepPotentialApproverDAO, 'approvalStepId')
    potentialApprovers?: TimeTrackingCategorizedHourApprovalStepPotentialApproverDAO[];

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

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

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