import { Sequelize, DataTypes, Model, Optional } from 'sequelize';
import { ClientMasterAttributes } from './client_master';
import { ProjectMasterAttributes } from './project_master';
import { UserAttributes } from './User';

export interface WeeklyPlanAttributes {
    id: number;
    task_name: string;
    type: 'Business Development' | 'Operations';
    user_id: number;
    client_id: number;
    project_id: number;
    status: 'ACTIVE' | 'COMPLETED';
    start_date: Date;
    end_date: Date;
    week_number?: number;
    is_active: boolean;
    executive_id?: number;
}

interface WeeklyPlanCreationAttributes extends Optional<WeeklyPlanAttributes, 'id'> {}

export class WeeklyPlan
    extends Model<WeeklyPlanAttributes, WeeklyPlanCreationAttributes>
    implements WeeklyPlanAttributes
{
    public static associate(models: any): void {
        WeeklyPlan.belongsTo(models.ClientMaster, {
            foreignKey: 'client_id',
            as: 'client',
        });
        WeeklyPlan.belongsTo(models.ProjectMaster, {
            foreignKey: 'project_id',
            as: 'project',
        });
        WeeklyPlan.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user',
        });
        WeeklyPlan.belongsTo(models.User, {
            foreignKey: 'executive_id',
            as: 'executive',
        });
    }

    public id!: number;
    public task_name!: string;
    public type!: 'Business Development' | 'Operations';
    public user_id!: number;
    public client_id!: number;
    public project_id!: number;
    public status!: 'ACTIVE' | 'COMPLETED';
    public start_date!: Date;
    public end_date!: Date;
    public week_number?: number;
    public is_active!: boolean;
    public executive_id?: number;

    public client?: ClientMasterAttributes;
    public project?: ProjectMasterAttributes;
    public user?: UserAttributes;
    public executive?: UserAttributes;

    public toJSON(): WeeklyPlanAttributes & {
        client?: ClientMasterAttributes;
        project?: ProjectMasterAttributes;
        user?: UserAttributes;
        executive?: UserAttributes;
    } {
        return {
            ...super.toJSON(),
            id: this.id,
            task_name: this.task_name,
            type: this.type,
            user_id: this.user_id,
            client_id: this.client_id,
            project_id: this.project_id,
            status: this.status,
            start_date: this.start_date,
            end_date: this.end_date,
            week_number: this.week_number,
            is_active: this.is_active,
            client: this.client,
            project: this.project,
            user: this.user,
            executive: this.executive,
        };
    }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
    WeeklyPlan.init(
        {
            id: {
                type: dataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            task_name: {
                type: dataTypes.STRING,
                allowNull: false,
            },
            type: {
                type: dataTypes.ENUM('Business Development', 'Operations'),
                allowNull: false,
            },
            user_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'Users',
                    key: 'id',
                },
            },
            client_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'client_masters',
                    key: 'id',
                },
            },
            project_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'project_masters',
                    key: 'id',
                },
            },
            status: {
                type: dataTypes.ENUM('ACTIVE', 'COMPLETED'),
                allowNull: false,
                defaultValue: 'ACTIVE',
            },
            start_date: {
                type: dataTypes.DATE,
                allowNull: false,
            },
            end_date: {
                type: dataTypes.DATE,
                allowNull: false,
            },
            week_number: {
                type: dataTypes.SMALLINT,
                allowNull: true,
            },
            is_active: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: true,
            },
            executive_id: {
                type: dataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: 'Users',
                    key: 'id',
                },
            },
        },
        {
            sequelize,
            modelName: 'WeeklyPlan',
            tableName: 'weekly_plans',
            timestamps: true,
        }
    );

    return WeeklyPlan;
};

