package com.tasteby.service;

import com.tasteby.domain.Channel;
import com.tasteby.mapper.ChannelMapper;
import com.tasteby.util.IdGenerator;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChannelService {

    private final ChannelMapper mapper;

    public ChannelService(ChannelMapper mapper) {
        this.mapper = mapper;
    }

    public List<Channel> findAllActive() {
        return mapper.findAllActive();
    }

    public String create(String channelId, String channelName, String titleFilter) {
        String id = IdGenerator.newId();
        mapper.insert(id, channelId, channelName, titleFilter);
        return id;
    }

    public boolean deactivate(String channelId) {
        // Try deactivate by channel_id first, then by DB id
        int rows = mapper.deactivateByChannelId(channelId);
        if (rows == 0) {
            rows = mapper.deactivateById(channelId);
        }
        return rows > 0;
    }

    public Channel findByChannelId(String channelId) {
        return mapper.findByChannelId(channelId);
    }

    public void update(String id, String description, String tags, Integer sortOrder) {
        mapper.updateChannel(id, description, tags, sortOrder);
    }
}
