import Foundation

enum ContactsServiceError: LocalizedError {
    case unexpectedStatus(String)
    case decoding(debugDescription: String)
    case encoding(String)

    var errorDescription: String? {
        switch self {
        case .unexpectedStatus(let message):
            return message
        case .decoding(let debugDescription):
            return AppConfig.DEBUG
                ? debugDescription
                : NSLocalizedString("Не удалось загрузить контакты.", comment: "Contacts service decoding error")
        case .encoding(let message):
            return message
        }
    }
}

struct ContactPayload: Decodable {
    let userId: UUID
    let login: String?
    let fullName: String?
    let customName: String?
    let friendCode: Bool
    let createdAt: Date
}

struct ContactsListPayload: Decodable {
    let items: [ContactPayload]
    let hasMore: Bool
}

private struct ContactCreateRequestPayload: Encodable {
    let userId: UUID?
    let login: String?
    let friendCode: String?
    let customName: String?
}

private struct ContactDeleteRequestPayload: Encodable {
    let userId: UUID
}

private struct ContactUpdateRequestPayload: Encodable {
    let userId: UUID
    let customName: String?
}

final class ContactsService {
    private let client: NetworkClient
    private let decoder: JSONDecoder
    private let encoder: JSONEncoder

    init(client: NetworkClient = .shared) {
        self.client = client
        self.decoder = JSONDecoder()
        self.decoder.keyDecodingStrategy = .convertFromSnakeCase
        self.decoder.dateDecodingStrategy = .custom(Self.decodeDate)

        self.encoder = JSONEncoder()
        self.encoder.keyEncodingStrategy = .convertToSnakeCase
    }

    func fetchContacts(limit: Int, offset: Int, completion: @escaping (Result<ContactsListPayload, Error>) -> Void) {
        client.request(
            path: "/v1/user/contact/list",
            method: .get,
            query: [
                "limit": String(limit),
                "offset": String(offset)
            ],
            requiresAuth: true
        ) { [decoder] result in
            switch result {
            case .success(let response):
                do {
                    let apiResponse = try decoder.decode(APIResponse<ContactsListPayload>.self, from: response.data)
                    guard apiResponse.status == "fine" else {
                        let message = apiResponse.detail ?? NSLocalizedString("Не удалось загрузить контакты.", comment: "Contacts service unexpected status")
                        completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                        return
                    }
                    completion(.success(apiResponse.data))
                } catch {
                    let debugMessage = Self.describeDecodingError(error: error, data: response.data)
                    if AppConfig.DEBUG {
                        print("[ContactsService] decode contacts failed: \(debugMessage)")
                    }
                    completion(.failure(ContactsServiceError.decoding(debugDescription: debugMessage)))
                }
            case .failure(let error):
                if case let NetworkError.server(_, data) = error,
                   let data,
                   let message = Self.errorMessage(from: data) {
                    completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                    return
                }
                completion(.failure(error))
            }
        }
    }

    func fetchContacts(limit: Int, offset: Int) async throws -> ContactsListPayload {
        try await withCheckedThrowingContinuation { continuation in
            fetchContacts(limit: limit, offset: offset) { result in
                continuation.resume(with: result)
            }
        }
    }

    func addContact(userId: UUID, customName: String?, completion: @escaping (Result<ContactPayload, Error>) -> Void) {
        let request = ContactCreateRequestPayload(
            userId: userId,
            login: nil,
            friendCode: nil,
            customName: customName
        )

        guard let body = try? encoder.encode(request) else {
            let message = NSLocalizedString("Не удалось подготовить данные запроса.", comment: "Contacts service encoding error")
            completion(.failure(ContactsServiceError.encoding(message)))
            return
        }

        client.request(
            path: "/v1/user/contact/add",
            method: .post,
            body: body,
            requiresAuth: true
        ) { [decoder] result in
            switch result {
            case .success(let response):
                do {
                    let apiResponse = try decoder.decode(APIResponse<ContactPayload>.self, from: response.data)
                    guard apiResponse.status == "fine" else {
                        let message = apiResponse.detail ?? NSLocalizedString("Не удалось добавить контакт.", comment: "Contacts service add unexpected status")
                        completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                        return
                    }
                    completion(.success(apiResponse.data))
                } catch {
                    let debugMessage = Self.describeDecodingError(error: error, data: response.data)
                    if AppConfig.DEBUG {
                        print("[ContactsService] decode contact add failed: \(debugMessage)")
                    }
                    completion(.failure(ContactsServiceError.decoding(debugDescription: debugMessage)))
                }
            case .failure(let error):
                if case let NetworkError.server(_, data) = error,
                   let data,
                   let message = Self.errorMessage(from: data) {
                    completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                    return
                }
                completion(.failure(error))
            }
        }
    }

    func addContact(userId: UUID, customName: String?) async throws -> ContactPayload {
        try await withCheckedThrowingContinuation { continuation in
            addContact(userId: userId, customName: customName) { result in
                continuation.resume(with: result)
            }
        }
    }

    func removeContact(userId: UUID, completion: @escaping (Result<Void, Error>) -> Void) {
        let request = ContactDeleteRequestPayload(userId: userId)

        guard let body = try? encoder.encode(request) else {
            let message = NSLocalizedString("Не удалось подготовить данные запроса.", comment: "Contacts service encoding error")
            completion(.failure(ContactsServiceError.encoding(message)))
            return
        }

        client.request(
            path: "/v1/user/contact/remove",
            method: .delete,
            body: body,
            requiresAuth: true
        ) { [decoder] result in
            switch result {
            case .success(let response):
                do {
                    let apiResponse = try decoder.decode(APIResponse<MessagePayload>.self, from: response.data)
                    guard apiResponse.status == "fine" else {
                        let message = apiResponse.detail ?? NSLocalizedString("Не удалось удалить контакт.", comment: "Contacts service delete unexpected status")
                        completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                        return
                    }
                    completion(.success(()))
                } catch {
                    let debugMessage = Self.describeDecodingError(error: error, data: response.data)
                    if AppConfig.DEBUG {
                        print("[ContactsService] decode contact delete failed: \(debugMessage)")
                    }
                    completion(.failure(ContactsServiceError.decoding(debugDescription: debugMessage)))
                }
            case .failure(let error):
                if case let NetworkError.server(_, data) = error,
                   let data,
                   let message = Self.errorMessage(from: data) {
                    completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                    return
                }
                completion(.failure(error))
            }
        }
    }

    func removeContact(userId: UUID) async throws {
        try await withCheckedThrowingContinuation { continuation in
            removeContact(userId: userId) { result in
                continuation.resume(with: result)
            }
        }
    }

    func updateContact(userId: UUID, customName: String?, completion: @escaping (Result<Void, Error>) -> Void) {
        let request = ContactUpdateRequestPayload(userId: userId, customName: customName)

        guard let body = try? encoder.encode(request) else {
            let message = NSLocalizedString("Не удалось подготовить данные запроса.", comment: "Contacts service encoding error")
            completion(.failure(ContactsServiceError.encoding(message)))
            return
        }

        client.request(
            path: "/v1/user/contact/update",
            method: .patch,
            body: body,
            requiresAuth: true
        ) { [decoder] result in
            switch result {
            case .success(let response):
                do {
                    let apiResponse = try decoder.decode(APIResponse<MessagePayload>.self, from: response.data)
                    guard apiResponse.status == "fine" else {
                        let message = apiResponse.detail ?? NSLocalizedString("Не удалось обновить контакт.", comment: "Contacts service update unexpected status")
                        completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                        return
                    }
                    completion(.success(()))
                } catch {
                    let debugMessage = Self.describeDecodingError(error: error, data: response.data)
                    if AppConfig.DEBUG {
                        print("[ContactsService] decode contact update failed: \(debugMessage)")
                    }
                    completion(.failure(ContactsServiceError.decoding(debugDescription: debugMessage)))
                }
            case .failure(let error):
                if case let NetworkError.server(_, data) = error,
                   let data,
                   let message = Self.errorMessage(from: data) {
                    completion(.failure(ContactsServiceError.unexpectedStatus(message)))
                    return
                }
                completion(.failure(error))
            }
        }
    }

    func updateContact(userId: UUID, customName: String?) async throws {
        try await withCheckedThrowingContinuation { continuation in
            updateContact(userId: userId, customName: customName) { result in
                continuation.resume(with: result)
            }
        }
    }

    private static func decodeDate(from decoder: Decoder) throws -> Date {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)
        if let date = iso8601WithFractionalSeconds.date(from: string) {
            return date
        }
        if let date = iso8601Simple.date(from: string) {
            return date
        }
        throw DecodingError.dataCorruptedError(
            in: container,
            debugDescription: "Невозможно декодировать дату: \(string)"
        )
    }

    private static func describeDecodingError(error: Error, data: Data) -> String {
        var parts: [String] = []

        if let decodingError = error as? DecodingError {
            parts.append(decodingDescription(from: decodingError))
        } else {
            parts.append(error.localizedDescription)
        }

        if let payload = truncatedPayload(from: data) {
            parts.append("payload=\(payload)")
        }

        return parts.joined(separator: "\n")
    }

    private static func decodingDescription(from error: DecodingError) -> String {
        switch error {
        case .typeMismatch(let type, let context):
            return "Type mismatch for \(type) at \(codingPath(from: context)): \(context.debugDescription)"
        case .valueNotFound(let type, let context):
            return "Value not found for \(type) at \(codingPath(from: context)): \(context.debugDescription)"
        case .keyNotFound(let key, let context):
            return "Missing key '\(key.stringValue)' at \(codingPath(from: context)): \(context.debugDescription)"
        case .dataCorrupted(let context):
            return "Corrupted data at \(codingPath(from: context)): \(context.debugDescription)"
        @unknown default:
            return error.localizedDescription
        }
    }

    private static func codingPath(from context: DecodingError.Context) -> String {
        let path = context.codingPath.map { $0.stringValue }.filter { !$0.isEmpty }
        return path.isEmpty ? "root" : path.joined(separator: ".")
    }

    private static func truncatedPayload(from data: Data, limit: Int = 512) -> String? {
        guard let string = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines),
              !string.isEmpty else {
            return nil
        }

        if string.count <= limit {
            return string
        }

        let index = string.index(string.startIndex, offsetBy: limit)
        return String(string[string.startIndex..<index]) + "…"
    }

    private static func errorMessage(from data: Data) -> String? {
        if let apiError = try? JSONDecoder().decode(ErrorResponse.self, from: data) {
            if let detail = apiError.detail, !detail.isEmpty {
                return detail
            }
            if let message = apiError.data?.message, !message.isEmpty {
                return message
            }
        }
        if let string = String(data: data, encoding: .utf8), !string.isEmpty {
            return string
        }
        return nil
    }

    private static let iso8601WithFractionalSeconds: ISO8601DateFormatter = {
        let formatter = ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
        return formatter
    }()

    private static let iso8601Simple: ISO8601DateFormatter = {
        let formatter = ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime]
        return formatter
    }()
}
