import Foundation
import AirliftFFI

/// Drives the RPPairing host: requests Local Network, keeps the app alive while
/// the user approves the PIN in Settings, advertises the service over Bonjour,
/// and runs `al_pairing_run_host` off the main thread.
@MainActor
final class PairingController: NSObject, ObservableObject, NetServiceDelegate {

    static let shared = PairingController()

    private let hostName = AppIdentity.name
    private let hostModel = "Mac17,7"   // device sees a Mac-like pairing host
    private let bindAddress = "0.0.0.0"

    private var netService: NetService?
    private let localNetwork = LocalNetworkAuthorization()
    private let keepAlive = KeepAlive()

    @Published private(set) var running = false
    @Published var pairingStatus: String = "idle"
    @Published var pairingPIN: String? = nil

    /// Path to the pairing file that was actively found or created.
    static var customPairingFilePath: String? = nil

    /// Persisted altIRK keeps the host identity stable across pairings so a
    /// device that has already paired recognises this host.
    private static let altIRKKey = "airliftSilencePairingHostAltIRK"
    nonisolated private static var storedAltIRK: String {
        get { UserDefaults.standard.string(forKey: altIRKKey) ?? "" }
        set { UserDefaults.standard.set(newValue, forKey: altIRKKey) }
    }

    private var pairContinuation: CheckedContinuation<String, Error>?

    // MARK: - Public API

    enum PairingError: LocalizedError {
        case busy
        case localNetworkDenied
        case zeroBytes
        case failed(String)

        var errorDescription: String? {
            switch self {
            case .busy: return String(localized: "Pairing is already in progress.")
            case .localNetworkDenied: return String(localized: "Local Network permission is off. Enable it in Settings › \(AppIdentity.name) › Local Network.")
            case .zeroBytes: return String(localized: "Pairing produced an empty file. Approve the pairing request, then try again.")
            case let .failed(msg): return msg
            }
        }
    }

    /// The single pairing file this app reads and writes. It lives in Documents,
    /// which is visible in the Files app (file sharing is on), so it carries the
    /// app's own name rather than the upstream project's.
    static let pairingFileName = "airlift_pairing.plist"

    /// Ensures the given pairing file is copied to the canonical path.
    @discardableResult
    static func syncCanonicalPairingFile(from sourcePath: String) -> String {
        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let canonicalURL = dir.appendingPathComponent(pairingFileName)

        if let data = try? Data(contentsOf: URL(fileURLWithPath: sourcePath)), !data.isEmpty {
            if sourcePath != canonicalURL.path {
                try? data.write(to: canonicalURL, options: .atomic)
            }
            customPairingFilePath = canonicalURL.path
            return canonicalURL.path
        }
        return sourcePath
    }

    /// Deletes every credential copy created or adopted by this app and clears the stored AltIRK.
    static func deleteStoredPairingCredentials() {
        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let canonicalURL = dir.appendingPathComponent(pairingFileName)
        try? FileManager.default.removeItem(at: canonicalURL)
        if let custom = customPairingFilePath, custom != canonicalURL.path {
            try? FileManager.default.removeItem(atPath: custom)
            customPairingFilePath = nil
        }
        storedAltIRK = ""
    }

    /// Path where the pairing file is written or read from.
    static func pairingFilePath() -> String {
        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let canonicalPath = dir.appendingPathComponent(pairingFileName).path

        if let size = try? FileManager.default.attributesOfItem(atPath: canonicalPath)[.size] as? Int,
           size > 0 {
            return canonicalPath
        }

        if let custom = customPairingFilePath, FileManager.default.fileExists(atPath: custom) {
            let size = (try? FileManager.default.attributesOfItem(atPath: custom)[.size] as? Int) ?? 0
            if size > 0 {
                _ = syncCanonicalPairingFile(from: custom)
                return canonicalPath
            }
        }

        return canonicalPath
    }

    /// Start the host and resolve with the pairing-file path, or throw.
    func startAndWait() async throws -> String {
        // If already running, cancel previous to allow clean restart
        if running {
            softCancel()
            try? await Task.sleep(nanoseconds: 100_000_000)
        }
        return try await withCheckedThrowingContinuation { cont in
            pairContinuation = cont
            start()
        }
    }

    func softCancel() {
        stopAdvertising()
        keepAlive.stopAll()
        running = false
        pairingPIN = nil
        pairingStatus = String(localized: "Cancelled")
        PinNotifier.clear()
        resolve(.failure(CancellationError()))
    }

    private func resolve(_ result: Result<String, Error>) {
        guard let cont = pairContinuation else { return }
        pairContinuation = nil
        cont.resume(with: result)
    }

    func start() {
        stopAdvertising()
        keepAlive.stopAll()
        running = true
        pairingPIN = nil
        PinNotifier.clear()
        pairingStatus = String(localized: "Starting local host…")

        Task {
            _ = await localNetwork.request()
            guard running else { return }

            keepAlive.startAudio()
            pairingStatus = String(localized: "Broadcasting… open Settings to pair")
            runHost()
        }
    }

    // MARK: - Private

    private func runHost() {
        let bind = bindAddress
        let name = hostName
        let model = hostModel
        let outPath = Self.pairingFilePath()
        let altIRK = Self.storedAltIRK
        nonisolated(unsafe) let ctx = UnsafeMutableRawPointer(
            Unmanaged.passRetained(self).toOpaque()
        )

        DispatchQueue.global(qos: .userInitiated).async {
            var result = ALPairResult()
            let rc = bind.withCString { bindC in
                name.withCString { nameC in
                    model.withCString { modelC in
                        outPath.withCString { outC in
                            altIRK.withCString { irkC in
                                al_pairing_run_host(
                                    bindC, 0, nameC, modelC, outC, irkC,
                                    pairReadyCallback, pairPinCallback, ctx, &result)
                            }
                        }
                    }
                }
            }

            let outcome: Outcome
            if rc == 0 {
                let issued = cStr(result.host_alt_irk_hex)
                if !issued.isEmpty { Self.storedAltIRK = issued }
                let devName = cStr(result.device_name)
                let filePath = cStr(result.pairing_file_path)
                outcome = .success(
                    name: devName.isEmpty ? "iPhone" : devName,
                    path: filePath.isEmpty ? outPath : filePath
                )
            } else {
                let msg = cStr(result.error)
                outcome = .failure(msg.isEmpty ? String(localized: "pairing failed (rc=\(String(describing: rc)))") : msg)
            }
            al_pairing_result_free(&result)

            DispatchQueue.main.async {
                Unmanaged<PairingController>.fromOpaque(ctx).release()
                self.finish(outcome)
            }
        }
    }

    private enum Outcome {
        case success(name: String, path: String)
        case failure(String)
    }

    private func finish(_ outcome: Outcome) {
        stopAdvertising()
        // Keep background alive for 5s so iOS doesn't kill the app before user returns from Settings
        DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { [weak self] in
            self?.keepAlive.stopAll()
        }
        running = false
        pairingPIN = nil
        PinNotifier.clear()

        switch outcome {
        case let .success(name, path):
            let canonical = Self.syncCanonicalPairingFile(from: path)
            let size = (try? FileManager.default.attributesOfItem(atPath: canonical)[.size] as? Int) ?? 0
            if size == 0 {
                pairingStatus = String(localized: "Failed: empty pairing file")
                resolve(.failure(PairingError.zeroBytes))
            } else {
                pairingStatus = String(localized: "Paired: \(name) (\(size.formatted()) B)")
                resolve(.success(canonical))
            }
        case let .failure(message):
            pairingStatus = String(localized: "Failed: \(message)")
            resolve(.failure(PairingError.failed(message)))
        }
    }


    // MARK: Bonjour advertising

    fileprivate func startAdvertising(serviceID: String, port: Int32, txt: [String: Data]) {
        stopAdvertising()
        let service = NetService(
            domain: "",
            type: "_remotepairing-pairable-host._tcp.",
            name: serviceID,
            port: port
        )
        service.delegate = self
        service.setTXTRecord(NetService.data(fromTXTRecord: txt))
        // The status is set by the delegate methods below rather than here:
        // publishing fails silently when the Local Network permission is off,
        // and then Settings never offers the "Pair with App" entry. Saying
        // "Advertising" in that case sends the user hunting for a setting that
        // will never show up.
        pairingStatus = String(localized: "Waiting for the system to publish the pairing service…")
        service.publish()
        netService = service
    }

    // MARK: - NetServiceDelegate

    nonisolated func netServiceDidPublish(_ sender: NetService) {
        DispatchQueue.main.async { [weak self] in
            self?.pairingStatus = String(localized: "Advertising — open Settings › Privacy & Security › Developer Mode")
        }
    }

    nonisolated func netService(_ sender: NetService, didNotPublish errorDict: [String: NSNumber]) {
        let code = errorDict[NetService.errorCode]?.intValue ?? 0
        DispatchQueue.main.async { [weak self] in
            self?.pairingStatus = String(localized: "Broadcast failed (code \(String(describing: code))). Check Settings › \(AppIdentity.name) › Local Network, then try again.")
        }
    }

    fileprivate func presentPin(_ pin: String) {
        pairingPIN = pin
        pairingStatus = String(localized: "Enter PIN \(pin) in Settings › Privacy & Security › Developer Mode › Pair with \(AppIdentity.name)")
        // The user is inside Settings at this point, so surface the code there too.
        PinNotifier.post(pin: pin)
    }

    private func stopAdvertising() {
        netService?.stop()
        netService = nil
    }
}

// MARK: - C callbacks

private let pairReadyCallback: ALPairReadyCb = { ctx, serviceID, port, keys, vals, count in
    guard let ctx = ctx, let serviceID = serviceID else { return }
    let controller = Unmanaged<PairingController>.fromOpaque(ctx).takeUnretainedValue()
    let id = String(cString: serviceID)

    var txt: [String: Data] = [:]
    if let keys = keys, let vals = vals {
        for i in 0..<Int(count) {
            guard let k = keys[i], let v = vals[i] else { continue }
            txt[String(cString: k)] = Data(String(cString: v).utf8)
        }
    }
    DispatchQueue.main.async {
        controller.startAdvertising(serviceID: id, port: Int32(port), txt: txt)
    }
}

private let pairPinCallback: ALPairPinCb = { pin, ctx in
    guard let ctx = ctx, let pin = pin else { return }
    let controller = Unmanaged<PairingController>.fromOpaque(ctx).takeUnretainedValue()
    let pinString = String(cString: pin)
    DispatchQueue.main.async {
        controller.presentPin(pinString)
    }
}

private func cStr(_ ptr: UnsafeMutablePointer<CChar>?) -> String {
    guard let ptr = ptr else { return "" }
    return String(cString: ptr)
}

