module.exports = (sequelize, DataTypes) => {
  const PurchaseOrder = sequelize.define('PurchaseOrder', {
    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
    },
    po_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    rfq_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'RequestForQuotation',
        key: 'id'
      }
    },
    pr_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseRequisition',
        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
    },
    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', 'POSTED', 'CLOSED', 'CANCELLED'),
      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'
      }
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    is_edited: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    previous_status: {
      type: DataTypes.STRING(30),
      allowNull: true
    },
    rev_no: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    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: 'purchase_order',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'po_number']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['vendor_id']
      },
      {
        fields: ['rfq_id']
      },
      {
        fields: ['pr_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['po_date']
      }
    ]
  });

  PurchaseOrder.associate = function(models) {
    PurchaseOrder.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    PurchaseOrder.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    PurchaseOrder.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    PurchaseOrder.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    PurchaseOrder.belongsTo(models.RequestForQuotation, { foreignKey: 'rfq_id', as: 'requestForQuotation' });
    PurchaseOrder.belongsTo(models.PurchaseRequisition, { foreignKey: 'pr_id', as: 'purchaseRequisition' });
    PurchaseOrder.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    PurchaseOrder.hasMany(models.PurchaseOrderItem, { foreignKey: 'po_id', as: 'items' });
    PurchaseOrder.hasMany(models.PurchaseCosting, { foreignKey: 'po_id', as: 'purchaseCostings' });
    PurchaseOrder.hasMany(models.PurchaseOrderRateHistory, { foreignKey: 'po_id', as: 'rateHistory' });
    PurchaseOrder.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    PurchaseOrder.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    PurchaseOrder.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    PurchaseOrder.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    PurchaseOrder.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return PurchaseOrder;
};

