'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('print_header', {
      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',
        comment: 'Company this print header belongs to'
      },
      header_code: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'Unique code for the print header'
      },
      header_name: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Name/description of the print header'
      },
      logo_path: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Path to company logo file'
      },
      logo_file_name: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: 'Original filename of the logo'
      },
      company_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Company name to display on print header'
      },
      former_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Former name of the company (e.g., Formerly Known as...)'
      },
      address: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Complete address for print header'
      },
      phone: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Phone number(s)'
      },
      fax: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Fax number'
      },
      email: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Email address'
      },
      website: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Website URL'
      },
      gstin: {
        type: Sequelize.STRING(15),
        allowNull: true,
        comment: 'GSTIN (GST Identification Number)'
      },
      quality_certification_logo_path: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Path to quality certification logo file'
      },
      quality_certification_logo_file_name: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: 'Original filename of the certification logo'
      },
      quality_certification_text: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Quality certification text (e.g., ISO 9001, DNV)'
      },
      quality_certification_body: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Certification body (e.g., DNV)'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      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('print_header', ['header_code'], { unique: true });
    await queryInterface.addIndex('print_header', ['company_id']);
    await queryInterface.addIndex('print_header', ['status']);
  },

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

