'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('business_partner', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      bp_number: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      bp_type: {
        type: Sequelize.ENUM('CUSTOMER', 'VENDOR', 'BOTH'),
        allowNull: false
      },
      name: {
        type: Sequelize.STRING(200),
        allowNull: false
      },
      legal_name: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      pan: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      aadhaar: {
        type: Sequelize.STRING(12),
        allowNull: true
      },
      registration_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      registration_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      tax_category: {
        type: Sequelize.ENUM('REGULAR', 'COMPOSITION', 'UNREGISTERED', 'SEZ', 'DEEMED_EXPORT'),
        allowNull: true
      },
      payment_terms: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Payment terms in days'
      },
      credit_limit: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: true,
        defaultValue: 0.00
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      gl_account_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'gl_account',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'BLOCKED', 'BLACKLISTED'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('business_partner', ['company_id', 'bp_number'], { unique: true });
    await queryInterface.addIndex('business_partner', ['bp_type']);
    await queryInterface.addIndex('business_partner', ['status']);
    await queryInterface.addIndex('business_partner', ['pan']);
    await queryInterface.addIndex('business_partner', ['effective_from', 'effective_to']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('business_partner');
  }
};

