package com.its.op.service.its.oper;

import com.its.op.dao.repository.its.oper.TbWwwMemberRepository;
import com.its.op.dto.its.oper.TbWwwMemberDto;
import com.its.op.entity.its.oper.TbWwwMember;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbWwwMemberService {

    private final TbWwwMemberRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbWwwMember requireOne(String email) throws NoSuchElementException {
        Optional<TbWwwMember> info = this.repo.findById(email);
        if (info.isPresent()) {
            return info.get();
        }
        else {
            throw new NoSuchElementException("데이터가 존재하지 않습니다: " + email);
        }
    }

    // 전체 데이터 조회
    @Transactional(readOnly = true)
    public List<TbWwwMemberDto> findAll() {
        List<TbWwwMemberDto> result = new ArrayList<>();
        List<TbWwwMember> data = this.repo.findAll();
        for (TbWwwMember entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

    // 전체 데이터 조회
    @Transactional(readOnly = true)
    public List<TbWwwMemberDto> findAllList() {
        List<TbWwwMemberDto> result = new ArrayList<>();
        List<TbWwwMember> data = this.repo.findAllList();
        for (TbWwwMember entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

    // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
    @Transactional(readOnly = true)
    public TbWwwMemberDto findById(String email) {
        TbWwwMember entity = requireOne(email);
        return entity.toDto();
    }

    // 데이터 변경
    @Transactional
    public TbWwwMemberDto updateById(String email, TbWwwMemberDto.TbWwwMemberUpdReq req) {
        TbWwwMember entity = requireOne(email);
        entity.updateInfo(req);
        this.repo.save(entity);
        return entity.toDto();
    }

    // 데이터 변경 또는 생성-목록(데이터가 존재하면 업데이트 없으면 신규로 생성)
    @Transactional
    public List<TbWwwMemberDto> mergeInfoList(List<TbWwwMemberDto.TbWwwMemberUpdReq> reqList) {
        List<TbWwwMemberDto> result = new ArrayList<>();
        for (TbWwwMemberDto.TbWwwMemberUpdReq req : reqList) {
            result.add(mergeInfo(req));
        }
        return result;
    }

    // 데이터 변경 또는 생성-개별(데이터가 존재하면 업데이트 없으면 신규로 생성)
    @Transactional
    public TbWwwMemberDto mergeInfo(TbWwwMemberDto.TbWwwMemberUpdReq req) {
        TbWwwMember obj = req.toEntity();
        try {
            TbWwwMember entity = requireOne(req.getEmail());
            obj.setPassword(entity.getPassword());
        }
        catch(NoSuchElementException e_notfound) {
            obj.setPassword("");
        }

        this.repo.save(obj);
        return obj.toDto();
    }

    // 정보 삭제-개별, 데이터 존재하지 않으면 Exception
    @Transactional
    public TbWwwMemberDto deleteById(String email) {
        TbWwwMember entity = requireOne(email);
        this.repo.deleteById(email);
        return entity.toDto();
    }

    // 정보 삭제-목록, 존재하는 데이터 만 삭제
    @Transactional
    public List<TbWwwMemberDto> deleteByIds(List<String> ids) {
        List<TbWwwMemberDto> result = new ArrayList<>();
        for (String id : ids) {
            Optional<TbWwwMember> obj = this.repo.findById(id);
            if (obj.isPresent()) {
                this.repo.deleteById(id);
                result.add(obj.get().toDto());
            }
        }
        return result;
    }

    @Transactional
    public TbWwwMemberDto accountLockFree(String id) {
        TbWwwMember obj = requireOne(id);
        obj.setAccountLockFree();
        this.repo.save(obj);
        return obj.toDto();
    }
}
