using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using CncModels.Entity;
using CncRepository.Base;
using CncRepository.Interface;

namespace CncRepository.Impl
{
    public class ScreenConfigRepository : BusinessRepository, IScreenConfigRepository
    {
        public ScreenConfigRepository(string connectionString) : base(connectionString) { }

        public List<ScreenConfig> GetAll()
        {
            using (var conn = CreateConnection())
            {
                return conn.Query<ScreenConfig>("SELECT * FROM cnc_screen_config").AsList();
            }
        }

        public List<ScreenConfig> GetEnabled()
        {
            using (var conn = CreateConnection())
            {
                string sql = "SELECT * FROM cnc_screen_config WHERE is_enabled = 1 ORDER BY sort_order ASC";
                return conn.Query<ScreenConfig>(sql).AsList();
            }
        }

        public ScreenConfig GetById(int id)
        {
            using (var conn = CreateConnection())
            {
                string sql = "SELECT * FROM cnc_screen_config WHERE id = @Id";
                return conn.QuerySingleOrDefault<ScreenConfig>(sql, new { Id = id });
            }
        }

        public int Create(ScreenConfig entity)
        {
            using (var conn = CreateConnection())
            {
                string sql = @"INSERT INTO cnc_screen_config (card_key, card_type, title, metric, dimension, sort_order, is_enabled, chart_config, created_at, updated_at)
                                VALUES (@CardKey, @CardType, @Title, @Metric, @Dimension, @SortOrder, @IsEnabled, @ChartConfig, @CreatedAt, @UpdatedAt); SELECT LAST_INSERT_ID();";
                return conn.ExecuteScalar<int>(sql, entity);
            }
        }

        public bool Update(ScreenConfig entity)
        {
            using (var conn = CreateConnection())
            {
                string sql = @"UPDATE cnc_screen_config SET card_key = @CardKey, card_type = @CardType, title = @Title, metric = @Metric, dimension = @Dimension, sort_order = @SortOrder, is_enabled = @IsEnabled, chart_config = @ChartConfig, updated_at = @UpdatedAt WHERE id = @Id";
                int affected = conn.Execute(sql, entity);
                return affected > 0;
            }
        }

        public bool Delete(int id)
        {
            using (var conn = CreateConnection())
            {
                string sql = "DELETE FROM cnc_screen_config WHERE id = @Id";
                return conn.Execute(sql, new { Id = id }) > 0;
            }
        }

        public bool ToggleEnabled(int id)
        {
            using (var conn = CreateConnection())
            {
                string sql = "UPDATE cnc_screen_config SET is_enabled = CASE WHEN is_enabled = 1 THEN 0 ELSE 1 END, updated_at = NOW() WHERE id = @Id";
                int affected = conn.Execute(sql, new { Id = id });
                return affected > 0;
            }
        }
    }
}
