package com.social.media.util;

import com.social.media.dto.SocialAccountDto;
import com.social.media.domain.entity.SocialAccount;

/**
 * Utility class for mapping between SocialAccount entity and SocialAccountDto
 */
public class SocialAccountMapper {
    
    private SocialAccountMapper() {
        // Private constructor to prevent instantiation
    }
    
    /**
     * Converts SocialAccount entity to SocialAccountDto
     */
    public static SocialAccountDto toDto(SocialAccount entity) {
        if (entity == null) {
            return null;
        }
        
        SocialAccountDto dto = new SocialAccountDto();
        dto.setId(entity.getId());
        dto.setCompanyId(entity.getCompanyId());
        dto.setSocialNetworkId(entity.getSocialNetworkId());
        dto.setUsername(entity.getUsername());
        dto.setDisplayName(entity.getDisplayName());
        dto.setAccountCode(entity.getAccountCode());
        dto.setProfileUrl(entity.getProfileUrl());
        dto.setAvatarUrl(entity.getProfilePhotoUrl()); // Entity uses profilePhotoUrl
        dto.setBio(entity.getBio());
        dto.setLocation(entity.getLocation());
        dto.setWebsite(entity.getWebsite());
        dto.setConnectionStatus(entity.getConnectionStatus() != null ? entity.getConnectionStatus().name() : null);
        dto.setActive(entity.getActive());
        dto.setVerified(entity.getVerified());
        dto.setIsPrivate(entity.getIsPrivate());
        dto.setAccountType(entity.getAccountType());
        dto.setBusinessCategory(entity.getBusinessCategory());
        dto.setResponsibleUserId(entity.getResponsibleUserId());
        dto.setFollowerTarget(entity.getFollowerTarget());
        // Convert BigDecimal to Double for engagement target
        dto.setEngagementTarget(entity.getEngagementTarget() != null ? entity.getEngagementTarget().doubleValue() : null);
        dto.setRegistrationDate(entity.getRegistrationDate());
        dto.setLastSyncDate(entity.getLastSyncDate());
        dto.setNextSyncDate(entity.getNextSyncDate());
        // dto.setLastPostDate(entity.getLastPostDate()); // Entity doesn't have this field yet
        dto.setLastMetricsUpdate(entity.getLastMetricsUpdate());
        dto.setLastSyncError(entity.getLastSyncError());
        dto.setMetrics(entity.getMetrics());
        // dto.setAdditionalData(entity.getAdditionalData()); // Entity doesn't have this field yet
        dto.setCreatedAt(entity.getCreatedAt());
        dto.setUpdatedAt(entity.getUpdatedAt());
        dto.setDeleted(entity.getDeleted());
        
        return dto;
    }
    
    /**
     * Converts SocialAccountDto to SocialAccount entity
     */
    public static SocialAccount toEntity(SocialAccountDto dto) {
        if (dto == null) {
            return null;
        }
        
        SocialAccount entity = new SocialAccount();
        entity.setId(dto.getId());
        entity.setCompanyId(dto.getCompanyId());
        entity.setSocialNetworkId(dto.getSocialNetworkId());
        entity.setUsername(dto.getUsername());
        entity.setDisplayName(dto.getDisplayName());
        entity.setAccountCode(dto.getAccountCode());
        entity.setProfileUrl(dto.getProfileUrl());
        entity.setProfilePhotoUrl(dto.getAvatarUrl()); // Entity uses profilePhotoUrl
        entity.setBio(dto.getBio());
        entity.setLocation(dto.getLocation());
        entity.setWebsite(dto.getWebsite());
        
        if (dto.getConnectionStatus() != null) {
            entity.setConnectionStatus(SocialAccount.ConnectionStatus.valueOf(dto.getConnectionStatus()));
        }
        
        entity.setActive(dto.getActive());
        entity.setVerified(dto.getVerified());
        entity.setIsPrivate(dto.getIsPrivate());
        entity.setAccountType(dto.getAccountType());
        entity.setBusinessCategory(dto.getBusinessCategory());
        entity.setResponsibleUserId(dto.getResponsibleUserId());
        entity.setFollowerTarget(dto.getFollowerTarget());
        // Convert Double to BigDecimal for engagement target
        entity.setEngagementTarget(dto.getEngagementTarget() != null ? 
            java.math.BigDecimal.valueOf(dto.getEngagementTarget()) : null);
        entity.setRegistrationDate(dto.getRegistrationDate());
        entity.setLastSyncDate(dto.getLastSyncDate());
        entity.setNextSyncDate(dto.getNextSyncDate());
        // entity.setLastPostDate(dto.getLastPostDate()); // Will need to add this field to entity
        entity.setLastMetricsUpdate(dto.getLastMetricsUpdate());
        entity.setLastSyncError(dto.getLastSyncError());
        entity.setMetrics(dto.getMetrics());
        // entity.setAdditionalData(dto.getAdditionalData()); // Will need to add this field to entity
        entity.setCreatedAt(dto.getCreatedAt());
        entity.setUpdatedAt(dto.getUpdatedAt());
        entity.setDeleted(dto.getDeleted());
        
        return entity;
    }
    
    /**
     * Updates an existing SocialAccount entity with data from SocialAccountDto
     * Excludes ID, createdAt, and other immutable fields
     */
    public static void updateEntityFromDto(SocialAccount entity, SocialAccountDto dto) {
        if (entity == null || dto == null) {
            return;
        }
        
        // Update mutable fields only
        entity.setDisplayName(dto.getDisplayName());
        entity.setProfileUrl(dto.getProfileUrl());
        entity.setProfilePhotoUrl(dto.getAvatarUrl()); // Entity uses profilePhotoUrl
        entity.setBio(dto.getBio());
        entity.setLocation(dto.getLocation());
        entity.setWebsite(dto.getWebsite());
        
        if (dto.getConnectionStatus() != null) {
            entity.setConnectionStatus(SocialAccount.ConnectionStatus.valueOf(dto.getConnectionStatus()));
        }
        
        entity.setActive(dto.getActive());
        entity.setVerified(dto.getVerified());
        entity.setIsPrivate(dto.getIsPrivate());
        entity.setAccountType(dto.getAccountType());
        entity.setBusinessCategory(dto.getBusinessCategory());
        entity.setResponsibleUserId(dto.getResponsibleUserId());
        entity.setFollowerTarget(dto.getFollowerTarget());
        // Convert Double to BigDecimal for engagement target
        entity.setEngagementTarget(dto.getEngagementTarget() != null ? 
            java.math.BigDecimal.valueOf(dto.getEngagementTarget()) : null);
        entity.setLastSyncError(dto.getLastSyncError());
        entity.setMetrics(dto.getMetrics());
        // entity.setAdditionalData(dto.getAdditionalData()); // Will need to add this field to entity
        
        // Don't update: id, companyId, socialNetworkId, username, accountCode, createdAt
        // These should be immutable after creation
    }
}
