using MailKit;
using System.Text.Json.Serialization;
using System.Xml;

namespace Services
{
    public class EmailMessage
    {
        [JsonIgnore]
        public MailKit.UniqueId? Uid { get; set; } // Pour charger le corps plus tard

        [JsonPropertyName("UidString")]
        public string? UidString
        {
            get => Uid != null ? Uid?.ToString() : null;
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    if (MailKit.UniqueId.TryParse(value, out var parsed))
                        Uid = parsed;
                    else
                        Uid = null; // ou log une erreur
                }
                else
                {
                    Uid = null;
                }
            }
        }
        /*
        public string UidString
        {
            get => Uid.ToString();
            set => Uid = UniqueId.Parse(value);
        }
        */
        public string Id { get; set; } = "";
        public string To { get; set; } = "";
        public string Cc { get; set; } = "";
        public bool IsDestinatairePrincipal { get; set; }=false;
        public string From { get; set; } = "";
        public string FromName { get; set; } = "";
        public string Subject { get; set; } = "";
        public string InReplyTo { get; set; } = "";
        public DateTime Date { get; set; }
        public string DateSTR { get => Date.ToString("dd/MM/yyyy HH:mm"); }

        public string Analyse { get; set; } = "";

        public string Preview { get; set; } = "";
        public string TextBody { get; set; } = "";
        public string TextBodyHTML { get; set; } = "";

        [JsonIgnore]
        public string TextBody_PJ
        {
            get
            {
                if (ContentPJ == null || ContentPJ.Count == 0)
                    return TextBody;

                var joined = string.Join("\n---\n", ContentPJ);
                return $"{TextBody}\n\n--- Contenu des pièces jointes ---\n{joined}";
            }
        }

        [JsonIgnore]
        public string ContentPJ_STR
        {
            get
            {
                if (ContentPJ == null || ContentPJ.Count == 0)
                    return "";

                var joined = string.Join("\n---\n", ContentPJ);
                return $"--- Contenu des pièces jointes ---\n{joined}";
            }
        }

        public string Resume { get; set; } = "";
        public string Categorie { get; set; } = "";
        public string Strategie { get; set; } = "";
        public string Reponse { get; set; } = "";
        public bool IsImportant { get; set; } = false;
        public bool IsUrgent { get; set; } = false;
        public int ImportanceScore { get; set; } = 0; // de 0 à 5
        public int UrgenceScore { get; set; } = 0;    // de 0 à 5

        public List<string>? ContentPJ { get; set; } = new();
        public int PJ_NBR
        {
            get
            {
                if (ContentPJ == null || ContentPJ.Count == 0)
                    return 0;
                return ContentPJ.Count;
            }
        }

        [JsonIgnore]
        public string PJ_NBR_STR
        {
            get
            {
                if (PJ_NBR > 0)
                    return "📎";
                return "";
            }
        }

        public bool IsPJ { get => ContentPJ != null && ContentPJ.Count > 0; }

        public string Priorite { get => $"Important : {(IsImportant ? "✔️" : "❌")} - Urgent : {(IsUrgent ? "⚠️" : "❌")}"; }
        public string ImportanceSTR => $"{ImportanceScore}/5";
        public string UrgenceSTR => $"{UrgenceScore}/5";

        public string ModeleIA { get; set; } = "";

        [JsonIgnore]
        public bool ToRemove { get; set; } = false;

        public bool PresenceSuspecte { get; set; } = false;

        [JsonIgnore]
        public string PresenceSuspecte_STR
        {
            get
            {
                if (PresenceSuspecte)
                    return "⚠️";
                return "";
            }
        }

        [JsonIgnore]
        public string IsDestinatairePrincipal_STR
        {
            get
            {
                if (IsDestinatairePrincipal)
                    return "📩";
                else
                    return "👥";
            }
        }
        [JsonIgnore]
        public string IsDestinatairePrincipal_STR_TXT
        {
            get
            {
                if (IsDestinatairePrincipal)
                    return "Vous êtes destinataire principal de ce mail";
                else
                    return "Vous êtes en copie de ce mail";
            }
        }
    }

    #region Classe MessageToSend
    public class MessageToSend
    {

        public string Object { get; set; } = "";
        public string DestinataireA { get; set; } = "";
        public string DestinataireCC { get; set; } = "";

        public string Type { get; set; } = "Professionnel";
        public string Ton { get; set; } = ""; 
        public string Posture { get; set; } = "";
        public string Couleur { get; set; } = "";
        public string NiveauDetail{ get; set; } = "";
        public bool VOUS_TU { get; set; } = true;// "true : 'Vous'" ou "false : 'Tu'"

        public string Query { get; set; } = "";
        public string Body { get; set; } = "...";
    }
    #endregion
}
