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


export interface ProjectStaffingAttributes {
    id: number;
    user_id: number;
    project_id: number;
    project_role_master_id: number;
    utilization_type: 'active' | 'upcoming';
    utilization_percentage: number;
    is_active: boolean;

    // Optional associations
    user?: UserAttributes;
    project?: ProjectMasterAttributes;
    project_role?: ProjectRoleMasterAttributes;
}

interface ProjectStaffingCreationAttributes extends Optional<ProjectStaffingAttributes, 'id'> {}

export class ProjectStaffing
    extends Model<ProjectStaffingAttributes, ProjectStaffingCreationAttributes>
    implements ProjectStaffingAttributes 
{
    public id!: number;
    public user_id!: number;
    public project_id!: number;
    public project_role_master_id!: number;
    public utilization_type!: 'active' | 'upcoming';
    public utilization_percentage!: number;
    public is_active!: boolean;

    // Optional associations
    public user?: UserAttributes;
    public project?: ProjectMasterAttributes;
    public project_role?: ProjectRoleMasterAttributes;

    public static associate(models: any): void {
        ProjectStaffing.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user',
        });
        ProjectStaffing.belongsTo(models.ProjectMaster, {
            foreignKey: 'project_id',
            as: 'project',
        });
        ProjectStaffing.belongsTo(models.ProjectRoleMaster, {
            foreignKey: 'project_role_master_id',
            as: 'role',
        });
    }

    public toJSON(): ProjectStaffingAttributes {
        return {
            ...super.toJSON(),
            id: this.id,
            user_id: this.user_id,
            project_id: this.project_id,
            project_role_master_id: this.project_role_master_id,
            utilization_type: this.utilization_type,
            utilization_percentage: this.utilization_percentage,
            is_active: this.is_active,
            user: this.user,
            project: this.project,
            project_role: this.project_role
        };
    }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
    ProjectStaffing.init(
        {
            id: {
                type: dataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            user_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'Users',
                    key: 'id',
                },
            },
            project_id: {
                type: dataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: 'project_masters',
                    key: 'id',
                },
            },
            project_role_master_id: {
                type: dataTypes.INTEGER,
                allowNull: true,
                references: {
                    model: 'project_role_masters',
                    key: 'id',
                },
            },
            utilization_type: {
                type: dataTypes.ENUM('active', 'upcoming'),
                allowNull: false,
            },
            utilization_percentage: {
                type: dataTypes.INTEGER,
                allowNull: false,
            },
            is_active: {
                type: dataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: true,
            },
        },
        {
            sequelize,
            modelName: 'ProjectStaffing',
            tableName: 'project_staffings',
            timestamps: true,
        }
    );

    return ProjectStaffing;
};
