package com.spring.domain.post.batch;

import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import com.spring.domain.post.entity.Post;
import com.spring.domain.post.mapper.PostMapper;
import com.spring.infra.batch.AbstractBatchTask;
import com.spring.infra.batch.BatchJobInfo;
import com.spring.infra.db.orm.jpa.SecondaryJpaConfig;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@BatchJobInfo(
    group = "${batch-info.post-batch.group}",
    jobName = "${batch-info.post-batch.job-name}",
    cronExpression = "${batch-info.post-batch.cron-expression}",
    description = "${batch-info.post-batch.description}"
)
@RequiredArgsConstructor
public class PostCreateBatch extends AbstractBatchTask {

    private final PostMapper postMapper;

    @Autowired
    @Override
    public void setTransactionManager(@Qualifier(SecondaryJpaConfig.TRANSACTION_MANAGER) PlatformTransactionManager transactionManager) {
        super.setTransactionManager(transactionManager);
    }

    @Override
    protected Tasklet createTasklet() {
        return ((contribution, chunkContext) -> {
            postMapper.save(Post.builder().title("testTitle").content("testPost").build());
            return RepeatStatus.FINISHED;
        });
    }

}
