import { Sequelize, DataTypes, Model, Optional } from 'sequelize';
import { TaskAttributes } from './Task';

export interface TaskHistoryAttributes {
    id: number;
    task_id: number;
    status: 'assigned' | 'started' | 'escalated' | 'completed' | 'released'| 'pending';
    task_date: Date;
    is_active: boolean;
    user_id: number;
}

interface TaskHistoryCreationAttributes extends Optional<TaskHistoryAttributes, 'id'> {}

export class TaskHistory
    extends Model<TaskHistoryAttributes, TaskHistoryCreationAttributes>
    implements TaskHistoryAttributes
{
    public id!: number;
    public task_id!: number;
    public status!: 'assigned' | 'started' | 'escalated' | 'completed' | 'released';
    public task_date!: Date;
    public is_active!: boolean;
    public user_id!: number;

    public Task?: TaskAttributes;

    public toJSON(): TaskHistoryAttributes & {
        Task?: TaskAttributes;
    } {
        return {
            id: this.id,
            task_id: this.task_id,
            status: this.status,
            task_date: this.task_date,
            is_active: this.is_active,
            user_id: this.user_id,
            Task: this.Task,
        };
    }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
    TaskHistory.init(
        {
            id: {
                type: dataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            task_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'tasks',
                    key: 'id',
                },
            },
            status: {
                type: dataTypes.ENUM('assigned', 'started', 'escalated', 'completed', 'released','pending'),
                allowNull: false,
            },
            task_date: {
                type: dataTypes.DATE,
                allowNull: false,
                defaultValue: dataTypes.NOW,
            },
            is_active: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: true,
            },
            user_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'users',
                    key: 'id',
                },
            },
        },
        {
            sequelize,
            modelName: 'TaskHistory',
            tableName: 'task_histories',
            timestamps: true,
        }
    );

    return TaskHistory;
};