'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('receipt_entry', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'company', key: 'id' }
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'plant', key: 'id' }
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' }
      },
      receipt_no: { type: Sequelize.STRING(60), allowNull: false },
      receipt_date: { type: Sequelize.DATEONLY, allowNull: false },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'business_partner', key: 'id' }
      },
      invoice_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'sales_invoice', key: 'id' }
      },
      delivery_challan_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'delivery_challan', key: 'id' }
      },
      invoice_total: { type: Sequelize.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
      paid_amount: { type: Sequelize.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
      balance_amount: { type: Sequelize.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
      payment_mode: {
        type: Sequelize.ENUM('CASH', 'BANK', 'UPI', 'CHEQUE', 'NEFT'),
        allowNull: false,
        defaultValue: 'CASH'
      },
      reference_no: { type: Sequelize.STRING(120), allowNull: true },
      collected_from: {
        type: Sequelize.ENUM('DELIVERY', 'OFFICE'),
        allowNull: false,
        defaultValue: 'OFFICE'
      },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      status: {
        type: Sequelize.ENUM('DRAFT', 'POSTED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      posted_at: { type: Sequelize.DATE, allowNull: true },
      posted_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' }
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' }
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' }
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('receipt_entry', ['company_id', 'plant_id', 'fiscal_year_id', 'receipt_no'], {
      unique: true,
      name: 'uniq_receipt_no'
    });
    await queryInterface.addIndex('receipt_entry', ['invoice_id']);
    await queryInterface.addIndex('receipt_entry', ['customer_id']);
    await queryInterface.addIndex('receipt_entry', ['delivery_challan_id']);
    await queryInterface.addIndex('receipt_entry', ['status']);
    await queryInterface.addIndex('receipt_entry', ['receipt_date']);
  },

  async down(queryInterface) {
    await queryInterface.dropTable('receipt_entry');
  }
};

