module.exports = (sequelize, DataTypes) => {
  const GSTIN = sequelize.define('GSTIN', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    gstin: {
      type: DataTypes.STRING(15),
      allowNull: false,
      unique: true
    },
    trade_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    legal_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    registration_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    cancellation_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
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    is_primary: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    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: 'gstin',
    indexes: [
      {
        unique: true,
        fields: ['gstin']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  GSTIN.associate = function(models) {
    GSTIN.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GSTIN.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    GSTIN.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
    GSTIN.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
    GSTIN.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
  };

  return GSTIN;
};

