module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      unique: true
    },
    name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    legal_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    pan: {
      type: DataTypes.STRING(16),
      allowNull: true
    },
    cin: {
      type: DataTypes.STRING(21),
      allowNull: true
    },
    registration_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    registration_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    address_line1: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    address_line2: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    city_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'City',
        key: 'id'
      }
    },
    state_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'State',
        key: 'id'
      }
    },
    country_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Country',
        key: 'id'
      }
    },
    pincode: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    email: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    website: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    financial_year_start: {
      type: DataTypes.STRING(5),
      allowNull: true,
      comment: 'Format: MM-DD (e.g., 04-01 for April 1)'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'SUSPENDED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    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: 'company',
    indexes: [
      {
        unique: true,
        fields: ['code']
      },
      {
        fields: ['status']
      },
      {
        fields: ['pan']
      }
    ]
  });

  Company.associate = function(models) {
    Company.hasMany(models.Plant, { foreignKey: 'company_id', as: 'plants' });
    Company.hasMany(models.GSTIN, { foreignKey: 'company_id', as: 'gstins' });
    Company.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
    Company.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
    Company.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
    Company.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
  };

  return Company;
};

