//
//  Color+Extensions.swift
//  oAI
//
//  Color scheme matching Python TUI dark theme
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Rune Olsen
//
// This file is part of oAI.
//
// oAI is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// oAI is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
// Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with oAI. If not, see <https://www.gnu.org/licenses/>.


import SwiftUI

extension Color {
    // MARK: - oAI Color Palette (Matching Python TUI)
    
    static let oaiBackground = Color(hex: "#1e1e1e")       // Main background
    static let oaiSurface = Color(hex: "#2d2d2d")          // Cards, surfaces
    static let oaiPrimary = Color(hex: "#cccccc")          // Primary text
    static let oaiSecondary = Color(hex: "#888888")        // Secondary text
    static let oaiAccent = Color(hex: "#0a7aca")           // Blue accent (assistant)
    static let oaiSuccess = Color(hex: "#90ee90")          // Green (user messages)
    static let oaiError = Color(hex: "#ff6b6b")            // Red (errors)
    static let oaiWarning = Color(hex: "#ffaa00")          // Orange (warnings)
    static let oaiBorder = Color(hex: "#555555")           // Borders, dividers
    
    // MARK: - Message Role Colors
    
    static func messageColor(for role: MessageRole) -> Color {
        switch role {
        case .user: return .oaiSuccess
        case .assistant: return .oaiAccent
        case .system: return .oaiSecondary
        }
    }
    
    static func messageBackground(for role: MessageRole) -> Color {
        switch role {
        case .user: return .oaiSurface
        case .assistant: return .oaiBackground
        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
        )
    }
}
