package com.social.media.infrastructure.persistence.repository;

import com.social.media.domain.campaign.aggregate.AutomationCampaign;
import com.social.media.domain.campaign.repository.AutomationCampaignRepository;
import com.social.media.domain.campaign.valueobject.*;
import com.social.media.infrastructure.persistence.entity.AutomationCampaignEntity;
import com.social.media.application.shared.security.AuthenticationService;
// import com.social.media.infrastructure.persistence.mapper.AutomationCampaignEntityMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Implementation of AutomationCampaignRepository using Spring Data JPA
 */
@Repository
@RequiredArgsConstructor
public class AutomationCampaignRepositoryImpl implements AutomationCampaignRepository {
    
    private final AutomationCampaignJpaRepository jpaRepository;
    private final AuthenticationService authenticationService;
    // private final AutomationCampaignEntityMapper mapper;
    
    private Long getCurrentUserId() {
        return authenticationService.getAuthenticatedUserInfo().getUserIdAsLong();
    }
    
    private Long getCurrentCompanyId() {
        return authenticationService.getAuthenticatedUserInfo().companyIdAsLong();
    }
    
    @Override
    public AutomationCampaign save(AutomationCampaign campaign) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public Optional<AutomationCampaign> findById(AutomationCampaignId campaignId) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public void deleteById(AutomationCampaignId campaignId) {
        jpaRepository.deleteById(campaignId.getValue());
    }
    
    @Override
    public boolean existsById(AutomationCampaignId campaignId) {
        return jpaRepository.existsById(campaignId.getValue());
    }
    
    @Override
    public List<AutomationCampaign> findByCompanyId(Long companyId) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findBySocialAccountId(Long socialAccountId) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByStatus(CampaignStatus status) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByType(CampaignType type) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByCompanyIdAndStatus(Long companyId, CampaignStatus status) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByCompanyIdAndType(Long companyId, CampaignType type) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public Optional<AutomationCampaign> findByCompanyIdAndName(Long companyId, String name) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public boolean existsByCompanyIdAndName(Long companyId, String name) {
        return jpaRepository.existsByCompanyIdAndName(companyId, name);
    }
    
    @Override
    public boolean existsByCampaignCode(String campaignCode) {
        // Temporary implementation using findAll and filter
        return jpaRepository.findAll().stream()
                .anyMatch(entity -> campaignCode.equals(entity.getCampaignCode()));
    }
    
    @Override
    public List<AutomationCampaign> findActiveCampaigns() {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findScheduledCampaigns(LocalDateTime from, LocalDateTime to) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findExpiredCampaigns(LocalDateTime before) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findCampaignsRequiringExecution() {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByCompanyIdWithPagination(Long companyId, int page, int size) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findByStatusWithPagination(CampaignStatus status, int page, int size) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public long countByCompanyId(Long companyId) {
        return jpaRepository.countByCompanyIdAndUserId(companyId, getCurrentUserId());
    }
    
    @Override
    public long countByCompanyIdAndStatus(Long companyId, CampaignStatus status) {
        AutomationCampaignEntity.CampaignStatusEntity entityStatus = mapToEntityStatus(status);
        return jpaRepository.countByCompanyIdAndStatusAndUserId(companyId, entityStatus, getCurrentUserId());
    }
    
    @Override
    public long countActiveCampaigns() {
        return jpaRepository.countByStatus(AutomationCampaignEntity.CampaignStatusEntity.ACTIVE);
    }
    
    @Override
    public List<AutomationCampaign> findCampaignsForExecution(LocalDateTime executionTime) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    @Override
    public List<AutomationCampaign> findCampaignsByDateRange(LocalDateTime startDate, LocalDateTime endDate) {
        // Implementação temporária sem mapper
        throw new UnsupportedOperationException("Mapper temporariamente desabilitado");
    }
    
    private AutomationCampaignEntity.CampaignStatusEntity mapToEntityStatus(CampaignStatus status) {
        return switch (status) {
            case DRAFT -> AutomationCampaignEntity.CampaignStatusEntity.DRAFT;
            case ACTIVE -> AutomationCampaignEntity.CampaignStatusEntity.ACTIVE;
            case PAUSED -> AutomationCampaignEntity.CampaignStatusEntity.PAUSED;
            case COMPLETED -> AutomationCampaignEntity.CampaignStatusEntity.COMPLETED;
            case CANCELLED -> AutomationCampaignEntity.CampaignStatusEntity.CANCELLED;
            case FAILED -> AutomationCampaignEntity.CampaignStatusEntity.FAILED;
        };
    }
}
