package dev_services

import (
	"cpone/db"
	"cpone/models"
	dbx "cpone/package/database"
	"fmt"

	"go.uber.org/zap"
)

func NewServicesKesimpulan(uStore db.AppStore) *KesimpulanServices {
	return &KesimpulanServices{
		KesimpulanStore: uStore,
	}
}

type KesimpulanServices struct {
	KesimpulanStore db.AppStore
}

func (k *KesimpulanServices) GetListKesimpulanLab(id string) ([]models.ModelKesimpulanLab, error) {
	logger, _ := zap.NewProduction()
	var listLab []models.ModelKesimpulanLab
	var dataTot []models.ModelKesimpulanLab

	qryTot := `select '' as nomor,
	'' as Test, sum(total) as total
	from(
	   select count(*) as total
			   FROM mgm_header
			   join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
			   join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
			   join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
			   left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

			   where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'L'
			   and Mgm_HeaderIsNormal = 'N'
	   group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType ) x`

	err := dbx.Handlex.Select(&dataTot, qryTot, id)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get total",
			zap.Any("id", id),
			zap.Any("total data", dataTot),
		)
		// return dataTot, fmt.Errorf("QUERY_FAILED")
	}

	// Extracting the Total value
	var totalLab string
	if len(dataTot) > 0 {
		totalLab = dataTot[0].Total
	}

	logger.Info("data",
		zap.Any("id", id),
		zap.Any("cek total", totalLab),
	)
	query := `SELECT ROW_NUMBER() OVER (ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC) AS nomor,
		ifnull(Nat_TestName ,Mcu_KelainanGroupName) as Test,
		cast(count(Mgm_HeaderT_OrderHeaderID)/?*100 as decimal(2,0))  as total

		FROM mgm_header
		join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
		join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
		join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
		left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

		where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'L'
		and Mgm_HeaderIsNormal = 'N'
		group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType  ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC
		LIMIT 3`

	err = dbx.Handlex.Select(&listLab, query, totalLab, id)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error get data lab",
			zap.Any("id", id),
			zap.Any("list data", listLab),
		)
		return listLab, fmt.Errorf("QUERY_FAILED")
	}

	logger.Info("data",
		zap.Any("id", id),
		zap.Any("cekkkk", listLab),
	)
	return listLab, nil
}

func (k *KesimpulanServices) GetListKesimpulanNonLab(id string) ([]models.ModelKesimpulanLab, error) {
	logger, _ := zap.NewProduction()
	var listNonLab []models.ModelKesimpulanLab
	var dataTot []models.ModelKesimpulanLab

	qryTot := `select '' as nomor,
	'' as Test, sum(total) as total
	from(
	   select count(*) as total
			   FROM mgm_header
			   join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
			   join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
			   join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
			   left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

			   where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'NL'
			   and Mgm_HeaderIsNormal = 'N'
	   group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType ) x`

	err := dbx.Handlex.Select(&dataTot, qryTot, id)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get total",
			zap.Any("id", id),
			zap.Any("total data", dataTot),
		)
		// return dataTot, fmt.Errorf("QUERY_FAILED")
	}

	// Extracting the Total value
	var totalLab string
	if len(dataTot) > 0 {
		totalLab = dataTot[0].Total
	}

	logger.Info("data",
		zap.Any("id", id),
		zap.Any("cek total", totalLab),
	)
	query := `SELECT ROW_NUMBER() OVER (ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC) AS nomor,
		ifnull(Nat_TestName ,Mcu_KelainanGroupName) as Test,
		cast(count(Mgm_HeaderT_OrderHeaderID)/?*100 as decimal(2,0))  as total

		FROM mgm_header
		join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
		join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
		join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
		left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

		where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'NL'
		and Mgm_HeaderIsNormal = 'N'
		group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType  ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC
		LIMIT 3`

	err = dbx.Handlex.Select(&listNonLab, query, totalLab, id)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error get data NON lab",
			zap.Any("id", id),
			zap.Any("list data", listNonLab),
		)
		return listNonLab, fmt.Errorf("QUERY_FAILED")
	}

	logger.Info("data",
		zap.Any("id", id),
		zap.Any("cekkkk", listNonLab),
	)
	return listNonLab, nil
}

func (k *KesimpulanServices) GetListKesimpulanFisik(id string) ([]models.ModelKesimpulanLab, error) {
	logger, _ := zap.NewProduction()
	var listFisik []models.ModelKesimpulanLab
	var dataTot []models.ModelKesimpulanLab

	qryTot := `select '' as nomor,
	'' as Test, sum(total) as total
	from(
	   select count(*) as total
			   FROM mgm_header
			   join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
			   join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
			   join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
			   left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

			   where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'F'
			   and Mgm_HeaderIsNormal = 'N'
	   group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType ) x`

	err := dbx.Handlex.Select(&dataTot, qryTot, id)
	if err != nil {
		defer logger.Sync()
		logger.Error("Error get total",
			zap.Any("id", id),
			zap.Any("total data", dataTot),
		)
		// return dataTot, fmt.Errorf("QUERY_FAILED")
	}

	// Extracting the Total value
	var totalFisik string
	if len(dataTot) > 0 {
		totalFisik = dataTot[0].Total
	}

	logger.Info("data",
		zap.Any("id", id),
		zap.Any("cek total", totalFisik),
	)
	query := `SELECT ROW_NUMBER() OVER (ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC) AS nomor,
		ifnull(Nat_TestName ,Mcu_KelainanGroupName) as Test,
		cast(count(Mgm_HeaderT_OrderHeaderID)/?*100 as decimal(2,0))  as total

		FROM mgm_header
		join mgm_detail on Mgm_DetailMgm_HeaderID = Mgm_HeaderID  and Mgm_DetailIsActive = 'Y'
		join mcu_kelainan on Mgm_DetailMcu_KelainanID = Mcu_KelainanID
		join mcu_kelainangroup on Mgm_DetailMcu_KelainanGroupID = Mcu_KelainanGroupID
		left join nat_test on Nat_TestCode = Mgm_HeaderNat_TestCode

		where Mgm_HeaderIsActive = 'Y' and Mgm_HeaderMgm_McuID = ? AND Mgm_HeaderType = 'F'
		and Mgm_HeaderIsNormal = 'N'
		group by Mgm_DetailMcu_KelainanID, Mgm_HeaderType  ORDER BY  count(Mgm_HeaderT_OrderHeaderID) DESC
		LIMIT 3`

	err = dbx.Handlex.Select(&listFisik, query, totalFisik, id)

	if err != nil {
		defer logger.Sync()
		logger.Error("Error get data fisik",
			zap.Any("id", id),
			zap.Any("list data", listFisik),
		)
		return listFisik, fmt.Errorf("QUERY_FAILED")
	}

	return listFisik, nil
}
