'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('fabricator', {
      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 fabricator belongs to'
      },
      fabricator_code: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Unique fabricator code'
      },
      fabricator_name: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Fabricator full name'
      },
      employee_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'employee',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Link to employee if internal'
      },
      contact_number: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: 'Contact phone number'
      },
      email: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Email address'
      },
      qualification: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Welding qualification/certification'
      },
      certification_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Certification number'
      },
      certification_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Certification issue date'
      },
      certification_expiry: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Certification expiry date'
      },
      specialization: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Welding specialization (e.g., TIG, MIG, Arc)'
      },
      experience_years: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
        comment: 'Years of experience'
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Link to user account for digital signature'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Additional remarks'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'SUSPENDED'),
        allowNull: false,
        defaultValue: 'ACTIVE',
        comment: 'Fabricator 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: 'Fabricator Master for welding and fabrication personnel'
    });

    // Create indexes
    await queryInterface.addIndex('fabricator', ['company_id'], {
      name: 'idx_fabricator_company_id'
    });
    await queryInterface.addIndex('fabricator', ['company_id', 'fabricator_code'], {
      name: 'idx_fabricator_company_code',
      unique: true
    });
    await queryInterface.addIndex('fabricator', ['status'], {
      name: 'idx_fabricator_status'
    });
    await queryInterface.addIndex('fabricator', ['employee_id'], {
      name: 'idx_fabricator_employee_id'
    });
    await queryInterface.addIndex('fabricator', ['user_id'], {
      name: 'idx_fabricator_user_id'
    });
  },

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