//
//  AgentSkillFilesService.swift
//  oAI
//
//  Manages per-skill file directories in Application Support/oAI/skills/<uuid>/
//
// 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 Foundation
import UniformTypeIdentifiers

final class AgentSkillFilesService {
    static let shared = AgentSkillFilesService()

    private let baseDirectory: URL = {
        let appSupport = FileManager.default.urls(for: .applicationSupportDirectory,
                                                   in: .userDomainMask).first!
        return appSupport.appendingPathComponent("oAI/skills", isDirectory: true)
    }()

    func skillDirectory(for id: UUID) -> URL {
        baseDirectory.appendingPathComponent(id.uuidString, isDirectory: true)
    }

    func ensureDirectory(for id: UUID) {
        try? FileManager.default.createDirectory(
            at: skillDirectory(for: id), withIntermediateDirectories: true)
    }

    func listFiles(for id: UUID) -> [URL] {
        guard let contents = try? FileManager.default.contentsOfDirectory(
            at: skillDirectory(for: id),
            includingPropertiesForKeys: [.fileSizeKey],
            options: .skipsHiddenFiles)
        else { return [] }
        return contents.sorted { $0.lastPathComponent < $1.lastPathComponent }
    }

    func addFile(from sourceURL: URL, to id: UUID) throws {
        ensureDirectory(for: id)
        let dest = skillDirectory(for: id).appendingPathComponent(sourceURL.lastPathComponent)
        if FileManager.default.fileExists(atPath: dest.path) {
            try FileManager.default.removeItem(at: dest)
        }
        try FileManager.default.copyItem(at: sourceURL, to: dest)
    }

    func deleteFile(at url: URL) {
        try? FileManager.default.removeItem(at: url)
    }

    func deleteAll(for id: UUID) {
        try? FileManager.default.removeItem(at: skillDirectory(for: id))
    }

    func hasFiles(for id: UUID) -> Bool {
        !listFiles(for: id).isEmpty
    }

    /// Returns (filename, content) for all readable text files
    func readTextFiles(for id: UUID) -> [(name: String, content: String)] {
        listFiles(for: id).compactMap { url in
            guard let content = try? String(contentsOf: url, encoding: .utf8) else { return nil }
            return (url.lastPathComponent, content)
        }
    }

    /// Returns file size in bytes, or nil if unavailable
    func fileSize(at url: URL) -> Int? {
        (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize
    }
}
