Imports System.Collections.Generic
Imports System.Data
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Jobs.GraphQL
Imports DigitalData.Modules.Logging

Public Class GraphQLModel
    Private Database As MSSQLServer
    Private LogConfig As LogConfig
    Private Logger As Logger

    Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer)
        Database = pDatabase
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger()
    End Sub

    Public Function GetQueryList() As List(Of Query)
        Try
            Dim oQueryTable As DataTable = Database.GetDatatable("SELECT * FROM TBCUST_JOBRUNNER_QUERY WHERE ACTIVE = 1 ORDER BY SEQUENCE")
            Dim oQueryList As New List(Of Query)

            For Each oRow As DataRow In oQueryTable.Rows
                Dim oQuery As New Query With {
                    .Id = oRow.Item("GUID"),
                    .Name = oRow.Item("TITLE"),
                    .ClearBeforeFill = oRow.ItemEx("CLEAR_BEFORE_FILL", False),
                    .ConnectionId = oRow.ItemEx("CON_ID", 1), ' TODO: Connection String?
                    .DestinationTable = oRow.ItemEx("DESTINATION_TABLE", String.Empty),
                    .OperationName = oRow.ItemEx("OPERATION_NAME", String.Empty),
                    .MappingBasePath = oRow.ItemEx("MAPPING_BASE_PATH", String.Empty),
                    .QueryString = oRow.ItemEx("QUERY_STRING", String.Empty)
                }

                If oQuery.DestinationTable = String.Empty Then
                    Logger.Warn("Value [DestinationTable] could not be read. Configuration incomplete.")
                End If

                If oQuery.OperationName = String.Empty Then
                    Logger.Warn("Value [OperationName] could not be read. Configuration incomplete.")
                End If

                If oQuery.MappingBasePath = String.Empty Then
                    Logger.Warn("Value [MappingBasePath] could not be read. Configuration incomplete.")
                End If

                If oQuery.QueryString = String.Empty Then
                    Logger.Warn("Value [QueryString] could not be read. Configuration incomplete.")
                End If

                oQuery.MappingFields = GetQueryMapping(oQuery.Id)

                oQueryList.Add(oQuery)
            Next

            Return oQueryList
        Catch ex As Exception
            Logger.Error(ex)
            Return New List(Of Query)
        End Try
    End Function

    Public Function GetQueryMapping(pQueryId As Integer) As List(Of FieldMapping)
        Try
            Dim oSQL As String = "SELECT t2.* FROM TBCUST_JOBRUNNER_QUERY_MAPPING t
                                          JOIN TBCUST_JOBRUNNER_MAPPING t2 ON t.MAPPING_ID = t2.GUID
                                          WHERE t.QUERY_ID = {0}"
            Dim oMappingTable As DataTable = Database.GetDatatable(String.Format(oSQL, pQueryId))
            Dim oMappings As New List(Of FieldMapping)

            For Each oMapping As DataRow In oMappingTable.Rows
                oMappings.Add(New FieldMapping With {
                    .DestinationColumn = oMapping.Item("DestinationColumn"),
                    .SourcePath = oMapping.Item("SourcePath")
                })
            Next

            Return oMappings
        Catch ex As Exception
            Logger.Error(ex)
            Return New List(Of FieldMapping)
        End Try
    End Function

End Class
