//******************************************************************************************************
//  CustomFilterAdapterOperations.cs - Gbtc
//
//  Copyright © 2026, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
//  file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  01/15/2026 - J. Ritchie Carroll
//       Generated original version of source code.
//
//******************************************************************************************************

using System.Diagnostics;
using Gemstone.Data.Model;

namespace DNP3ConfigLoader.TableOperations;

/// <summary>
/// Provides operations for CustomFilterAdapter table records.
/// </summary>
public static class CustomFilterAdapterOperations
{
    /// <summary>
    /// Gets or creates a CustomFilterAdapter record by adapter name.
    /// </summary>
    public static CustomFilterAdapter? GetCustomFilterAdapterRecord(
        TableOperations<CustomFilterAdapter> adapterOps,
        Guid nodeID,
        string adapterName,
        string assemblyName,
        string typeName,
        string connectionString)
    {
        try
        {
            // Lookup adapter record by name
            CustomFilterAdapter? adapter = adapterOps.QueryRecordWhere("AdapterName = {0}", adapterName);

            if (adapter is not null)
            {
                // If the record exists, check if it matches the provided details
                if (adapter.NodeID == nodeID &&
                    adapter.AssemblyName == assemblyName &&
                    adapter.TypeName == typeName &&
                    adapter.ConnectionString == connectionString)
                {
                    return adapter;
                }
            }
            else
            {
                adapter = adapterOps.NewRecord();
                Debug.Assert(adapter is not null, "Failed to create new CustomFilterAdapter record.");
            }

            // Update record fields
            adapter.NodeID = nodeID;
            adapter.AdapterName = adapterName;
            adapter.AssemblyName = assemblyName;
            adapter.TypeName = typeName;
            adapter.ConnectionString = connectionString;

            if (adapter.ID == 0)
            {
                adapter.LoadOrder = 0;
                adapter.Enabled = true;
                adapterOps.AddNewRecord(adapter);

                // Re-query to get database-assigned ID
                adapter = adapterOps.QueryRecordWhere("AdapterName = {0}", adapterName);
            }
            else
            {
                adapterOps.UpdateRecord(adapter);
            }

            return adapter;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error occurred while reading or creating CustomFilterAdapter record for \"{adapterName}\": {ex.Message}");
            return null;
        }
    }
}
