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

export interface PlanAttributes {
    id: number;
    task_id: number;
    user_id: number;
    plan_date: Date;
    plan_status: 'draft' | 'published';
    task_status: 'assigned' | 'started' | 'escalated' | 'completed' | 'released';
    is_highlight: boolean;
    is_active: boolean;
    assigned_by: number;
    is_draft: boolean;
    is_published: boolean;
    is_started?: boolean;
    stuck_comments?: string;
    plan_released?: boolean;
    is_escalated?: boolean;
}

interface PlanCreationAttributes extends Optional<PlanAttributes, 'id'> {}

export class Plan 
    extends Model<PlanAttributes, PlanCreationAttributes>
    implements PlanAttributes 
    
{
    public static associate(models: any): void {
        Plan.belongsTo(models.Task, {
            foreignKey: 'task_id',
            as: 'task'  // Note: case sensitive, must match your include
        });

        Plan.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user'  // Note: case sensitive, must match your include
        });
        Plan.belongsTo(models.User, {
            foreignKey: 'assigned_by',
            as: 'assigner'
        });
    }

    public id!: number;
    public task_id!: number;
    public user_id!: number;
    public plan_date!: Date;
    public plan_status!: 'draft' | 'published';
    public task_status!: 'assigned' | 'started' | 'escalated' | 'completed' | 'released';
    public is_highlight!: boolean;
    public is_active!: boolean;
    public assigned_by!: number;
    public is_draft!: boolean;
    public is_published!: boolean;
    public is_started?: boolean;
    public stuck_comments?: string;
    public plan_released?: boolean;
    public is_escalated?: boolean;

    public task?: TaskAttributes;
    public user?: UserAttributes;
    public assigner?: UserAttributes;


    public toJSON(): PlanAttributes & {
        task?: TaskAttributes;
        user?: UserAttributes;
        assigner?: UserAttributes;
    } {
        return {
            ...super.toJSON(),
            id: this.id,
            task_id: this.task_id,
            user_id: this.user_id,
            plan_date: this.plan_date,
            plan_status: this.plan_status,
            task_status: this.task_status,
            is_highlight: this.is_highlight,
            is_active: this.is_active,
            assigned_by: this.assigned_by,
            is_draft:this.is_draft,
            is_published:this.is_published,
            is_started:this.is_started,
            stuck_comments:this.stuck_comments,
            plan_released:this.plan_released,
            is_escalated:this.is_escalated,
            task: this.task,
            user: this.user
        };
    }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
    Plan.init(
        {
            id: {
                type: dataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            task_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'tasks',
                    key: 'id',
                },
            },
            user_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'users',
                    key: 'id',
                },
            },
            plan_date: {
                type: dataTypes.DATE,
                allowNull: true,
            },
            plan_status: {
                type: dataTypes.ENUM('draft', 'published'),
                allowNull: false,
                defaultValue: 'draft',
            },
            task_status: {
                type: dataTypes.ENUM('pending','assigned', 'started', 'escalated', 'completed', 'released'),
                allowNull: false,
            },
            is_highlight: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: false,
            },
            is_active: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: true,
            },
            assigned_by: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'users',
                    key: 'id',
                },
            },
            is_draft: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: false,
            },
            is_published: {
                type: dataTypes.BOOLEAN,
                allowNull: true,
                defaultValue: false,
            },
            is_started: {
                type: dataTypes.BOOLEAN,
                allowNull: true,
                defaultValue: false,
            },
            stuck_comments: {
                type: dataTypes.STRING(1000),
                allowNull: true,
            },
            is_escalated: {
                type: dataTypes.BOOLEAN,
                allowNull: true,
                defaultValue: false,
            },
            plan_released: {
                type: dataTypes.BOOLEAN,
                allowNull: true,
                defaultValue: false,
            },
        },
        {
            sequelize,
            modelName: 'Plan',
            tableName: 'plans',
            timestamps: true,
        }
    );

    return Plan;
};