using System.Data;

namespace TravelAgencyWinForms.Utils;

public static class PresentationHelper
{
    private static readonly Dictionary<string, string> Labels = new(StringComparer.OrdinalIgnoreCase)
    {
        ["audit_id"] = "ID",
        ["client_id"] = "ID клиента",
        ["employee_id"] = "ID сотрудника",
        ["position_id"] = "ID должности",
        ["tour_id"] = "ID тура",
        ["city_id"] = "ID города",
        ["tour_list_id"] = "ID рейса",
        ["booking_id"] = "ID бронирования",
        ["user_id"] = "ID пользователя",

        ["first_name"] = "Имя",
        ["last_name"] = "Фамилия",
        ["middle_name"] = "Отчество",
        ["client_name"] = "Клиент",
        ["client_phone"] = "Телефон клиента",
        ["client_email"] = "Email клиента",
        ["employee_name"] = "Сотрудник",
        ["position_name"] = "Должность",

        ["passport_data"] = "Паспорт",
        ["phone"] = "Телефон",
        ["email"] = "Email",
        ["registration_date"] = "Дата регистрации",
        ["birth_date"] = "Дата рождения",
        ["salary"] = "Зарплата",
        ["price"] = "Стоимость",

        ["name"] = "Название",
        ["tour_name"] = "Тур",
        ["city_name"] = "Город",
        ["cities"] = "Города",
        ["type"] = "Тур",
        ["start_date"] = "Дата начала",
        ["end_date"] = "Дата окончания",

        ["booking_number"] = "Номер",
        ["booking_date"] = "Дата оформления",
        ["status"] = "Статус",

        ["login"] = "Логин",
        ["role"] = "Роль",
        ["is_active"] = "Активен",

        ["table_name"] = "Таблица",
        ["operation"] = "Операция",
        ["record_id"] = "Код записи",
        ["changed_by"] = "Пользователь",
        ["changed_by_db_user"] = "Пользователь БД",
        ["changed_by_app_user"] = "Пользователь приложения",
        ["changed_at"] = "Дата изменения",
        ["old_data"] = "Было",
        ["new_data"] = "Стало"
    };

    private static readonly HashSet<string> HiddenViewColumns = new(StringComparer.OrdinalIgnoreCase)
    {
        "employee_id",
        "client_id",
        "tour_list_id",
        "tour_id",
        "city_id",
        "position_id",
        "user_id",
        "password_hash"
    };

    private static readonly HashSet<string> HiddenReportColumns = new(StringComparer.OrdinalIgnoreCase)
    {
        "audit_id",
        "booking_id",
        "employee_id",
        "client_id",
        "tour_list_id",
        "tour_id",
        "city_id",
        "position_id",
        "user_id",
        "password_hash"
    };

    public static string GetHeader(string columnName)
    {
        if (Labels.TryGetValue(columnName, out var label))
            return label;

        var words = columnName.Split('_', StringSplitOptions.RemoveEmptyEntries);
        return string.Join(' ', words.Select(w => char.ToUpper(w[0]) + w[1..]));
    }

    public static void ApplyHeaders(DataGridView grid)
    {
        foreach (DataGridViewColumn column in grid.Columns)
            column.HeaderText = GetHeader(column.Name);
    }

    public static void HideViewColumns(DataGridView grid)
    {
        foreach (DataGridViewColumn column in grid.Columns)
            if (HiddenViewColumns.Contains(column.Name))
                column.Visible = false;
    }

    public static void HideReportColumns(DataGridView grid)
    {
        foreach (DataGridViewColumn column in grid.Columns)
            if (HiddenReportColumns.Contains(column.Name))
                column.Visible = false;
    }

    public static string ShortDate(object? value)
    {
        if (value == null || value == DBNull.Value)
            return "—";
        if (value is DateTime dateTime)
            return dateTime.ToString("dd.MM.yyyy");
        return Convert.ToString(value) ?? "—";
    }
}
