package com.samsung.sample.approval.internal.impl;

import com.samsung.approval.enums.ApprovalDocStatus;
import com.samsung.authentication.Account;
import com.samsung.core.entity.PagingResult;
import com.samsung.resource.FileManagerService;
import com.samsung.sample.approval.internal.InternalApprovalSampleService;
import com.samsung.sample.approval.internal.dao.InternalApprovalSampleDao;
import com.samsung.sample.approval.internal.dto.InternalSampleSearchDto;
import com.samsung.sample.approval.internal.entity.InternalApprovalSampleDocument;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class InternalApprovalSampleServiceImpl implements InternalApprovalSampleService {

    final
    InternalApprovalSampleDao internalApprovalSampleDao;


    final
    FileManagerService fileManagerService;


    public InternalApprovalSampleServiceImpl(InternalApprovalSampleDao internalApprovalSampleDao, FileManagerService fileManagerService) {
        this.internalApprovalSampleDao = internalApprovalSampleDao;
        this.fileManagerService = fileManagerService;
    }

    @Override
    public PagingResult<InternalApprovalSampleDocument> getSampleApprovalDocumentPagingList(InternalSampleSearchDto internalSampleSearchDto) {

        // 시스템 어드민이 아닌 경우 해당 사용자가 등록한 목록만 확인 가능.
        if(Account.currentUser().isSystemAdminUser()) {
            internalSampleSearchDto.setSystemAdmin(true);
        } else {
            internalSampleSearchDto.setSystemAdmin(false);
            internalSampleSearchDto.setFirstRegrId(Objects.requireNonNull(Account.currentUser()).getUserId());
        }

        return internalApprovalSampleDao.getSampleApprovalDocumentPagingList(internalSampleSearchDto);
    }

    @Override
    public InternalApprovalSampleDocument getSampleApprovalDocument(String docId) {
        return internalApprovalSampleDao.getSampleApprovalDocument(docId);
    }

    @Override
    @Transactional
    public void insertSampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        internalApprovalSampleDao.insertSampleApprovalDocument(internalApprovalSampleDocument);
    }
    
    @Override
    @Transactional
    public void modifySampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        internalApprovalSampleDao.modifySampleApprovalDocument(internalApprovalSampleDocument);
    }
    
    @Override
    @Transactional
    public void deleteSampleApprovalDocument(InternalApprovalSampleDocument internalApprovalSampleDocument) {
        internalApprovalSampleDao.deleteSampleApprovalDocument(internalApprovalSampleDocument);
    }

    @Override
    @Transactional
    public void updateSampleApprovalDocument(String docId, ApprovalDocStatus approvalDocStatus) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("docId", docId);
        condition.put("approvalDocStatus", approvalDocStatus);
        internalApprovalSampleDao.updateSampleApprovalDocument(condition);
    }
}
