import Testing
@testable import IMessageSync

@Suite("Send Validation")
struct SendValidationTests {

    @Test func invalidPhoneTooFewDigitsReturnsError() {
        let result = SendService.validate(recipient: "12345")
        #expect(result.error == "Invalid phone number format")
    }

    @Test func validTenDigitPhoneAccepted() {
        let result = SendService.validate(recipient: "5551234567")
        #expect(result.error == nil)
        #expect(result.buddyId == "5551234567")
        #expect(result.isEmail == false)
    }

    @Test func phoneWithFormattingCleanedToDigits() {
        let result = SendService.validate(recipient: "(555) 123-4567")
        #expect(result.error == nil)
        #expect(result.buddyId == "5551234567")
    }

    @Test func validEmailAccepted() {
        let result = SendService.validate(recipient: "test@example.com")
        #expect(result.error == nil)
        #expect(result.buddyId == "test@example.com")
        #expect(result.isEmail == true)
    }

    @Test func invalidEmailNoDotAfterAtReturnsError() {
        let result = SendService.validate(recipient: "test@localhost")
        #expect(result.error == "Invalid email address format")
    }

    @Test func messageIdFormatContainsSendPrefix() {
        let messageId = SendService.generateMessageId(buddyId: "+15551234567", isEmail: false)
        #expect(messageId.hasPrefix("send_"))
    }

    @Test func emailMessageIdUsesLastFourOfLocalPart() {
        let messageId = SendService.generateMessageId(buddyId: "alice@example.com", isEmail: true)
        #expect(messageId.hasPrefix("send_lice_"))
    }

    @Test func phoneMessageIdUsesLastFourDigits() {
        let messageId = SendService.generateMessageId(buddyId: "+15551234567", isEmail: false)
        #expect(messageId.hasPrefix("send_4567_"))
    }

    @Test func phoneWithPlusPrefixAccepted() {
        let result = SendService.validate(recipient: "+15551234567")
        #expect(result.error == nil)
        #expect(result.buddyId == "+15551234567")
    }

    @Test func emptyRecipientPhoneReturnsError() {
        let result = SendService.validate(recipient: "")
        #expect(result.error != nil)
    }
}
