//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Copyright (c) 2026 everettjf. All rights reserved.
//

import SwiftUI

struct OpenRockyLicensesView: View {
    var body: some View {
        List(Self.entries) { entry in
            NavigationLink {
                OpenRockyLicenseDetailView(entry: entry)
            } label: {
                VStack(alignment: .leading, spacing: 2) {
                    Text(entry.name)
                        .font(.system(size: 15, weight: .medium, design: .rounded))
                        .foregroundStyle(.primary)
                    Text("\(entry.license) · \(entry.author)")
                        .font(.system(size: 12, weight: .medium))
                        .foregroundStyle(.secondary)
                }
                .padding(.vertical, 2)
            }
        }
        .navigationTitle("Licenses")
        .navigationBarTitleDisplayMode(.inline)
    }

    static let entries: [OpenRockyLicenseEntry] = [
        .init(
            name: "LanguageModelChatUI",
            author: "Lakr233",
            license: "MIT",
            url: "https://github.com/Lakr233/LanguageModelChatUI",
            text: OpenRockyLicenseTexts.mit(copyright: "Copyright (c) 2026 @Lakr233")
        ),
        .init(
            name: "MarkdownView",
            author: "Lakr233",
            license: "MIT",
            url: "https://github.com/Lakr233/MarkdownView",
            text: OpenRockyLicenseTexts.mit(copyright: "Copyright (c) 2025 @Lakr233")
        ),
        .init(
            name: "SwiftOpenAI",
            author: "James Rochabrun",
            license: "MIT",
            url: "https://github.com/jamesrochabrun/SwiftOpenAI",
            text: OpenRockyLicenseTexts.mit(copyright: "Copyright (c) 2023 James Rochabrun")
        ),
        .init(
            name: "ios_system",
            author: "Nicolas Holzschuch",
            license: "BSD-3-Clause",
            url: "https://github.com/holzschu/ios_system",
            text: OpenRockyLicenseTexts.bsd3(copyright: "Copyright (c) 2017-2024 Nicolas Holzschuch")
        ),
        .init(
            name: "Python-Apple-support",
            author: "BeeWare",
            license: "BSD-3-Clause",
            url: "https://github.com/beeware/Python-Apple-support",
            text: OpenRockyLicenseTexts.bsd3(copyright: "Copyright (c) 2020-2024 Russell Keith-Magee and the BeeWare contributors")
        ),
        .init(
            name: "FFmpeg-iOS",
            author: "FFmpeg project",
            license: "LGPL-2.1",
            url: "https://github.com/FFmpeg/FFmpeg",
            text: OpenRockyLicenseTexts.lgpl21Notice
        ),
    ]
}

struct OpenRockyLicenseEntry: Identifiable {
    let id = UUID()
    let name: String
    let author: String
    let license: String
    let url: String
    let text: String
}

struct OpenRockyLicenseDetailView: View {
    let entry: OpenRockyLicenseEntry

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 12) {
                VStack(alignment: .leading, spacing: 4) {
                    Text(entry.name)
                        .font(.system(size: 20, weight: .bold, design: .rounded))
                    Text("\(entry.license) · \(entry.author)")
                        .font(.system(size: 13, weight: .medium))
                        .foregroundStyle(.secondary)
                    if let url = URL(string: entry.url) {
                        Link(entry.url, destination: url)
                            .font(.system(size: 12, design: .monospaced))
                    }
                }
                Divider()
                Text(entry.text)
                    .font(.system(size: 12, design: .monospaced))
                    .textSelection(.enabled)
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            .padding(16)
        }
        .navigationTitle(entry.name)
        .navigationBarTitleDisplayMode(.inline)
    }
}

enum OpenRockyLicenseTexts {
    static func mit(copyright: String) -> String {
        """
        MIT License

        \(copyright)

        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        """
    }

    static func bsd3(copyright: String) -> String {
        """
        BSD 3-Clause License

        \(copyright)
        All rights reserved.

        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:

        1. Redistributions of source code must retain the above copyright notice,
           this list of conditions and the following disclaimer.

        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.

        3. Neither the name of the copyright holder nor the names of its contributors
           may be used to endorse or promote products derived from this software
           without specific prior written permission.

        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
        LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        POSSIBILITY OF SUCH DAMAGE.
        """
    }

    static let lgpl21Notice = """
    This application uses FFmpeg libraries under the GNU Lesser General Public
    License, version 2.1 or later.

    FFmpeg is a collection of free software that can record, convert, and stream
    digital audio and video in numerous formats. FFmpeg is licensed under the
    GNU Lesser General Public License (LGPL) version 2.1 or later. However,
    FFmpeg incorporates several optional parts and optimizations that are covered
    by the GNU General Public License (GPL) version 2 or later. If those parts
    get used, the GPL applies to all of FFmpeg.

    Source code: https://github.com/FFmpeg/FFmpeg
    License: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html

    To request the corresponding source code of the FFmpeg libraries used in this
    application, please file an issue at:
    https://github.com/openrocky/openrocky/issues
    """
}
