package com.ecep.contract.ds.project.service;

import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.project.repository.ProjectQuotationRepository;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectQuotation;

@Lazy
@Service
public class ProjectQuotationService implements IEntityService<ProjectQuotation> {
    private static final Logger logger = LoggerFactory.getLogger(ProjectQuotationService.class);


    @Lazy
    @Autowired
    private ProjectQuotationRepository repository;


    public ProjectQuotation findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<ProjectQuotation> findAll(Specification<ProjectQuotation> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    public List<ProjectQuotation> findAllByProject(Project project) {
        return repository.findAllByProject(project);
    }

    public ProjectQuotation save(ProjectQuotation approval) {
        return repository.save(approval);
    }

    @Override
    public Specification<ProjectQuotation> getSpecification(String searchText) {
        return null;
    }

    public void delete(ProjectQuotation approval) {
        repository.delete(approval);
    }


    public ProjectQuotation newInstanceByProject(Project project) {
        ProjectQuotation approval = new ProjectQuotation();
        approval.setProject(project);
        approval.setApplyTime(LocalDateTime.now());
        approval.setLevel(1);
        approval.setEvaluationFile(null);
        return approval;
    }

    public List<ProjectQuotation> findAll(Specification<ProjectQuotation> spec, Sort sort) {
        return repository.findAll(spec, sort);
    }
}
