package com.social.media.application.company.handler;

import com.social.media.application.company.query.GetCompanyByIdQuery;
import com.social.media.application.company.dto.CompanyResponseDto;
import com.social.media.domain.company.aggregate.Company;
import com.social.media.domain.company.repository.CompanyRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Optional;

/**
 * Handler for GetCompanyByIdQuery
 */
@Service
public class GetCompanyByIdQueryHandler {
    
    private final CompanyRepository companyRepository;
    
    public GetCompanyByIdQueryHandler(CompanyRepository companyRepository) {
        this.companyRepository = companyRepository;
    }
    
    @Transactional(readOnly = true)
    public Optional<CompanyResponseDto> handle(GetCompanyByIdQuery query) {
        return companyRepository.findById(query.companyId())
            .map(this::mapToDto);
    }
    
    private CompanyResponseDto mapToDto(Company company) {
        return CompanyResponseDto.builder()
            .id(company.getId().value().toString())
            .name(company.getName())
            .email(company.getEmail().value())
            .website(company.getWebsite())
            .description(company.getActivitySector())
            .phone(company.getPhone() != null ? company.getPhone().value() : null)
            .address(company.getAddress() != null ? company.getAddress().street() : null)
            .city(company.getAddress() != null ? company.getAddress().city() : null)
            .state(company.getAddress() != null ? company.getAddress().state() : null)
            .country(company.getAddress() != null ? company.getAddress().country() : null)
            .postalCode(company.getAddress() != null ? company.getAddress().zipCode() : null)
            .industry(company.getActivitySector())
            .employeeCount(1) // Default value
            .timezone("America/Sao_Paulo") // Default value
            .active(company.getStatus().name().equals("ATIVO"))
            .logoUrl(company.getLogoUrl())
            .settings(Map.of("plan", company.getActivePlan().name()))
            .createdAt(company.getCreatedAt())
            .updatedAt(company.getUpdatedAt())
            .build();
    }
}

