using System;
using System.Data.SQLite;
using System.IO;

namespace OutlookAIAssistant.Services
{
    public class LocalCacheManager
    {
        private static string _dbPath;
        private static string _connectionString;

        public static void Initialize()
        {
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string folderPath = Path.Combine(appData, "OutlookAIAssistant");
            
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            _dbPath = Path.Combine(folderPath, "LocalCache.db");
            _connectionString = $"Data Source={_dbPath};Version=3;";

            if (!File.Exists(_dbPath))
            {
                SQLiteConnection.CreateFile(_dbPath);
                CreateTables();
            }
        }

        private static void CreateTables()
        {
            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();

                string createLogsTable = @"
                    CREATE TABLE IF NOT EXISTS AILogs (
                        Id INTEGER PRIMARY KEY AUTOINCREMENT,
                        ThreadId TEXT,
                        Prompt TEXT,
                        Response TEXT,
                        ModelUsed TEXT,
                        Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
                    );";

                using (var command = new SQLiteCommand(createLogsTable, connection))
                {
                    command.ExecuteNonQuery();
                }

                string createSettingsTable = @"
                    CREATE TABLE IF NOT EXISTS Settings (
                        Key TEXT PRIMARY KEY,
                        Value TEXT
                    );";

                using (var command = new SQLiteCommand(createSettingsTable, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }

        public static void LogInteraction(string threadId, string prompt, string response, string modelUsed)
        {
            try
            {
                using (var connection = new SQLiteConnection(_connectionString))
                {
                    connection.Open();
                    string insertQuery = @"
                        INSERT INTO AILogs (ThreadId, Prompt, Response, ModelUsed) 
                        VALUES (@threadId, @prompt, @response, @modelUsed)";

                    using (var command = new SQLiteCommand(insertQuery, connection))
                    {
                        command.Parameters.AddWithValue("@threadId", threadId ?? "");
                        command.Parameters.AddWithValue("@prompt", prompt);
                        command.Parameters.AddWithValue("@response", response);
                        command.Parameters.AddWithValue("@modelUsed", modelUsed);
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Failed to log interaction: " + ex.Message);
            }
        }
    }
}
