import Foundation

enum CameraServiceError: LocalizedError {
    case notConnected
    case assetNotFound
    case downloadFailed(String)
    case permissionDenied

    var errorDescription: String? {
        switch self {
        case .notConnected:
            "相机未连接"
        case .assetNotFound:
            "找不到相机内的文件"
        case .downloadFailed(let reason):
            "下载失败：\(reason)"
        case .permissionDenied:
            "未获得相机访问权限"
        }
    }
}

protocol CameraServiceProtocol: AnyObject {
    var brand: CameraBrand { get }
    var connectionState: CameraConnectionState { get }
    var onConnectionStateChange: ((CameraConnectionState) -> Void)? { get set }
    var onNewAsset: ((CameraAsset) -> Void)? { get set }

    func connect() async
    /// 页面离开：仅清空 UI 回调，不关闭相机会话。
    func detachFromUI()
    /// App 终止或物理断开：完整释放相机资源。
    func shutdown() async
    func listAssets() async throws -> [CameraAsset]
    func downloadAsset(_ asset: CameraAsset) async throws -> URL
}

extension CameraServiceProtocol {
    /// 兼容旧调用，语义等同 `shutdown()`。
    func disconnect() async {
        await shutdown()
    }
}
