using System;
using System.Data;
using Npgsql;  

namespace MuseumWinForms
{
    public static class DatabaseHelper
    {
        // Строка подключения для PostgreSQL
        private static string connectionString = "Server=edu-pg.itiscaf.ru;Port=5432;Database=dbwork;UserId=dyachkov_ya;Password=i5G&l405V27;";

        public static NpgsqlConnection GetConnection()
        {
            return new NpgsqlConnection(connectionString);
        }

        // Вызов PostgreSQL функции
        public static int GetExhibitCountByAuthor(int authorId)
        {
            using (var conn = GetConnection())
            {
                conn.Open();
                // PostgreSQL вызывает функцию через SELECT
                string sql = "SELECT get_exhibit_count_by_author(@author_id)";
                using (var cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@author_id", authorId);
                    object result = cmd.ExecuteScalar();
                    return Convert.ToInt32(result);
                }
            }
        }

        // Получить всех авторов
        public static DataTable GetAllCreators()
        {
            using (var conn = GetConnection())
            {
                conn.Open();
                string sql = "SELECT id_creator, full_name FROM Creator ORDER BY id_creator";
                using (var cmd = new NpgsqlCommand(sql, conn))
                using (var adapter = new NpgsqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    return dt;
                }
            }
        }
    }
}