const { logger } = require('../../middleware/logger');

class UserStatusRepository {
  constructor(unitOfWork) {
    this.unitOfWork = unitOfWork;
  }

  async getStatusByUserId(userId) {
    logger.info('UserStatusRepository.getStatusByUserId:start', { userId });
    try {
      const conn = this.unitOfWork.connection;
      const [rows] = await conn.query('SELECT * FROM user_status WHERE user_id = ?', [userId]);
      logger.info('UserStatusRepository.getStatusByUserId:success', { userId, found: rows.length > 0 });
      return rows.length > 0 ? rows[0] : null;
    } catch (error) {
      logger.error('UserStatusRepository.getStatusByUserId:error', { userId, error: error.message });
      throw error;
    }
  }

  async initializeUserStatus(userId, status = 'pending') {
    logger.info('UserStatusRepository.initializeUserStatus:start', { userId, status });
    try {
      const conn = this.unitOfWork.connection;
      await conn.query(
        `INSERT INTO user_status (user_id, status) VALUES (?, ?)
         ON DUPLICATE KEY UPDATE status = VALUES(status)`,
        [userId, status]
      );
      logger.info('UserStatusRepository.initializeUserStatus:success', { userId });
    } catch (error) {
      logger.error('UserStatusRepository.initializeUserStatus:error', { userId, error: error.message });
      throw error;
    }
  }

  async updateEmailVerified(userId) {
    logger.info('UserStatusRepository.updateEmailVerified:start', { userId });
    try {
      const conn = this.unitOfWork.connection;
      await conn.query(
        `UPDATE user_status 
         SET email_verified = TRUE, email_verified_at = CURRENT_TIMESTAMP 
         WHERE user_id = ?`,
        [userId]
      );
      logger.info('UserStatusRepository.updateEmailVerified:success', { userId });
    } catch (error) {
      logger.error('UserStatusRepository.updateEmailVerified:error', { userId, error: error.message });
      throw error;
    }
  }

  async markRegistrationComplete(userId) {
    logger.info('UserStatusRepository.markRegistrationComplete:start', { userId });
    try {
      const conn = this.unitOfWork.connection;
      await conn.query(
        `UPDATE user_status 
         SET registration_completed = TRUE,
             status = CASE WHEN is_admin_verified = 1 THEN 'active' ELSE 'pending' END
         WHERE user_id = ?`,
        [userId]
      );
      logger.info('UserStatusRepository.markRegistrationComplete:success', { userId });
    } catch (error) {
      logger.error('UserStatusRepository.markRegistrationComplete:error', { userId, error: error.message });
      throw error;
    }
  }

  async setPendingIfComplete(userId) {
    logger.info('UserStatusRepository.setPendingIfComplete:start', { userId });
    try {
      const status = await this.getStatusByUserId(userId);
      if (
        status &&
        status.email_verified &&
        status.profile_completed &&
        status.documents_uploaded &&
        status.contract_signed &&
        status.status === 'inactive'
      ) {
        const conn = this.unitOfWork.connection;
        await conn.query(
          `UPDATE user_status SET status = 'pending' WHERE user_id = ?`,
          [userId]
        );
        logger.info('UserStatusRepository.setPendingIfComplete:status_set_pending', { userId });
      }
      logger.info('UserStatusRepository.setPendingIfComplete:success', { userId });
    } catch (error) {
      logger.error('UserStatusRepository.setPendingIfComplete:error', { userId, error: error.message });
      throw error;
    }
  }
}


module.exports = UserStatusRepository;
