'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class invoice extends Model {
    static associate(models) {
      invoice.hasOne(models.invoicedetails, { foreignKey: 'inv_id', as: 'invoiceDetails' });
      invoice.hasMany(models.invoice_trans, { foreignKey: 'inv_id', as: 'invoiceTrans' });
      invoice.hasMany(models.invoiceitem, { foreignKey: 'inv_id', as: 'invoiceitem' });
    }
  }
  invoice.init({
    inv_id: {
      primaryKey: true,
      type: DataTypes.STRING
    },
    inv_name: DataTypes.STRING,
    inv_status: DataTypes.STRING,
    phoneNumber: DataTypes.STRING,
    inv_type: DataTypes.STRING,
    content: DataTypes.TEXT,
    isFile: DataTypes.BOOLEAN,
    pdfName: DataTypes.STRING,
    pdfCreated: DataTypes.BOOLEAN,
    delivered: DataTypes.BOOLEAN,
    // === Skip Tracking Fields ===
    skipped_at: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null
    },
    skip_reason: {
      type: DataTypes.ENUM('auto_expired', 'manual', 'phone_error'),
      allowNull: true,
      defaultValue: null
    },
    skipped_by: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: null
    }
  }, {
    sequelize,
    modelName: 'invoice',
  });
  return invoice;
};