'use strict';
const { Model } = require('sequelize');
const { defaultKeys, modelDefaults, actionUsers } = require('../sequelize/defaults');
module.exports = (sequelize, DataTypes) => {
  class Order extends Model {
    static associate(models) {
      actionUsers(Order, models);
      Order.belongsTo(models.Branch, {
        targetKey: "branch_id",
        foreignKey: "branch_id",
        as: "branch",
      });
      Order.belongsTo(models.Customer, {
        targetKey: "customer_id",
        foreignKey: "customer_id",
        as: "customer",
      });      
      Order.belongsTo(models.Area, {
        targetKey: "area_id",
        foreignKey: "area_id",
        as: "area",
      });
      Order.belongsTo(models.Landmark, {
        targetKey: "landmark_id",
        foreignKey: "landmark_id",
        as: "landmark",
      });
      Order.hasMany(models.OrderDetails, {
        sourceKey: "order_id",
        foreignKey: "order_id",
        as: "orderDetails",
      }); 
      Order.hasMany(models.Collection, {
        sourceKey: "order_id",
        foreignKey: "order_id",
        as: "collectionDetails",
      }); 
    }
  }

  Order.init({
    ...defaultKeys("order_id"),
    tbl_ref_id: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    collection_day: {
      type: DataTypes.ENUM(["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]),
      unique: true,
      allowNull: false,
    },
    order_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
    },
    order_no: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    sub_amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    advance_amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
    },
    total_amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    payment_status: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: 'Unpaid',
    },
  }, modelDefaults(sequelize, 'orders'));
  return Order;
};