// iOS File Provider extension scaffold for UltiDrive (drive-mount).
//
// Surfaces the Ulti drive in the system Files app. Add as a "File Provider
// Extension" target in the UltiDrive gen/apple project after `pnpm tauri ios
// init`. Requires the App Group `group.space.ulti.suite` and reads the OIDC
// token from the shared Keychain access group (service "space.ulti.suite").
//
// This is a minimal NSFileProviderReplicatedExtension skeleton — wire the item
// enumeration + content fetch to /api/v1/drive. Targets iOS 16+.

import FileProvider

final class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {

    required init(domain: NSFileProviderDomain) {
        super.init()
        // TODO: load shared session (Keychain access group) for auth.
    }

    func invalidate() {}

    func item(
        for identifier: NSFileProviderItemIdentifier,
        request: NSFileProviderRequest,
        completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void
    ) -> Progress {
        // TODO GET /api/v1/drive/items/{identifier}; map to NSFileProviderItem.
        completionHandler(nil, NSError(domain: NSFileProviderErrorDomain,
                                       code: NSFileProviderError.noSuchItem.rawValue))
        return Progress()
    }

    func fetchContents(
        for itemIdentifier: NSFileProviderItemIdentifier,
        version requestedVersion: NSFileProviderItemVersion?,
        request: NSFileProviderRequest,
        completionHandler: @escaping (URL?, NSFileProviderItem?, Error?) -> Void
    ) -> Progress {
        // TODO download /api/v1/drive/items/{id}/content to a temp URL.
        completionHandler(nil, nil, NSError(domain: NSFileProviderErrorDomain,
                                            code: NSFileProviderError.serverUnreachable.rawValue))
        return Progress()
    }

    func createItem(
        basedOn itemTemplate: NSFileProviderItem,
        fields: NSFileProviderItemFields,
        contents url: URL?,
        options: NSFileProviderCreateItemOptions = [],
        request: NSFileProviderRequest,
        completionHandler: @escaping (NSFileProviderItem?, NSFileProviderItemFields, Bool, Error?) -> Void
    ) -> Progress {
        // TODO POST /api/v1/drive/items (upload).
        completionHandler(nil, [], false, NSError(domain: NSFileProviderErrorDomain,
                                                  code: NSFileProviderError.serverUnreachable.rawValue))
        return Progress()
    }

    func modifyItem(
        _ item: NSFileProviderItem,
        baseVersion version: NSFileProviderItemVersion,
        changedFields: NSFileProviderItemFields,
        contents newContents: URL?,
        options: NSFileProviderModifyItemOptions = [],
        request: NSFileProviderRequest,
        completionHandler: @escaping (NSFileProviderItem?, NSFileProviderItemFields, Bool, Error?) -> Void
    ) -> Progress {
        completionHandler(nil, [], false, NSError(domain: NSFileProviderErrorDomain,
                                                  code: NSFileProviderError.serverUnreachable.rawValue))
        return Progress()
    }

    func deleteItem(
        identifier: NSFileProviderItemIdentifier,
        baseVersion version: NSFileProviderItemVersion,
        options: NSFileProviderDeleteItemOptions = [],
        request: NSFileProviderRequest,
        completionHandler: @escaping (Error?) -> Void
    ) -> Progress {
        // TODO DELETE /api/v1/drive/items/{identifier}.
        completionHandler(nil)
        return Progress()
    }

    func enumerator(
        for containerItemIdentifier: NSFileProviderItemIdentifier,
        request: NSFileProviderRequest
    ) throws -> NSFileProviderEnumerator {
        throw NSError(domain: NSFileProviderErrorDomain,
                      code: NSFileProviderError.noSuchItem.rawValue)
    }
}
