'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('accounting_document', {
      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: true,
        references: {
          model: 'plant',
          key: 'id'
        }
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        }
      },
      fiscal_period_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'fiscal_period',
          key: 'id'
        }
      },
      document_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      document_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      posting_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      document_type: {
        type: Sequelize.ENUM('GRN', 'VENDOR_INVOICE', 'SALES_INVOICE', 'GOODS_RECEIPT', 'GOODS_ISSUE', 'STOCK_TRANSFER', 'STOCK_ADJUSTMENT', 'JOURNAL_ENTRY'),
        allowNull: false
      },
      reference_type: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      reference_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      reference_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        }
      },
      exchange_rate: {
        type: Sequelize.DECIMAL(18, 6),
        allowNull: false,
        defaultValue: 1.0
      },
      total_debit: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      total_credit: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'POSTED', 'REVERSED'),
        allowNull: false,
        defaultValue: 'POSTED'
      },
      reversed_document_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'accounting_document',
          key: 'id'
        }
      },
      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('accounting_document', ['company_id', 'document_number'], {
      unique: true,
      name: 'idx_accounting_document_company_doc_number'
    });

    await queryInterface.addIndex('accounting_document', ['document_date'], {
      name: 'idx_accounting_document_date'
    });

    await queryInterface.addIndex('accounting_document', ['posting_date'], {
      name: 'idx_accounting_document_posting_date'
    });

    await queryInterface.addIndex('accounting_document', ['fiscal_year_id'], {
      name: 'idx_accounting_document_fiscal_year'
    });

    await queryInterface.addIndex('accounting_document', ['fiscal_period_id'], {
      name: 'idx_accounting_document_fiscal_period'
    });

    await queryInterface.addIndex('accounting_document', ['document_type'], {
      name: 'idx_accounting_document_type'
    });

    await queryInterface.addIndex('accounting_document', ['reference_type', 'reference_id'], {
      name: 'idx_accounting_document_reference'
    });

    await queryInterface.addIndex('accounting_document', ['status'], {
      name: 'idx_accounting_document_status'
    });
  },

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

