module.exports = (sequelize, DataTypes) => {
  const GateEntry = sequelize.define('GateEntry', {
    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'
      }
    },
    gate_entry_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Unique gate entry number'
    },
    gate_entry_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrder',
        key: 'id'
      },
      comment: 'Reference to Purchase Order'
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    invoice_number: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    lr_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Lorry Receipt / Transport Receipt Number'
    },
    lr_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    transporter_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    vehicle_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    driver_name: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    driver_contact: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    entry_time: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Time when material entered the gate'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'CHECKED', 'QC_PENDING', 'QC_APPROVED', 'QC_REJECTED', 'GRN_READY', 'GRN_CREATED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT',
      comment: 'Status: DRAFT, CHECKED (ready for stores/QC), QC_PENDING, QC_APPROVED, QC_REJECTED, GRN_READY, GRN_CREATED'
    },
    receiving_status: {
      type: DataTypes.ENUM('NOT_RECEIVED', 'PARTIAL_RECEIVED', 'FULLY_RECEIVED'),
      allowNull: true,
      comment: 'Overall receiving status: NOT_RECEIVED, PARTIAL_RECEIVED, FULLY_RECEIVED'
    },
    destination_type: {
      type: DataTypes.ENUM('STORES', 'QC'),
      allowNull: true,
      comment: 'Where to move checked items: STORES (direct to GRN) or QC (needs QC approval)'
    },
    qc_required: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'Whether QC approval is required'
    },
    qc_approved: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'Whether QC has approved'
    },
    qc_approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    qc_approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    qc_rejection_reason: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Reason for QC rejection if rejected'
    },
    grn_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GoodsReceiptNote',
        key: 'id'
      },
      comment: 'GRN created from this gate entry'
    },
    total_items: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: 'Total number of items checked'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    received_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'Person who received inward inspection material'
    },
    checked_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who checked the materials at gate'
    },
    checked_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Time when materials were checked'
    },
    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: 'gate_entry',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'gate_entry_number']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['po_id']
      },
      {
        fields: ['vendor_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['gate_entry_date']
      },
      {
        fields: ['grn_id']
      }
    ]
  });

  GateEntry.associate = function (models) {
    GateEntry.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GateEntry.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    GateEntry.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    GateEntry.belongsTo(models.PurchaseOrder, { foreignKey: 'po_id', as: 'purchaseOrder' });
    GateEntry.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    GateEntry.belongsTo(models.GoodsReceiptNote, { foreignKey: 'grn_id', as: 'goodsReceiptNote' });
    GateEntry.belongsTo(models.User, { foreignKey: 'received_by', as: 'receivedByUser' });
    GateEntry.belongsTo(models.User, { foreignKey: 'checked_by', as: 'checkedByUser' });
    GateEntry.belongsTo(models.User, { foreignKey: 'qc_approved_by', as: 'qcApprovedByUser' });
    GateEntry.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    GateEntry.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    GateEntry.hasMany(models.GateEntryItem, { foreignKey: 'gate_entry_id', as: 'items' });
  };

  return GateEntry;
};

