const PersonalUserRepository = require('../../../repositories/user/personal/PersonalUserRepository');
const { logger } = require('../../../middleware/logger');

class PersonalProfileService {
  static async completeProfile(userId, profileData, unitOfWork) {
    logger.info('PersonalProfileService.completeProfile:start', { userId });
    try {
      const required = [
        { key: 'dateOfBirth', label: 'Date of birth' },
        { key: 'nationality', label: 'Nationality' },
        { key: 'address', label: 'Street & house number' },
        { key: 'zip_code', label: 'Postal code' },
        { key: 'city', label: 'City' },
        { key: 'country', label: 'Country' },
        { key: 'accountHolderName', label: 'Account holder' },
        { key: 'iban', label: 'IBAN' }
      ];

      for (const field of required) {
        const value = (profileData[field.key] || '').toString().trim();
        if (!value) {
          throw new Error(`${field.label} is required.`);
        }
      }

      profileData.dateOfBirth = profileData.dateOfBirth.toString().trim();
      profileData.nationality = profileData.nationality.toString().trim();
      profileData.address = profileData.address.toString().trim();
      profileData.zip_code = profileData.zip_code.toString().trim();
      profileData.city = profileData.city.toString().trim();
      profileData.country = profileData.country.toString().trim();
      profileData.accountHolderName = profileData.accountHolderName.toString().trim();
      profileData.iban = profileData.iban.toString().trim();

      const repo = new PersonalUserRepository(unitOfWork);
      await repo.updateProfileAndMarkCompleted(userId, profileData);
      logger.info('PersonalProfileService.completeProfile:profile_completed', { userId });

      // Check if all steps are complete and set status to 'pending' if so
      const UserStatusService = require('../../status/UserStatusService');
      await UserStatusService.checkAndSetPendingIfComplete(userId, unitOfWork);
      logger.info('PersonalProfileService.completeProfile:pending_check_complete', { userId });
      logger.info('PersonalProfileService.completeProfile:success', { userId });
      return true;
    } catch (error) {
      logger.error('PersonalProfileService.completeProfile:error', { userId, error: error.message });
      throw error;
    }
  }

  static async updateBasic(userId, payload, unitOfWork) {
    logger.info('PersonalProfileService.updateBasic:start', { userId });
    const repo = new PersonalUserRepository(unitOfWork);
    const updated = await repo.updateBasicInfo(userId, payload);
    const UserStatusService = require('../../status/UserStatusService');
    await UserStatusService.checkAndSetPendingIfComplete(userId, unitOfWork);
    logger.info('PersonalProfileService.updateBasic:success', { userId });
    return updated;
  }

  static async updateBank(userId, payload, unitOfWork) {
    logger.info('PersonalProfileService.updateBank:start', { userId });
    const repo = new PersonalUserRepository(unitOfWork);
    const updated = await repo.updateBankInfo(userId, payload);
    const UserStatusService = require('../../status/UserStatusService');
    await UserStatusService.checkAndSetPendingIfComplete(userId, unitOfWork);
    logger.info('PersonalProfileService.updateBank:success', { userId });
    return updated;
  }
}

module.exports = PersonalProfileService;