using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace TeleBot.Models
{
    public class UserSession
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string HashedUserId { get; set; } = string.Empty;
        public SessionState State { get; set; } = SessionState.MainMenu;
        
        // Telegram User Information (for customer creation)
        public long TelegramUserId { get; set; }
        public string TelegramUsername { get; set; } = string.Empty;
        public string TelegramDisplayName { get; set; } = string.Empty;
        public string TelegramFirstName { get; set; } = string.Empty;
        public string TelegramLastName { get; set; } = string.Empty;
        public ShoppingCart Cart { get; set; } = new();
        public Dictionary<string, object> TempData { get; set; } = new();
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        public DateTime LastActivityAt { get; set; } = DateTime.UtcNow;
        public DateTime ExpiresAt { get; set; }
        public bool IsEphemeral { get; set; } = true;
        public PrivacySettings Privacy { get; set; } = new();
        
        // Order flow data (temporary)
        public OrderFlowData? OrderFlow { get; set; }
        
        public static string HashUserId(long telegramUserId, string salt = "TeleBot-Privacy-Salt")
        {
            using var sha256 = SHA256.Create();
            var bytes = Encoding.UTF8.GetBytes($"{telegramUserId}:{salt}");
            var hash = sha256.ComputeHash(bytes);
            return Convert.ToBase64String(hash);
        }
        
        public bool IsExpired()
        {
            return DateTime.UtcNow > ExpiresAt;
        }
        
        public void UpdateActivity()
        {
            LastActivityAt = DateTime.UtcNow;
        }
    }
    
    public class PrivacySettings
    {
        public bool UseEphemeralMode { get; set; } = true;
        public bool UseTorOnly { get; set; } = false;
        public bool DisableAnalytics { get; set; } = true;
        public bool RequirePGP { get; set; } = false;
        public string? PGPPublicKey { get; set; }
        public bool EnableDisappearingMessages { get; set; } = true;
        public int DisappearingMessageTTL { get; set; } = 30; // seconds
    }
    
    public class OrderFlowData
    {
        public string? IdentityReference { get; set; }
        public string? ShippingName { get; set; }
        public string? ShippingAddress { get; set; }
        public string? ShippingCity { get; set; }
        public string? ShippingPostCode { get; set; }
        public string? ShippingCountry { get; set; } = "United Kingdom";
        public string? Notes { get; set; }
        public string? SelectedCurrency { get; set; }
        public bool UsePGPEncryption { get; set; }
        public OrderFlowStep CurrentStep { get; set; } = OrderFlowStep.CollectingName;
    }
    
    public enum OrderFlowStep
    {
        CollectingName,
        CollectingAddress,
        CollectingCity,
        CollectingPostCode,
        CollectingCountry,
        CollectingNotes,
        ReviewingOrder,
        SelectingPaymentMethod,
        ProcessingPayment,
        Complete
    }
    
    public enum SessionState
    {
        MainMenu,
        BrowsingCategories,
        ViewingProducts,
        ViewingProduct,
        ViewingCart,
        CheckoutFlow,
        ViewingOrders,
        ViewingOrder,
        PrivacySettings,
        CustomerSupport,
        Help
    }
}