module.exports = (sequelize, DataTypes) => {
  const CustomerPO = sequelize.define('CustomerPO', {
    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'
      }
    },
    po_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    order_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    po_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    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'
      }
    },
    cq_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerQuotation',
        key: 'id'
      }
    },
    sales_order_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'SalesOrder',
        key: 'id'
      },
      comment: 'Reference to Sales Order if Customer PO was created from Sales Order'
    },
    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
    },
    delivery_date: {
      type: DataTypes.DATEONLY,
      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', 'RELEASED', 'PARTIALLY_DELIVERED', 'DELIVERED', '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_po',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'po_number']
      },
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'order_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  CustomerPO.associate = function(models) {
    CustomerPO.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    CustomerPO.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    CustomerPO.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    CustomerPO.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    CustomerPO.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    CustomerPO.belongsTo(models.CustomerQuotation, { foreignKey: 'cq_id', as: 'customerQuotation' });
    CustomerPO.belongsTo(models.SalesOrder, { foreignKey: 'sales_order_id', as: 'salesOrder' });
    CustomerPO.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    CustomerPO.hasMany(models.CustomerPOItem, { foreignKey: 'po_id', as: 'items' });
    CustomerPO.hasMany(models.Delivery, { foreignKey: 'customer_po_id', as: 'deliveries' });
    CustomerPO.hasMany(models.SubcontractDC, { foreignKey: 'customer_po_id', as: 'subcontractDCs' });
    CustomerPO.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    CustomerPO.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return CustomerPO;
};

