'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('delivery', {
      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'
        }
      },
      delivery_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      delivery_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'business_partner',
          key: 'id'
        }
      },
      customer_address_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'bp_address',
          key: 'id'
        }
      },
      so_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'sales_order',
          key: 'id'
        }
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      sub_total: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      discount_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      tax_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      total_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'POSTED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      posted_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      posted_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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', ['company_id', 'plant_id', 'fiscal_year_id', 'delivery_number'], {
      unique: true,
      name: 'unique_delivery_number'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('delivery');
  }
};

