package com.crm.chat.data.api

import com.crm.chat.data.model.*
import retrofit2.Response
import retrofit2.http.*

interface ChatApiService {

    // Chat Providers
    @GET("api/chat/providers")
    suspend fun getChatProviders(@Query("offset") offset: Int = 0): Response<List<ChatProvider>>

    // Chats
    @GET("api/chat/chats")
    suspend fun getChats(
        @Query("limit") limit: Int = 50,
        @Query("offset") offset: Int = 0
    ): Response<List<Chat>>

    @GET("api/chat/chats/{chatId}")
    suspend fun getChat(@Path("chatId") chatId: Long): Response<Chat>

    @DELETE("api/chat/chats/{chatId}")
    suspend fun deleteChat(@Path("chatId") chatId: Long): Response<Unit>

    // Create chats
    @POST("api/chat/chats/personal")
    suspend fun createPersonalChat(@Body request: CreatePersonalChatRequest): Response<Chat>

    @POST("api/chat/chats/group")
    suspend fun createGroupChat(@Body request: CreateGroupChatRequest): Response<Chat>

    @POST("api/chat/chats/external")
    suspend fun createExternalChat(@Body request: CreateExternalChatRequest): Response<Chat>

    // Chat search
    @GET("api/chat/chats/find")
    suspend fun findChats(@QueryMap filters: Map<String, String>): Response<List<Chat>>

    @GET("api/chat/chats/find/full")
    suspend fun findChatsFull(@QueryMap filters: Map<String, String>): Response<List<Chat>>

    @GET("api/chat/chats/find/full/personal")
    suspend fun findChatsFullPersonal(@QueryMap filters: Map<String, String>): Response<List<Chat>>

    @GET("api/chat/chats/find/full/by-message-content")
    suspend fun findChatsByMessageContent(@QueryMap filters: Map<String, String>): Response<List<Chat>>

    // Chat management
    @PATCH("api/chat/chats/group/{chatId}")
    suspend fun updateGroupChat(
        @Path("chatId") chatId: Long,
        @Body request: UpdateGroupChatRequest
    ): Response<Chat>

    @PUT("api/chat/chats/{chatId}/pin/{messageId}")
    suspend fun pinMessage(@Path("chatId") chatId: Long, @Path("messageId") messageId: Long): Response<Chat>

    @PUT("api/chat/chats/{chatId}/unpin/{messageId}")
    suspend fun unpinMessage(@Path("chatId") chatId: Long, @Path("messageId") messageId: Long): Response<Chat>

    @POST("api/chat/chats/{chatId}/contact")
    suspend fun createContactLead(
        @Path("chatId") chatId: Long,
        @Body request: CreateContactLeadRequest
    ): Response<EntityInfo>

    // Chat messages
    @GET("api/chat/chats/{chatId}/messages")
    suspend fun getChatMessages(
        @Path("chatId") chatId: Long,
        @Query("limit") limit: Int = 50,
        @Query("offset") offset: Int = 0
    ): Response<ChatMessagesResponse>

    @POST("api/chat/chats/{chatId}/messages")
    suspend fun sendMessage(
        @Path("chatId") chatId: Long,
        @Body request: SendMessageRequest
    ): Response<ChatMessage>

    @PUT("api/chat/chats/{chatId}/messages/{messageId}")
    suspend fun updateMessage(
        @Path("chatId") chatId: Long,
        @Path("messageId") messageId: Long,
        @Body request: UpdateMessageRequest
    ): Response<ChatMessage>

    @DELETE("api/chat/chats/{chatId}/messages/{messageId}")
    suspend fun deleteMessage(@Path("chatId") chatId: Long, @Path("messageId") messageId: Long): Response<Unit>

    @PUT("api/chat/chats/{chatId}/messages/{messageId}/status/{status}")
    suspend fun updateMessageStatus(
        @Path("chatId") chatId: Long,
        @Path("messageId") messageId: Long,
        @Path("status") status: String
    ): Response<Unit>

    @POST("api/chat/chats/{chatId}/messages/status/{status}")
    suspend fun updateMessagesStatus(
        @Path("chatId") chatId: Long,
        @Path("status") status: String,
        @Body messageIds: List<Long>
    ): Response<List<ChatMessage>>

    @PUT("api/chat/chats/{chatId}/messages/{messageId}/react/{reaction}")
    suspend fun reactToMessage(
        @Path("chatId") chatId: Long,
        @Path("messageId") messageId: Long,
        @Path("reaction") reaction: String
    ): Response<ChatMessage>

    @PUT("api/chat/chats/{chatId}/messages/{messageId}/unreact/{reactionId}")
    suspend fun unreactToMessage(
        @Path("chatId") chatId: Long,
        @Path("messageId") messageId: Long,
        @Path("reactionId") reactionId: Long
    ): Response<ChatMessage>

    // Mark all messages as read
    @PUT("api/chat/chats/{chatId}/status/read")
    suspend fun markAllMessagesAsRead(@Path("chatId") chatId: Long): Response<Unit>

    @POST("api/chat/chats/{chatId}/messages/status/seen")
    suspend fun markMessagesAsRead(@Path("chatId") chatId: Long, @Body request: MarkMessagesReadRequest): Response<List<ChatMessage>>

    // Unseen count
    @GET("api/chat/unseen-count")
    suspend fun getUnseenCount(): Response<Int>

    // Users for chat creation
    @GET("api/iam/users")
    suspend fun getUsers(
        @Query("limit") limit: Int = 50,
        @Query("offset") offset: Int = 0,
        @Query("search") search: String? = null
    ): Response<List<User>>
}

// User models for address book
data class User(
    val id: Long,
    val firstName: String,
    val lastName: String,
    val email: String,
    val phone: String? = null,
    val role: String,
    val isActive: Boolean,
    val departmentId: Long? = null,
    val position: String? = null,
    val avatarUrl: String? = null,
    val analyticsId: String? = null,
    val objectPermissions: List<ObjectPermission>? = null,
    val accessibleUserIds: List<Long>? = null,
    val isPlatformAdmin: Boolean
) {
    // Computed property for full name
    val name: String
        get() = "$firstName $lastName"
}

data class ObjectPermission(
    val objectType: String,
    val objectId: Long? = null,
    val createPermission: String,
    val viewPermission: String,
    val editPermission: String,
    val deletePermission: String,
    val reportPermission: String,
    val dashboardPermission: String
)
