package com.devjob.job.service;

import com.devjob.job.domain.Company;
import com.devjob.job.dto.CompanyCreateRequest;
import com.devjob.job.dto.CompanyResponse;
import com.devjob.common.exception.BusinessException;
import com.devjob.common.exception.ErrorCode;
import com.devjob.job.repository.CompanyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CompanyService {

    private final CompanyRepository companyRepository;

    @Transactional
    public CompanyResponse create(CompanyCreateRequest req) {
        Company company = Company.builder()
                .name(req.name())
                .location(req.location())
                .build();
        return CompanyResponse.from(companyRepository.save(company));
    }

    public CompanyResponse findById(Long id) {
        return companyRepository.findById(id)
                .map(CompanyResponse::from)
                .orElseThrow(() -> new BusinessException(ErrorCode.COMPANY_NOT_FOUND));
    }

    Company getEntity(Long id) {
        return companyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ErrorCode.COMPANY_NOT_FOUND));
    }
}
