//
//  Color+Extensions.swift
//  Confab
//
//  Color scheme matching Python TUI dark theme
//
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (C) 2026 Rune Olsen
//
// This file is part of Confab.
//
// Confab is licensed under the PolyForm Noncommercial License 1.0.0.
// You may use, study, modify, and share it for any noncommercial
// purpose. Commercial use — including selling Confab or any part of
// it, standalone or bundled into another product or service —
// requires a separate commercial license from the copyright holder.
//
// See the LICENSE file or
// <https://polyformproject.org/licenses/noncommercial/1.0.0> for
// the full license text. For commercial licensing, contact Rune
// Olsen via <https://confab.no>.


import SwiftUI

extension Color {
    // MARK: - oAI Color Palette (Matching Python TUI)
    
    static let confabBackground = Color(hex: "#1e1e1e")       // Main background
    static let confabSurface = Color(hex: "#2d2d2d")          // Cards, surfaces
    static let confabPrimary = Color(hex: "#cccccc")          // Primary text
    static let confabSecondary = Color(hex: "#888888")        // Secondary text
    static let confabAccent = Color(hex: "#0a7aca")           // Blue accent (assistant)
    static let confabSuccess = Color(hex: "#90ee90")          // Green (user messages)
    static let confabError = Color(hex: "#ff6b6b")            // Red (errors)
    static let confabWarning = Color(hex: "#ffaa00")          // Orange (warnings)
    static let confabBorder = Color(hex: "#555555")           // Borders, dividers
    
    // MARK: - Message Role Colors
    
    static func messageColor(for role: MessageRole) -> Color {
        switch role {
        case .user: return .confabSuccess
        case .assistant: return .confabAccent
        case .system: return .confabSecondary
        }
    }
    
    static func messageBackground(for role: MessageRole) -> Color {
        switch role {
        case .user: return .confabSurface
        case .assistant: return .confabBackground
        case .system: return Color(hex: "#2a2a2a")
        }
    }
    
    // MARK: - Provider Colors
    
    static func providerColor(_ provider: Settings.Provider) -> Color {
        switch provider {
        case .openrouter: return Color(hex: "#7c3aed")  // Purple
        case .anthropic: return Color(hex: "#d4895a")    // Orange
        case .openai: return Color(hex: "#10a37f")       // Green
        case .ollama: return Color(hex: "#ffffff")       // White
        case .appleOnDevice: return Color(hex: "#636366")  // Apple grey
        }
    }
    
    // MARK: - Hex Initializer
    
    init(hex: String) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        
        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue:  Double(b) / 255,
            opacity: Double(a) / 255
        )
    }
}
