package com.ffii.fpsms.m18.web

import com.ffii.core.utils.JwtTokenUtil
import com.ffii.fpsms.m18.M18Config
import com.ffii.fpsms.m18.service.M18MasterDataService
import com.ffii.fpsms.m18.service.M18PurchaseOrderService
import com.ffii.fpsms.m18.service.M18PurchaseQuotationService
import com.ffii.fpsms.modules.master.entity.ItemUom
import com.ffii.fpsms.modules.master.entity.Items
import com.ffii.fpsms.modules.master.entity.Shop
import com.ffii.fpsms.modules.master.entity.ShopRepository
import com.ffii.fpsms.modules.master.entity.projections.SearchId
import com.ffii.fpsms.modules.master.enums.ShopType
import com.ffii.fpsms.modules.master.service.ItemUomService
import com.ffii.fpsms.modules.master.service.ItemsService
import com.ffii.fpsms.modules.master.service.ShopService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
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("/m18")
class M18TestController (
    private val shopRepository: ShopRepository,
    private val shopService: ShopService,
    private val itemsService: ItemsService,
    private val m18MasterDataService: M18MasterDataService,
    private val m18PurchaseOrderService: M18PurchaseOrderService,
    private val m18Config: M18Config,
    private val itemUomService: ItemUomService,
    private val m18PurchaseQuotationService: M18PurchaseQuotationService,
) {
    var logger: Logger = LoggerFactory.getLogger(JwtTokenUtil::class.java)

    @GetMapping("/test1")
    fun test1(@RequestParam name: String, @RequestParam m18UomId: Long): Items? {
        val temp = itemsService.findByNameAndM18UomId(name, m18UomId)
        println(name)
        println(m18UomId)
        println(temp?.id)
        return temp
    }

    @GetMapping("/test2")
    fun test2(): List<Long>? {
        return shopRepository.findM18IdsByCodeRegexpAndTypeAndDeletedIsFalse("P06|P07", ShopType.SUPPLIER.value)
    }

    @GetMapping("/test3")
    fun test3(@RequestParam m18ItemId: Long): ItemUom? {
        val response = itemUomService.findPurchaseUnitByM18ItemId(m18ItemId)
        println(response?.item?.id)
        println(response?.uom?.id)
        return response
    }
    // --------------------------------------------- Master Data --------------------------------------------- ///
    @GetMapping("/master-data")
    fun m18MasterData() {
        // Master Data
        m18MasterDataService.saveUnits()
        m18MasterDataService.saveProducts()
        m18MasterDataService.saveVendors()
        m18MasterDataService.saveBusinessUnits()
        m18MasterDataService.saveCurrencies()
        m18MasterDataService.saveBoms()
    }

    // --------------------------------------------- Master Data --------------------------------------------- ///
    @GetMapping("/product")
    fun m18Products() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveProducts()
    }

    @GetMapping("/vendor")
    fun m18Vendor() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveVendors()
    }

    @GetMapping("/unit")
    fun m18Unit() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveUnits()
    }

    @GetMapping("/currency")
    fun m18Currency() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveCurrencies()
    }

    @GetMapping("/bom")
    fun m18Bom() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveBoms()
    }

    @GetMapping("/businessUnit")
    fun m18BusinessUnit() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18MasterDataService.saveBusinessUnits()
    }

    // --------------------------------------------- Purchase Order --------------------------------------------- ///
    @GetMapping("/po")
    fun m18PO() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18PurchaseOrderService.savePurchaseOrders()
    }

    // --------------------------------------------- Purchase Quotation --------------------------------------------- ///
    @GetMapping("/pq")
    fun m18PQ() {
        logger.info("Access token: ${m18Config.ACCESS_TOKEN}")
        m18PurchaseQuotationService.savePurchaseQuotations()
    }
}