import Foundation enum CommentDraftValidationError: LocalizedError { case emptySubreddit case invalidSubreddit case emptyPostTitle case emptyParentComment case emptyComment case commentTooLong case invalidPostURL var errorDescription: String? { switch self { case .emptySubreddit: "Enter a subreddit to tailor the comment." case .invalidSubreddit: "Subreddit names must be 3–21 characters and use only letters, numbers, or underscores." case .emptyPostTitle: "Enter the post title so the AI understands the thread context." case .emptyParentComment: "Paste the comment you're replying to." case .emptyComment: "Write or generate a comment before copying." case .commentTooLong: "Comments must be 10,000 characters or fewer." case .invalidPostURL: "Enter a valid Reddit post URL (https://reddit.com/…)." } } } enum CommentDraftValidator { static let maxCommentLength = 10_000 static func validateForGeneration(from draft: CommentDraft) throws { guard !draft.subreddit.trimmingCharacters(in: .whitespaces).isEmpty else { throw CommentDraftValidationError.emptySubreddit } guard PostDraftValidator.isValidSubreddit(draft.subreddit) else { throw CommentDraftValidationError.invalidSubreddit } guard !draft.postTitle.trimmingCharacters(in: .whitespaces).isEmpty else { throw CommentDraftValidationError.emptyPostTitle } if draft.commentType == .reply { guard !draft.parentComment.trimmingCharacters(in: .whitespaces).isEmpty else { throw CommentDraftValidationError.emptyParentComment } } } static func validateForExport(_ draft: CommentDraft) throws { try validateForGeneration(from: draft) let body = draft.body.trimmingCharacters(in: .whitespaces) guard !body.isEmpty else { throw CommentDraftValidationError.emptyComment } guard body.count <= maxCommentLength else { throw CommentDraftValidationError.commentTooLong } let url = draft.postURL.trimmingCharacters(in: .whitespaces) if !url.isEmpty, !isValidRedditURL(url) { throw CommentDraftValidationError.invalidPostURL } } static func canGenerate(from draft: CommentDraft, isGenerating: Bool) -> Bool { guard !isGenerating else { return false } return (try? validateForGeneration(from: draft)) != nil } static func canExport(_ draft: CommentDraft) -> Bool { (try? validateForExport(draft)) != nil } static func isValidRedditURL(_ string: String) -> Bool { let trimmed = string.trimmingCharacters(in: .whitespaces) guard let url = URL(string: trimmed), let scheme = url.scheme?.lowercased(), scheme == "http" || scheme == "https", let host = url.host?.lowercased(), host.contains("reddit.com") else { return false } return true } }