'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('delivery_challan', {
      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' }
      },
      challan_no: { type: Sequelize.STRING(60), allowNull: false },
      dc_date: { type: Sequelize.DATEONLY, allowNull: false },
      dispatch_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fg_dispatch', key: 'id' }
      },
      invoice_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'sales_invoice', key: 'id' }
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'business_partner', key: 'id' }
      },
      customer_po_no: { type: Sequelize.STRING(80), allowNull: true },
      delivery_address: { type: Sequelize.TEXT, allowNull: true },
      transport_name: { type: Sequelize.STRING(120), allowNull: true },
      vehicle_no: { type: Sequelize.STRING(40), allowNull: true },
      driver_name: { type: Sequelize.STRING(120), allowNull: true },
      eway_bill_no: { type: Sequelize.STRING(80), allowNull: true },
      payment_type: {
        type: Sequelize.ENUM('CREDIT', 'COD'),
        allowNull: false,
        defaultValue: 'CREDIT'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'DELIVERED', 'CLOSED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      delivered_at: { type: Sequelize.DATE, allowNull: true },
      delivered_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' }
      },
      closed_at: { type: Sequelize.DATE, allowNull: true },
      closed_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('delivery_challan', ['company_id', 'plant_id', 'fiscal_year_id', 'challan_no'], {
      unique: true,
      name: 'uniq_delivery_challan_no'
    });
    await queryInterface.addIndex('delivery_challan', ['dispatch_id']);
    await queryInterface.addIndex('delivery_challan', ['invoice_id']);
    await queryInterface.addIndex('delivery_challan', ['customer_id']);
    await queryInterface.addIndex('delivery_challan', ['status']);
    await queryInterface.addIndex('delivery_challan', ['dc_date']);
  },

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

