package com.social.media.util;

import com.social.media.dto.SocialNetworkDto;
import com.social.media.domain.entity.SocialNetwork;

/**
 * Utility class for mapping between SocialNetwork entity and SocialNetworkDto
 */
public class SocialNetworkMapper {
    
    private SocialNetworkMapper() {
        // Private constructor to prevent instantiation
    }
    
    /**
     * Converts SocialNetwork entity to SocialNetworkDto
     */
    public static SocialNetworkDto toDto(SocialNetwork entity) {
        if (entity == null) {
            return null;
        }
        
        SocialNetworkDto dto = new SocialNetworkDto();
        dto.setId(entity.getId());
        dto.setNetworkCode(entity.getNetworkCode());
        dto.setName(entity.getName());
        dto.setPlatform(entity.getPlatform());
        dto.setApiBaseUrl(entity.getApiBaseUrl());
        dto.setApiVersion(entity.getApiVersion());
        dto.setOauthUrl(entity.getOauthUrl());
        dto.setWebhookUrl(entity.getWebhookUrl());
        dto.setApiConfig(entity.getApiConfig());
        dto.setRateLimits(entity.getRateLimits());
        dto.setSupportedFeatures(entity.getSupportedFeatures());
        dto.setIsActive(entity.getIsActive());
        dto.setRequiresApproval(entity.getRequiresApproval());
        dto.setLogoUrl(entity.getLogoUrl());
        dto.setBrandColor(entity.getBrandColor());
        dto.setSortOrder(entity.getSortOrder());
        dto.setCreatedAt(entity.getCreatedAt());
        dto.setUpdatedAt(entity.getUpdatedAt());
        dto.setCreatedBy(entity.getCreatedBy());
        dto.setUpdatedBy(entity.getUpdatedBy());
        
        // Set derived fields
        dto.setIsReadyForProduction(entity.isReadyForProduction());
        dto.setDisplayName(entity.getDisplayName());
        dto.setPerHourRateLimit(entity.getRateLimit("perHour"));
        dto.setPerMinuteRateLimit(entity.getRateLimit("perMinute"));
        
        return dto;
    }
    
    /**
     * Converts SocialNetworkDto to SocialNetwork entity
     */
    public static SocialNetwork toEntity(SocialNetworkDto dto) {
        if (dto == null) {
            return null;
        }
        
        SocialNetwork entity = new SocialNetwork();
        entity.setId(dto.getId());
        entity.setNetworkCode(dto.getNetworkCode());
        entity.setName(dto.getName());
        entity.setPlatform(dto.getPlatform());
        entity.setApiBaseUrl(dto.getApiBaseUrl());
        entity.setApiVersion(dto.getApiVersion());
        entity.setOauthUrl(dto.getOauthUrl());
        entity.setWebhookUrl(dto.getWebhookUrl());
        entity.setApiConfig(dto.getApiConfig());
        entity.setRateLimits(dto.getRateLimits());
        entity.setSupportedFeatures(dto.getSupportedFeatures());
        entity.setIsActive(dto.getIsActive());
        entity.setRequiresApproval(dto.getRequiresApproval());
        entity.setLogoUrl(dto.getLogoUrl());
        entity.setBrandColor(dto.getBrandColor());
        entity.setSortOrder(dto.getSortOrder());
        entity.setCreatedAt(dto.getCreatedAt());
        entity.setUpdatedAt(dto.getUpdatedAt());
        entity.setCreatedBy(dto.getCreatedBy());
        entity.setUpdatedBy(dto.getUpdatedBy());
        
        return entity;
    }
    
    /**
     * Updates an existing SocialNetwork entity with data from SocialNetworkDto
     * Excludes ID, networkCode, createdAt, and other immutable fields
     */
    public static void updateEntityFromDto(SocialNetwork entity, SocialNetworkDto dto) {
        if (entity == null || dto == null) {
            return;
        }
        
        // Update mutable fields only
        entity.setName(dto.getName());
        entity.setPlatform(dto.getPlatform());
        entity.setApiBaseUrl(dto.getApiBaseUrl());
        entity.setApiVersion(dto.getApiVersion());
        entity.setOauthUrl(dto.getOauthUrl());
        entity.setWebhookUrl(dto.getWebhookUrl());
        entity.setApiConfig(dto.getApiConfig());
        entity.setRateLimits(dto.getRateLimits());
        entity.setSupportedFeatures(dto.getSupportedFeatures());
        entity.setIsActive(dto.getIsActive());
        entity.setRequiresApproval(dto.getRequiresApproval());
        entity.setLogoUrl(dto.getLogoUrl());
        entity.setBrandColor(dto.getBrandColor());
        entity.setSortOrder(dto.getSortOrder());
        entity.setUpdatedBy(dto.getUpdatedBy());
        
        // Don't update: id, networkCode, createdAt, createdBy
        // These should be immutable after creation
    }
    
    /**
     * Creates a basic DTO with essential fields for list views
     */
    public static SocialNetworkDto toBasicDto(SocialNetwork entity) {
        if (entity == null) {
            return null;
        }
        
        SocialNetworkDto dto = new SocialNetworkDto();
        dto.setId(entity.getId());
        dto.setNetworkCode(entity.getNetworkCode());
        dto.setName(entity.getName());
        dto.setPlatform(entity.getPlatform());
        dto.setIsActive(entity.getIsActive());
        dto.setRequiresApproval(entity.getRequiresApproval());
        dto.setLogoUrl(entity.getLogoUrl());
        dto.setBrandColor(entity.getBrandColor());
        dto.setSortOrder(entity.getSortOrder());
        dto.setDisplayName(entity.getDisplayName());
        dto.setIsReadyForProduction(entity.isReadyForProduction());
        
        return dto;
    }
    
    /**
     * Creates a detailed DTO with all configuration fields for admin views
     */
    public static SocialNetworkDto toDetailedDto(SocialNetwork entity) {
        if (entity == null) {
            return null;
        }
        
        SocialNetworkDto dto = toDto(entity);
        
        // Add additional computed fields for detailed view
        if (entity.getRateLimits() != null) {
            dto.setPerHourRateLimit(entity.getRateLimit("perHour"));
            dto.setPerMinuteRateLimit(entity.getRateLimit("perMinute"));
        }
        
        return dto;
    }
    
    /**
     * Creates a configuration-focused DTO for setup wizards
     */
    public static SocialNetworkDto toConfigDto(SocialNetwork entity) {
        if (entity == null) {
            return null;
        }
        
        SocialNetworkDto dto = new SocialNetworkDto();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setPlatform(entity.getPlatform());
        dto.setApiBaseUrl(entity.getApiBaseUrl());
        dto.setApiVersion(entity.getApiVersion());
        dto.setOauthUrl(entity.getOauthUrl());
        dto.setWebhookUrl(entity.getWebhookUrl());
        dto.setApiConfig(entity.getApiConfig());
        dto.setRateLimits(entity.getRateLimits());
        dto.setSupportedFeatures(entity.getSupportedFeatures());
        dto.setIsReadyForProduction(entity.isReadyForProduction());
        
        return dto;
    }
}
