using EonaCat.Json;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;

namespace EonaCat.Logger.Server.Models;

public class LogEntry
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    public DateTime Timestamp { get; set; }

    public string ApplicationName { get; set; } = default!;
    public string ApplicationVersion { get; set; } = default!;
    public string Environment { get; set; } = default!;
    public string MachineName { get; set; } = default!;
    public int Level { get; set; }
    public string Category { get; set; } = default!;
    public string Message { get; set; } = default!;

    public string? Exception { get; set; }
    public string? StackTrace { get; set; }

    [Column(TypeName = "TEXT")]
    public string? PropertiesJson { get; set; }

    [NotMapped]
    public Dictionary<string, object>? Properties
    {
        get => string.IsNullOrEmpty(PropertiesJson)
            ? null
            : JsonHelper.ToObject<Dictionary<string, object>>(PropertiesJson);
        set => PropertiesJson = value == null ? null : JsonHelper.ToJson(value);
    }

    public string? UserId { get; set; }
    public string? SessionId { get; set; }
    public string? RequestId { get; set; }
    public string? CorrelationId { get; set; }
}

public class Application
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string ApiKey { get; set; } = Guid.NewGuid().ToString();
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public bool IsActive { get; set; } = true;
}