module.exports = (sequelize, DataTypes) => {
  const SalesQuotation = sequelize.define('SalesQuotation', {
    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: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    sq_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    sq_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    valid_until: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_address_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BPAddress',
        key: 'id'
      }
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    payment_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    delivery_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    sub_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'CONVERTED', 'EXPIRED', 'CLOSED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    submitted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    converted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    converted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    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: 'sales_quotation',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'sq_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['sq_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  SalesQuotation.associate = function(models) {
    SalesQuotation.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SalesQuotation.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SalesQuotation.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    SalesQuotation.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    SalesQuotation.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    SalesQuotation.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    SalesQuotation.hasMany(models.SalesQuotationItem, { foreignKey: 'sq_id', as: 'items' });
    SalesQuotation.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SalesQuotation.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    SalesQuotation.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    SalesQuotation.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    SalesQuotation.belongsTo(models.User, { foreignKey: 'converted_by', as: 'convertedByUser' });
  };

  return SalesQuotation;
};

