module.exports = (sequelize, DataTypes) => {
  const Fabricator = sequelize.define('Fabricator', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    fabricator_code: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Unique fabricator code'
    },
    fabricator_name: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: 'Fabricator full name'
    },
    employee_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Employee',
        key: 'id'
      },
      comment: 'Link to employee if internal'
    },
    contact_number: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'Contact phone number'
    },
    email: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Email address'
    },
    qualification: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Welding qualification/certification'
    },
    certification_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Certification number'
    },
    certification_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Certification issue date'
    },
    certification_expiry: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Certification expiry date'
    },
    specialization: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Welding specialization (e.g., TIG, MIG, Arc)'
    },
    experience_years: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      comment: 'Years of experience'
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'Link to user account for digital signature'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Additional remarks'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'SUSPENDED'),
      allowNull: false,
      defaultValue: 'ACTIVE',
      comment: 'Fabricator status'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'fabricator',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { unique: true, fields: ['company_id', 'fabricator_code'] },
      { fields: ['status'] },
      { fields: ['employee_id'] },
      { fields: ['user_id'] }
    ]
  });

  Fabricator.associate = function(models) {
    Fabricator.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    Fabricator.belongsTo(models.Employee, {
      foreignKey: 'employee_id',
      as: 'employee'
    });

    Fabricator.belongsTo(models.User, {
      foreignKey: 'user_id',
      as: 'user'
    });

    Fabricator.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    Fabricator.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });
  };

  return Fabricator;
};
