const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const CallPlanRealization = sequelize.define(
    "callplanrealization",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      callplan_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "callplans", key: "id" } },
      client_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "clients", key: "id" } },
      outlet_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "outlets", key: "id" } },
      start_date: { type: DataTypes.DATEONLY, allowNull: false },
      end_date: { type: DataTypes.DATEONLY, allowNull: false },
      color: { type: DataTypes.STRING, allowNull: true },
      textColor: { type: DataTypes.STRING, allowNull: true },
      realization_status: { type: DataTypes.STRING, allowNull: true },
      action: { type: DataTypes.STRING, allowNull: true },
      note: { type: DataTypes.TEXT, allowNull: true },
      data_source: { type: DataTypes.STRING, allowNull: true },
      doc_status: { type: DataTypes.INTEGER, allowNull: true },
      submitted_at: { type: DataTypes.DATE, allowNull: true },
      approved_at: { type: DataTypes.DATE, allowNull: true },

      latitude: { type: DataTypes.STRING, allowNull: true },
      longitude: { type: DataTypes.STRING, allowNull: true },
      address: { type: DataTypes.TEXT, allowNull: true },
      filename: { type: DataTypes.STRING, allowNull: true },
      takepicture_date: { type: DataTypes.DATEONLY, allowNull: true },

      clock_in: { type: DataTypes.TIME, allowNull: true },
      clock_out: { type: DataTypes.TIME, allowNull: true },
      uc_destination: { type: DataTypes.STRING, allowNull: true },
      clock_in_latitude: { type: DataTypes.STRING, allowNull: true },
      clock_in_longitude: { type: DataTypes.STRING, allowNull: true },
      clock_out_latitude: { type: DataTypes.STRING, allowNull: true },
      clock_out_longitude: { type: DataTypes.STRING, allowNull: true },
    },
    {
      tableName: "callplanrealizations",
      freezeTableName: true,
    }
  );

  CallPlanRealization.associate = (models) => {
    CallPlanRealization.belongsTo(models.CallPlan, { foreignKey: "callplan_id", as: "callplan", onDelete: "CASCADE" });
    CallPlanRealization.belongsTo(models.Client, { foreignKey: "client_id", as: "client" });
    CallPlanRealization.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet" });
  };

  return CallPlanRealization;
};
