module.exports = (sequelize, DataTypes) => {
  const CustomerQuotation = sequelize.define('CustomerQuotation', {
    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'
      }
    },
    cq_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    cq_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
    },
    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: 'customer_quotation',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'cq_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  CustomerQuotation.associate = function(models) {
    CustomerQuotation.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    CustomerQuotation.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    CustomerQuotation.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    CustomerQuotation.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    CustomerQuotation.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    CustomerQuotation.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    CustomerQuotation.hasMany(models.CustomerQuotationItem, { foreignKey: 'cq_id', as: 'items' });
    CustomerQuotation.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    CustomerQuotation.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return CustomerQuotation;
};

