package com.ffii.fpsms.modules.common.scheduler.web

import com.ffii.fpsms.modules.common.SettingNames
import com.ffii.fpsms.modules.common.scheduler.service.SchedulerService
import com.ffii.fpsms.modules.settings.service.SettingsService
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/scheduler")
class SchedulerController(
    val schedulerService: SchedulerService,
    val settingsService: SettingsService
) {
    @GetMapping("/testDetailedProd")
    fun testDetailedProd(): Any {
        schedulerService.getDetailedProdSchedule()
        return 200
    }

    // Cron Settings
    @GetMapping("/updateSetting/poCron")
    fun schedulePo(@RequestParam @Valid newCron: String) {
        settingsService.update(SettingNames.SCHEDULE_M18_PO, newCron);
        schedulerService.scheduleM18PoTask()
    }

    @GetMapping("/trigger/po")
    fun triggerPo(): String {
        schedulerService.getM18Po()
        return "M18 PO Sync Triggered Successfully"
    }

    @GetMapping("/trigger/do1")
    fun triggerDo1(): String {
        schedulerService.getM18Dos1()
        return "M18 DO1 Sync Triggered Successfully"
    }

    @GetMapping("/trigger/do2")
    fun triggerDo2(): String {
        schedulerService.getM18Dos2()
        return "M18 DO2 Sync Triggered Successfully"
    }

    @GetMapping("/trigger/master-data")
    fun triggerMasterData(): String {
        schedulerService.getM18MasterData()
        return "M18 Master Data Sync Triggered Successfully"
    }

    @GetMapping("/refresh-cron")
    fun refreshCron(): String {
        schedulerService.init()
        return "Cron Schedules Refreshed from Database"
    }
}