module.exports = {
  async up(queryInterface, Sequelize) {
    try {
      // 1. Users
      await queryInterface.addColumn('users', 'role', { type: Sequelize.DataTypes.ENUM('Student_Freelancer', 'Client', 'Admin') }).catch(console.error);
      await queryInterface.addColumn('users', 'name', { type: Sequelize.DataTypes.STRING }).catch(console.error);
      await queryInterface.addColumn('users', 'is_verified_student', { type: Sequelize.DataTypes.BOOLEAN, defaultValue: false }).catch(console.error);
      await queryInterface.addColumn('users', 'wallet_balance', { type: Sequelize.DataTypes.DECIMAL, defaultValue: 0.0 }).catch(console.error);
      await queryInterface.addColumn('users', 'skills', { type: Sequelize.DataTypes.ARRAY(Sequelize.DataTypes.STRING) }).catch(console.error);
      await queryInterface.addColumn('users', 'rating', { type: Sequelize.DataTypes.DECIMAL, defaultValue: 0.0 }).catch(console.error);

      // 2. Jobs
      await queryInterface.addColumn('jobs', 'budget', { type: Sequelize.DataTypes.DECIMAL }).catch(console.error);
      await queryInterface.removeColumn('jobs', 'status').catch(console.error);
      await queryInterface.sequelize.query('DROP TYPE IF EXISTS "enum_jobs_status" CASCADE;').catch(console.error);
      await queryInterface.addColumn('jobs', 'status', { type: Sequelize.DataTypes.ENUM('Open', 'In-Progress', 'Under_Review', 'Completed', 'Disputed') }).catch(console.error);

      // 3. Proposals
      await queryInterface.renameColumn('proposals', 'proposed_amount', 'bid_amount').catch(console.error);
      await queryInterface.removeColumn('proposals', 'status').catch(console.error);
      await queryInterface.sequelize.query('DROP TYPE IF EXISTS "enum_proposals_status" CASCADE;').catch(console.error);
      await queryInterface.addColumn('proposals', 'status', { type: Sequelize.DataTypes.ENUM('Pending', 'Accepted', 'Rejected') }).catch(console.error);

      // 4. Escrow Transactions
      await queryInterface.createTable('escrow_transactions', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        jobId: { type: Sequelize.DataTypes.UUID, references: { model: 'jobs', key: 'id' } },
        clientId: { type: Sequelize.DataTypes.UUID, references: { model: 'users', key: 'id' } },
        freelancerId: { type: Sequelize.DataTypes.UUID, references: { model: 'users', key: 'id' } },
        total_amount: { type: Sequelize.DataTypes.DECIMAL },
        client_fee: { type: Sequelize.DataTypes.DECIMAL },
        freelancer_fee: { type: Sequelize.DataTypes.DECIMAL },
        status: { type: Sequelize.DataTypes.ENUM('Held_in_Escrow', 'Released_to_Freelancer', 'Refunded') },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        updatedAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        deletedAt: { type: Sequelize.DataTypes.DATE }
      }).catch(console.error);

    } catch (err) {
      console.error(err);
    }
  },

  async down() {
  }
};