/**
 * Migration: Create Payment Orders and Commissions Tables
 * For the invitation commission system
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create payment_orders table
    await queryInterface.createTable('payment_orders', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      order_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: '订单编号',
      },
      user_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'user',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: '用户ID',
      },
      appointment_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'appointment',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: '关联预约单ID',
      },
      amount: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        comment: '支付金额',
      },
      service_content: {
        type: Sequelize.TEXT,
        allowNull: false,
        comment: '服务内容',
      },
      payment_time: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '支付时间',
      },
      notes: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注',
      },
      created_by: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'admin',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: '创建人(管理员ID)',
      },
      status: {
        type: Sequelize.ENUM('active', 'cancelled'),
        defaultValue: 'active',
        comment: '订单状态',
      },
      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'),
      },
    });

    // Add indexes for payment_orders
    await queryInterface.addIndex('payment_orders', ['user_id']);
    await queryInterface.addIndex('payment_orders', ['appointment_id']);
    await queryInterface.addIndex('payment_orders', ['status']);
    await queryInterface.addIndex('payment_orders', ['payment_time']);
    await queryInterface.addIndex('payment_orders', ['created_by']);

    // Create commissions table
    await queryInterface.createTable('commissions', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      inviter_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'user',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: '邀请人ID',
      },
      invitee_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'user',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: '被邀请人ID',
      },
      payment_order_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'payment_orders',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: '支付订单ID',
      },
      payment_amount: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        comment: '支付金额',
      },
      commission_rate: {
        type: Sequelize.DECIMAL(5, 4),
        allowNull: false,
        comment: '佣金比例(如0.02表示2%)',
      },
      commission_amount: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
        comment: '佣金金额',
      },
      status: {
        type: Sequelize.ENUM('credited', 'cancelled'),
        defaultValue: 'credited',
        comment: '佣金状态',
      },
      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'),
      },
    });

    // Add indexes for commissions
    await queryInterface.addIndex('commissions', ['inviter_id']);
    await queryInterface.addIndex('commissions', ['invitee_id']);
    await queryInterface.addIndex('commissions', ['payment_order_id']);
    await queryInterface.addIndex('commissions', ['status']);
  },

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