import Foundation

public struct DetectionRetry: Codable, Sendable {
    public var timestamp = Date()
    public let crop: PixelRect
    public let twoD: PoseResult?
    public let threeD: PoseResult?
    public init(crop: PixelRect, twoD: PoseResult?, threeD: PoseResult?) {
        self.crop = crop; self.twoD = twoD; self.threeD = threeD
    }
}
public struct RetryRequest: Codable, Sendable {
    public let source: URL
    public let sourceHash: String
    public let crop: PixelRect
    public init(source: URL, sourceHash: String, crop: PixelRect) {
        self.source = source; self.sourceHash = sourceHash; self.crop = crop
    }
    public func run() throws -> DetectionRetry {
        let input = try ImageInput.load(url: source)
        guard input.sha256 == sourceHash else { throw PoseError.invalidInput("Source photo changed.") }
        let image = try input.cropped(to: crop)
        return DetectionRetry(crop: crop, twoD: VisionPoseEstimator.estimate2D(image: image, crop: crop),
                              threeD: VisionPoseEstimator.estimate3D(image: image, crop: crop))
    }
}
