using MySql.Data.MySqlClient;
using System.Collections.Generic;
using Aberwyn.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; // Add this namespace

namespace Aberwyn.Data
{
    public class BudgetService
    {
        private readonly IConfiguration _configuration;
        private readonly IHostEnvironment _env; // Add this field

        public BudgetService(IConfiguration configuration, IHostEnvironment env) // Update constructor
        {
            _configuration = configuration;
            _env = env; // Initialize the environment field
        }

        public MySqlConnection GetConnection()
        {
            var connectionString = _env.IsDevelopment() // Use the injected environment variable
                ? _configuration.GetConnectionString("DefaultConnection")
                : _configuration.GetConnectionString("ProductionConnection");

            return new MySqlConnection(connectionString);
        }

        public bool UpdateBudgetItem(BudgetItem item)
        {
            using (var connection = GetConnection())
            {
                connection.Open();

                string query = @"
                    UPDATE tblBudgetItems 
                    SET Name = @name, Amount = @amount 
                    WHERE idtblBudgetItems = @id";

                using (var cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@name", item.Name);
                    cmd.Parameters.AddWithValue("@amount", item.Amount);
                    cmd.Parameters.AddWithValue("@id", item.ID);
                    return cmd.ExecuteNonQuery() > 0; // Returns true if one or more rows are updated
                }
            }
        }

        public List<BudgetItem> GetBudgetItems(int month, int year)
        {
            var budgetItems = new List<BudgetItem>();

            using (var connection = GetConnection())
            {
                connection.Open();

                string query = @"
                    SELECT 
                        b.idtblBudgetItems AS id, 
                        b.Name AS item_name, 
                        b.Amount AS amount, 
                        c1.Name AS category, 
                        b.Month, 
                        b.Year, 
                        b.Description AS description
                    FROM tblBudgetItems b
                    LEFT JOIN tblCategories c1 ON b.Category = c1.idtblCategories
                    WHERE b.Month = @month AND b.Year = @year";

                using (var cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@month", month);
                    cmd.Parameters.AddWithValue("@year", year);

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            budgetItems.Add(new BudgetItem
                            {
                                ID = reader.GetInt32("id"),
                                Name = reader.GetString("item_name"), // Updated alias
                                Amount = reader.GetDecimal("amount"),
                                Category = reader.GetString("category"),
                                Month = reader.GetInt32("Month"),
                                Year = reader.GetInt32("Year"),
                                Description = reader.IsDBNull(reader.GetOrdinal("description"))
                                    ? null
                                    : reader.GetString("description")
                            });
                        }
                    }
                }
            }

            return budgetItems;
        }

        public bool AddBudgetItem(BudgetItem item)
        {
            using (var connection = GetConnection())
            {
                connection.Open();

                string query = @"
                    INSERT INTO tblBudgetItems (Name, Amount, Category, Month, Year)
                    VALUES (@name, @amount, @category, @month, @year)";

                using (var cmd = new MySqlCommand(query, connection))
                {
                    cmd.Parameters.AddWithValue("@name", item.Name);
                    cmd.Parameters.AddWithValue("@amount", item.Amount);
                    cmd.Parameters.AddWithValue("@category", item.Category);
                    cmd.Parameters.AddWithValue("@month", item.Month);
                    cmd.Parameters.AddWithValue("@year", item.Year);
                    return cmd.ExecuteNonQuery() > 0; // Returns true if a row was inserted
                }
            }
        }*/

        // New method to fetch all categories
        public List<string> GetCategories()
        {
            var categories = new List<string>();

            using (var connection = GetConnection())
            {
                connection.Open();

                string query = "SELECT Name FROM tblCategories"; // Adjust based on your table structure

                using (var cmd = new MySqlCommand(query, connection))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            categories.Add(reader.GetString("Name"));
                        }
                    }
                }
            }

            return categories;
        }
    }
}
