using LAPS_XMLQC_Service.Models;
using Microsoft.Extensions.Configuration;
using Npgsql;
using System.Collections.Generic;

namespace LAPS_XMLQC_Service.Services
{
    public class RegexPatternService
    {
        private readonly string _connectionString;

        public RegexPatternService(IConfiguration configuration)
        {
            _connectionString = configuration.GetConnectionString("DbConnection");
        }

        public List<RegexPattern> GetRegexPatterns()
        {
            var patterns = new List<RegexPattern>();

            using (var connection = new NpgsqlConnection(_connectionString))
            {
                connection.Open();
                using (var command = new NpgsqlCommand("SELECT id, pattern from tblregexpatterns", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            patterns.Add(new RegexPattern
                            {
                                Id = reader.GetInt32(0),
                                Pattern = reader.GetString(1)
                            });
                        }
                    }
                }
            }

            return patterns;
        }
    }
}
