package com.realnet.scheduler.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.realnet.fnd.response.EntityResponse;
import com.realnet.scheduler.Entity.JobEntity;
import com.realnet.scheduler.Repository.JobRepository;
import com.realnet.scheduler.services.SmartSchedulerService;

@RestController
@RequestMapping("/scheduler")
public class JobController {

	private final SmartSchedulerService scheduler;
	private final JobRepository jobRepository;

	public JobController(SmartSchedulerService scheduler, JobRepository jobRepository) {
		this.scheduler = scheduler;
		this.jobRepository = jobRepository;
	}

	@PostMapping("/create")
	public JobEntity createJob(@RequestBody JobEntity body) {

		return scheduler.createAndStartJob(body);
	}

	@PostMapping("/pause/{id}")
	public EntityResponse pauseJob(@PathVariable Long id) {
		scheduler.pauseJob(id);
		EntityResponse entityResponse = new EntityResponse("Job paused: " + id);
		return entityResponse;
	}

	@PostMapping("/resume/{id}")
	public EntityResponse resumeJob(@PathVariable Long id) {
		scheduler.resumeJob(id);
		return new EntityResponse("Job resumed: " + id);
	}

	@DeleteMapping("/stop/{id}")
	public EntityResponse stopJob(@PathVariable Long id) {
		scheduler.stopJob(id);
		return new EntityResponse("Job stopped: " + id);
	}

	@GetMapping("/list")
	public List<JobEntity> listJobs() {
		return jobRepository.findAll();
	}

	@GetMapping("/lake/{lakeId}")
	public JobEntity listJobs(@PathVariable Integer lakeId) {
		return jobRepository.getBylakeId(lakeId);
	}
}
