//
//  DatabaseServiceTests.swift
//  oAITests
//
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (C) 2026 Rune Olsen

import Testing
import Foundation
@testable import oAI

@Suite("DatabaseService migrations, against a throwaway in-memory queue")
struct DatabaseServiceMigrationTests {

    @Test("All v1-v8 tables exist after migration")
    func allTablesExist() {
        let db = DatabaseService.makeInMemory()
        let expectedTables = [
            "conversations", "messages", "settings", "command_history",
            "email_logs", "message_metadata", "message_embeddings",
            "conversation_embeddings", "conversation_summaries",
        ]
        for table in expectedTables {
            #expect(db.tableExists(table), "expected table \(table) to exist")
        }
    }

    @Test("v4 adds modelId to messages and primaryModel to conversations")
    func v4AddsModelColumns() {
        let db = DatabaseService.makeInMemory()
        #expect(db.columnNames(in: "messages").contains("modelId"))
        #expect(db.columnNames(in: "conversations").contains("primaryModel"))
    }

    @Test("messages table has the expected v1 columns")
    func messagesTableColumns() {
        let db = DatabaseService.makeInMemory()
        let columns = Set(db.columnNames(in: "messages"))
        let expected: Set<String> = ["id", "conversationId", "role", "content", "tokens", "cost", "timestamp", "sortOrder"]
        #expect(expected.isSubset(of: columns))
    }

    @Test("message_metadata table has the expected v6 columns")
    func messageMetadataColumns() {
        let db = DatabaseService.makeInMemory()
        let columns = Set(db.columnNames(in: "message_metadata"))
        #expect(columns == ["message_id", "importance_score", "user_starred", "summary", "chunk_index"])
    }

    @Test("conversation_summaries table has the expected v8 columns")
    func conversationSummariesColumns() {
        let db = DatabaseService.makeInMemory()
        let columns = Set(db.columnNames(in: "conversation_summaries"))
        #expect(columns == ["id", "conversation_id", "start_message_index", "end_message_index", "summary", "token_count", "created_at", "summary_model"])
    }

    @Test("A nonexistent table reports as absent, not a crash")
    func nonexistentTableReportsAbsent() {
        let db = DatabaseService.makeInMemory()
        #expect(db.tableExists("not_a_real_table") == false)
    }

    @Test("Two in-memory instances are isolated from each other")
    func instancesAreIsolated() throws {
        let dbA = DatabaseService.makeInMemory()
        let dbB = DatabaseService.makeInMemory()

        _ = try dbA.saveConversation(name: "only in A", messages: [Message(role: .user, content: "hi")])

        #expect(try dbA.listConversations().count == 1)
        #expect(try dbB.listConversations().count == 0)
    }
}

@Suite("DatabaseService settings CRUD, against a throwaway in-memory queue")
struct DatabaseServiceSettingsTests {

    @Test("Round-trips a plain setting")
    func roundTripsSetting() {
        let db = DatabaseService.makeInMemory()
        db.setSetting(key: "theme", value: "dark")
        #expect((try? db.loadAllSettings()["theme"]) == "dark")
    }

    @Test("Deletes a setting")
    func deletesSetting() {
        let db = DatabaseService.makeInMemory()
        db.setSetting(key: "temp", value: "1")
        db.deleteSetting(key: "temp")
        #expect((try? db.loadAllSettings()["temp"]) == nil)
    }
}

@Suite("DatabaseService conversation + message persistence, against a throwaway in-memory queue")
struct DatabaseServiceConversationTests {

    @Test("Saving a conversation round-trips its messages via loadConversation")
    func savesAndLoadsConversation() throws {
        let db = DatabaseService.makeInMemory()
        let messages = [
            Message(role: .user, content: "hello"),
            Message(role: .assistant, content: "hi there"),
        ]
        let saved = try db.saveConversation(name: "Test Chat", messages: messages)

        let loaded = try db.loadConversation(id: saved.id)
        #expect(loaded?.0.name == "Test Chat")
        #expect(loaded?.1.map(\.content) == ["hello", "hi there"])
    }

    @Test("System messages are excluded from persistence")
    func systemMessagesExcluded() throws {
        let db = DatabaseService.makeInMemory()
        let messages = [
            Message(role: .system, content: "tool call"),
            Message(role: .user, content: "hello"),
        ]
        let saved = try db.saveConversation(name: "Test", messages: messages)
        let loaded = try db.loadConversation(id: saved.id)
        #expect(loaded?.1.count == 1)
        #expect(loaded?.1.first?.content == "hello")
    }
}

@Suite("DatabaseService usage statistics, against a throwaway in-memory queue")
struct DatabaseServiceUsageStatsTests {

    @Test("Overall stats aggregate tokens, cost, and message count across conversations")
    func overallStatsAggregate() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.saveConversation(name: "Chat A", messages: [
            Message(role: .user, content: "hi", tokens: 10, modelId: "claude-sonnet"),
            Message(role: .assistant, content: "hello", tokens: 20, cost: 0.01, modelId: "claude-sonnet"),
        ])
        _ = try db.saveConversation(name: "Chat B", messages: [
            Message(role: .user, content: "hey", tokens: 5, modelId: "gpt-4"),
            Message(role: .assistant, content: "hi", tokens: 15, cost: 0.02, modelId: "gpt-4"),
        ])

        let stats = try db.getOverallUsageStats()
        #expect(stats.totalMessages == 4)
        #expect(stats.totalTokens == 50)
        #expect(stats.hasCostData == true)
        #expect(abs(stats.totalCost - 0.03) < 0.0001)
    }

    @Test("Overall stats report no cost data when no message has a cost")
    func overallStatsNoCostData() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.saveConversation(name: "Chat", messages: [
            Message(role: .user, content: "hi", tokens: 10),
        ])

        let stats = try db.getOverallUsageStats()
        #expect(stats.hasCostData == false)
        #expect(stats.totalCost == 0)
    }

    @Test("Usage by model groups messages by modelId and sums their tokens/cost")
    func usageByModelGroups() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.saveConversation(name: "Chat A", messages: [
            Message(role: .assistant, content: "a1", tokens: 20, cost: 0.01, modelId: "claude-sonnet"),
        ])
        _ = try db.saveConversation(name: "Chat B", messages: [
            Message(role: .assistant, content: "b1", tokens: 15, cost: 0.02, modelId: "gpt-4"),
            Message(role: .assistant, content: "b2", tokens: 5, cost: 0.02, modelId: "gpt-4"),
        ])

        let byModel = try db.getUsageByModel()
        #expect(byModel.count == 2)

        let gpt4 = byModel.first { $0.modelId == "gpt-4" }
        #expect(gpt4?.messageCount == 2)
        #expect(gpt4?.totalTokens == 20)
        #expect(abs((gpt4?.totalCost ?? 0) - 0.04) < 0.0001)

        // gpt-4 has higher total cost than claude-sonnet, so it should sort first
        #expect(byModel.first?.modelId == "gpt-4")
    }

    @Test("Usage by model excludes messages with no modelId")
    func usageByModelExcludesNilModelId() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.saveConversation(name: "Chat", messages: [
            Message(role: .user, content: "hi", tokens: 10),
            Message(role: .assistant, content: "hello", tokens: 20, modelId: "claude-sonnet"),
        ])

        let byModel = try db.getUsageByModel()
        #expect(byModel.count == 1)
        #expect(byModel.first?.modelId == "claude-sonnet")
    }

    @Test("Usage by conversation joins conversation names and sorts by cost descending")
    func usageByConversationSortsByCost() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.saveConversation(name: "Cheap Chat", messages: [
            Message(role: .assistant, content: "a", tokens: 10, cost: 0.001, modelId: "m"),
        ])
        _ = try db.saveConversation(name: "Expensive Chat", messages: [
            Message(role: .assistant, content: "b", tokens: 10, cost: 0.05, modelId: "m"),
        ])

        let byConversation = try db.getUsageByConversation()
        #expect(byConversation.count == 2)
        #expect(byConversation.first?.name == "Expensive Chat")
    }

    @Test("Usage by conversation respects the limit parameter")
    func usageByConversationRespectsLimit() throws {
        let db = DatabaseService.makeInMemory()
        for i in 0..<5 {
            _ = try db.saveConversation(name: "Chat \(i)", messages: [
                Message(role: .assistant, content: "a", tokens: 10, cost: Double(i) * 0.01, modelId: "m"),
            ])
        }

        let byConversation = try db.getUsageByConversation(limit: 3)
        #expect(byConversation.count == 3)
    }
}

@Suite("DatabaseService folders, against a throwaway in-memory queue")
struct DatabaseServiceFolderTests {

    @Test("v9 adds the folders table and folderId to conversations")
    func v9AddsFolderSupport() {
        let db = DatabaseService.makeInMemory()
        #expect(db.tableExists("folders"))
        #expect(db.columnNames(in: "conversations").contains("folderId"))
    }

    @Test("Creating folders assigns increasing sort order")
    func createFolderAssignsSortOrder() throws {
        let db = DatabaseService.makeInMemory()
        let first = try db.createFolder(name: "Work")
        let second = try db.createFolder(name: "Personal")
        #expect(first.sortOrder == 0)
        #expect(second.sortOrder == 1)
    }

    @Test("listFolders returns folders sorted alphabetically, case-insensitive, regardless of creation order")
    func listFoldersOrdered() throws {
        let db = DatabaseService.makeInMemory()
        _ = try db.createFolder(name: "Work")
        _ = try db.createFolder(name: "apple")
        _ = try db.createFolder(name: "Personal")

        let folders = try db.listFolders()
        #expect(folders.map(\.name) == ["apple", "Personal", "Work"])
    }

    @Test("renameFolder updates the stored name")
    func renameFolderUpdatesName() throws {
        let db = DatabaseService.makeInMemory()
        let folder = try db.createFolder(name: "Work")
        try db.renameFolder(id: folder.id, name: "Projects")

        let folders = try db.listFolders()
        #expect(folders.first?.name == "Projects")
    }

    @Test("moveConversation files a conversation into a folder")
    func moveConversationFiles() throws {
        let db = DatabaseService.makeInMemory()
        let folder = try db.createFolder(name: "Work")
        let conversation = try db.saveConversation(name: "Chat", messages: [Message(role: .user, content: "hi")])

        try db.moveConversation(id: conversation.id, toFolder: folder.id)

        let loaded = try db.loadConversation(id: conversation.id)
        #expect(loaded?.0.folderId == folder.id)
    }

    @Test("moveConversation with nil folder unfiles a conversation")
    func moveConversationUnfiles() throws {
        let db = DatabaseService.makeInMemory()
        let folder = try db.createFolder(name: "Work")
        let conversation = try db.saveConversation(name: "Chat", messages: [Message(role: .user, content: "hi")])
        try db.moveConversation(id: conversation.id, toFolder: folder.id)

        try db.moveConversation(id: conversation.id, toFolder: nil)

        let loaded = try db.loadConversation(id: conversation.id)
        #expect(loaded?.0.folderId == nil)
    }

    @Test("Deleting a folder unfiles its conversations instead of deleting them")
    func deleteFolderUnfilesConversations() throws {
        let db = DatabaseService.makeInMemory()
        let folder = try db.createFolder(name: "Work")
        let conversation = try db.saveConversation(name: "Chat", messages: [Message(role: .user, content: "hi")])
        try db.moveConversation(id: conversation.id, toFolder: folder.id)

        try db.deleteFolder(id: folder.id)

        let folders = try db.listFolders()
        #expect(folders.isEmpty)

        let loaded = try db.loadConversation(id: conversation.id)
        #expect(loaded != nil)
        #expect(loaded?.0.folderId == nil)
    }

    @Test("listConversations reflects folderId")
    func listConversationsReflectsFolderId() throws {
        let db = DatabaseService.makeInMemory()
        let folder = try db.createFolder(name: "Work")
        let conversation = try db.saveConversation(name: "Chat", messages: [Message(role: .user, content: "hi")])
        try db.moveConversation(id: conversation.id, toFolder: folder.id)

        let conversations = try db.listConversations()
        #expect(conversations.first?.folderId == folder.id)
    }
}
