// Security: Directus permissions filter by user_created=$CURRENT_USER server-side.
// Client-side participant_hash filters remain for hash-based identity matching.
import { client } from './client.js'

export async function getConversations(participantHash) {
    const response = await client.get('/items/conversations', {
        fields: [
            '*',
            'listing_id.id',
            'listing_id.title',
            'listing_id.status',
            'listing_id.images.directus_files_id.id'
        ],
        filter: {
            _or: [
                { participant_hash_1: { _eq: participantHash } },
                { participant_hash_2: { _eq: participantHash } }
            ]
        },
        sort: ['-date_updated']
    })
    return response.data
}

export async function getConversation(id) {
    const response = await client.get(`/items/conversations/${id}`, {
        fields: [
            '*',
            'listing_id.*',
            'listing_id.images.directus_files_id.*'
        ]
    })
    return response.data
}

// Messages access restricted server-side to conversations owned by $CURRENT_USER
export async function getConversationMessages(conversationId) {
    const response = await client.get('/items/messages', {
        fields: ['*'],
        filter: { conversation: { _eq: conversationId } },
        sort: ['date_created']
    })
    return response.data
}

export async function sendMessage(conversationId, senderHash, encryptedContent, nonce, type = 'text') {
    const response = await client.post('/items/messages', {
        conversation: conversationId,
        sender_hash: senderHash,
        content_encrypted: encryptedContent,
        nonce: nonce,
        type: type
    })
    return response.data
}

// Directus sets user_created automatically for authenticated requests
export async function startConversation(listingId, participantHash1, participantHash2, publicKey1, publicKey2) {
    const response = await client.post('/items/conversations', {
        listing_id: listingId,
        participant_hash_1: participantHash1,
        participant_hash_2: participantHash2,
        public_key_1: publicKey1,
        public_key_2: publicKey2,
        status: 'active'
    })
    return response.data
}

export async function findConversation(listingId, participantHash1, participantHash2) {
    const response = await client.get('/items/conversations', {
        filter: {
            listing_id: { _eq: listingId },
            _or: [
                {
                    _and: [
                        { participant_hash_1: { _eq: participantHash1 } },
                        { participant_hash_2: { _eq: participantHash2 } }
                    ]
                },
                {
                    _and: [
                        { participant_hash_1: { _eq: participantHash2 } },
                        { participant_hash_2: { _eq: participantHash1 } }
                    ]
                }
            ]
        },
        limit: 1
    })
    return response.data?.[0] || null
}

export async function updateConversationStatus(id, status) {
    const response = await client.patch(`/items/conversations/${id}`, { status })
    return response.data
}
