'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create Header Table
    await queryInterface.createTable('dimensional_report', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Company this report belongs to'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Plant where inspection is done'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Fiscal year'
      },
      report_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'Dimensional Report Number'
      },
      report_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'Report date'
      },
      part_name: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Part name'
      },
      drawing_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Drawing number'
      },
      order_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Order number'
      },
      order_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Order quantity'
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Customer reference'
      },
      customer_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Customer name'
      },
      revision_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Revision number'
      },
      machine_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'machine',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Machine reference'
      },
      machine_name: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Machine name'
      },
      quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Inspected quantity'
      },
      operator_name: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Operator name'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'General remarks'
      },
      inspected_by: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Inspector name'
      },
      inspected_by_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Inspector user reference'
      },
      inspection_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Inspection date'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT',
        comment: 'Document status'
      },
      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: {
        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')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'Dimensional Report Header (RI/QA/F/02)'
    });

    // Create Detail Table
    await queryInterface.createTable('dimensional_report_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      report_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'dimensional_report',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Reference to header'
      },
      line_no: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Line number/sequence'
      },
      description: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Description & Parameters'
      },
      drg_dim: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Drawing dimension'
      },
      reading_1: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Measured reading 1'
      },
      reading_2: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Measured reading 2'
      },
      reading_3: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Measured reading 3'
      },
      reading_4: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Measured reading 4'
      },
      reading_5: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Measured reading 5'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Line-level remarks'
      },
      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')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'Dimensional Report Detail Lines'
    });

    // Create indexes for header table
    await queryInterface.addIndex('dimensional_report', ['company_id'], {
      name: 'idx_dim_report_company_id'
    });
    await queryInterface.addIndex('dimensional_report', ['plant_id'], {
      name: 'idx_dim_report_plant_id'
    });
    await queryInterface.addIndex('dimensional_report', ['report_no'], {
      name: 'idx_dim_report_no',
      unique: true
    });
    await queryInterface.addIndex('dimensional_report', ['report_date'], {
      name: 'idx_dim_report_date'
    });
    await queryInterface.addIndex('dimensional_report', ['customer_id'], {
      name: 'idx_dim_report_customer_id'
    });
    await queryInterface.addIndex('dimensional_report', ['status'], {
      name: 'idx_dim_report_status'
    });

    // Create indexes for detail table
    await queryInterface.addIndex('dimensional_report_detail', ['report_id'], {
      name: 'idx_dim_report_detail_report_id'
    });
    await queryInterface.addIndex('dimensional_report_detail', ['report_id', 'line_no'], {
      name: 'idx_dim_report_detail_report_line',
      unique: true
    });
  },

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