package com.mydreamjob.company; import com.mydreamjob.common.exception.BusinessException; import com.mydreamjob.common.exception.ErrorCode; import com.mydreamjob.security.UserRoleGuard; import com.mydreamjob.verification.CompanyVerification; import com.mydreamjob.verification.CompanyVerificationRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service @RequiredArgsConstructor public class CompanyOnboardingService { private final UserRoleGuard userRoleGuard; private final CompanyRepository companyRepository; private final CompanyVerificationRepository verificationRepository; @Transactional(readOnly = true) public CompanyOnboardingResponse get(Long userId) { userRoleGuard.requireCompany(userId); Company company = companyRepository.findByOwnerUserId(userId) .orElseThrow(() -> new BusinessException(ErrorCode.COMPANY_NOT_FOUND)); CompanyVerification latest = verificationRepository.findTopByCompanyIdOrderByIdDesc(company.getId()) .orElse(null); String status; if (Boolean.TRUE.equals(company.getVerified())) { status = "VERIFIED"; } else if (latest == null) { status = "UNVERIFIED"; } else if ("PENDING".equals(latest.getStatus()) || "RESUBMITTED".equals(latest.getStatus())) { status = "PENDING"; } else if ("REJECTED".equals(latest.getStatus())) { status = "REJECTED"; } else if ("APPROVED".equals(latest.getStatus())) { throw new BusinessException(ErrorCode.COMPANY_ONBOARDING_INCONSISTENT_STATE); } else { throw new BusinessException(ErrorCode.COMPANY_ONBOARDING_INCONSISTENT_STATE); } List missing = new ArrayList<>(); if (isBlank(company.getName())) missing.add("name"); if (isBlank(company.getIndustry())) missing.add("industry"); if (isBlank(company.getDescription())) missing.add("description"); if (isBlank(company.getAddress())) missing.add("address"); List actions = switch (status) { case "UNVERIFIED", "REJECTED" -> List.of("UPDATE_PROFILE", "SUBMIT_VERIFICATION"); case "PENDING" -> List.of("UPDATE_PROFILE", "WAIT_REVIEW"); case "VERIFIED" -> List.of("ENTER_COMPANY_WORKSPACE"); default -> List.of(); }; CompanyOnboardingResponse.VerificationSummary verification = latest == null ? null : new CompanyOnboardingResponse.VerificationSummary(latest.getId(), latest.getStatus(), latest.getSubmittedAt(), latest.getDecidedAt()); CompanyOnboardingResponse.CompanySummary summary = new CompanyOnboardingResponse.CompanySummary( company.getId(), company.getName(), company.getLogoUrl(), company.getDescription(), company.getIndustry(), company.getWebsite(), company.getAddress(), company.getVerified()); return new CompanyOnboardingResponse(summary, status, verification, missing, actions); } private boolean isBlank(String value) { return value == null || value.isBlank(); } }