using System;
using System.Data;
using Npgsql;  // Вместо MySql.Data.MySqlClient

namespace MuseumWinForms
{
    public static class ExhibitRepository
    {
        private static string connectionString = "Server=edu-pg.itiscaf.ru;Port=5432;Database=dbwork;UserId=dyachkov_ya;Password=i5G&l405V27;";

        public static DataTable GetAllExhibits()
        {
            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();
                string sql = @"SELECT e.id_exhibit AS ""ID"", 
                                      e.type AS ""Тип"", 
                                      e.creation_date AS ""Дата создания"",
                                      e.copy_price AS ""Цена"",
                                      e.creation_cost AS ""Себестоимость"",
                                      c.full_name AS ""Автор"",
                                      e.id_museum AS ""ID музея""
                               FROM Exhibit e
                               LEFT JOIN Creator c ON e.id_creator = c.id_creator
                               ORDER BY e.id_exhibit";
                using (var cmd = new NpgsqlCommand(sql, conn))
                using (var adapter = new NpgsqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    return dt;
                }
            }
        }

        public static void AddExhibit(int id, string type, DateTime date, decimal price, decimal cost, int creatorId, int museumId)
        {
            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();
                string sql = @"INSERT INTO Exhibit (id_exhibit, type, creation_date, copy_price, creation_cost, id_creator, id_museum) 
                               VALUES (@id, @type, @date, @price, @cost, @creatorId, @museumId)";
                using (var cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.Parameters.AddWithValue("@type", type);
                    cmd.Parameters.AddWithValue("@date", date);
                    cmd.Parameters.AddWithValue("@price", price);
                    cmd.Parameters.AddWithValue("@cost", cost);
                    cmd.Parameters.AddWithValue("@creatorId", creatorId);
                    cmd.Parameters.AddWithValue("@museumId", museumId);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public static void UpdateExhibitPrice(int exhibitId, decimal newPrice)
        {
            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();
                string sql = "UPDATE Exhibit SET copy_price = @price WHERE id_exhibit = @id";
                using (var cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@price", newPrice);
                    cmd.Parameters.AddWithValue("@id", exhibitId);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public static void DeleteExhibit(int exhibitId)
        {
            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();
                string sql = "DELETE FROM Exhibit WHERE id_exhibit = @id";
                using (var cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@id", exhibitId);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}