package com.tasteby.service;

import com.tasteby.domain.DaemonConfig;
import com.tasteby.mapper.DaemonConfigMapper;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DaemonConfigService {

    private final DaemonConfigMapper mapper;

    public DaemonConfigService(DaemonConfigMapper mapper) {
        this.mapper = mapper;
    }

    public DaemonConfig getConfig() {
        return mapper.getConfig();
    }

    public void updateConfig(Map<String, Object> body) {
        DaemonConfig current = mapper.getConfig();
        if (current == null) return;

        if (body.containsKey("scan_enabled")) {
            current.setScanEnabled(Boolean.TRUE.equals(body.get("scan_enabled")));
        }
        if (body.containsKey("scan_interval_min")) {
            current.setScanIntervalMin(((Number) body.get("scan_interval_min")).intValue());
        }
        if (body.containsKey("process_enabled")) {
            current.setProcessEnabled(Boolean.TRUE.equals(body.get("process_enabled")));
        }
        if (body.containsKey("process_interval_min")) {
            current.setProcessIntervalMin(((Number) body.get("process_interval_min")).intValue());
        }
        if (body.containsKey("process_limit")) {
            current.setProcessLimit(((Number) body.get("process_limit")).intValue());
        }
        mapper.updateConfig(current);
    }

    public void updateLastScan() {
        mapper.updateLastScan();
    }

    public void updateLastProcess() {
        mapper.updateLastProcess();
    }
}
