package com.swoosh.microblog.data.db

import androidx.room.*
import com.swoosh.microblog.data.model.LocalPost
import com.swoosh.microblog.data.model.PostStatus
import com.swoosh.microblog.data.model.QueueStatus
import kotlinx.coroutines.flow.Flow

@Dao
interface LocalPostDao {

    @Query("SELECT * FROM local_posts ORDER BY updatedAt DESC")
    fun getAllPosts(): Flow<List<LocalPost>>

    @Query("SELECT * FROM local_posts WHERE status = :status ORDER BY updatedAt DESC")
    fun getPostsByStatus(status: PostStatus): Flow<List<LocalPost>>

    @Query("SELECT * FROM local_posts ORDER BY createdAt ASC")
    fun getAllPostsOldestFirst(): Flow<List<LocalPost>>

    @Query("SELECT * FROM local_posts WHERE status = :status ORDER BY createdAt ASC")
    fun getPostsByStatusOldestFirst(status: PostStatus): Flow<List<LocalPost>>

    @Query("SELECT * FROM local_posts WHERE accountId = :accountId ORDER BY updatedAt DESC")
    fun getPostsByAccount(accountId: String): Flow<List<LocalPost>>

    @Query("SELECT * FROM local_posts WHERE queueStatus IN (:statuses) ORDER BY createdAt ASC")
    suspend fun getQueuedPosts(
        statuses: List<QueueStatus> = listOf(QueueStatus.QUEUED_PUBLISH, QueueStatus.QUEUED_SCHEDULED, QueueStatus.QUEUED_EMAIL_ONLY)
    ): List<LocalPost>

    @Query("SELECT * FROM local_posts WHERE accountId = :accountId AND queueStatus IN (:statuses) ORDER BY createdAt ASC")
    suspend fun getQueuedPostsByAccount(
        accountId: String,
        statuses: List<QueueStatus> = listOf(QueueStatus.QUEUED_PUBLISH, QueueStatus.QUEUED_SCHEDULED, QueueStatus.QUEUED_EMAIL_ONLY)
    ): List<LocalPost>

    @Query("SELECT * FROM local_posts WHERE localId = :localId")
    suspend fun getPostById(localId: Long): LocalPost?

    @Query("SELECT * FROM local_posts WHERE ghostId = :ghostId LIMIT 1")
    suspend fun getPostByGhostId(ghostId: String): LocalPost?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertPost(post: LocalPost): Long

    @Update
    suspend fun updatePost(post: LocalPost)

    @Delete
    suspend fun deletePost(post: LocalPost)

    @Query("SELECT * FROM local_posts WHERE content LIKE '%' || :query || '%' OR title LIKE '%' || :query || '%' ORDER BY createdAt DESC")
    suspend fun searchPosts(query: String): List<LocalPost>

    @Query("DELETE FROM local_posts WHERE localId = :localId")
    suspend fun deleteById(localId: Long)

    @Query("UPDATE local_posts SET queueStatus = :status WHERE localId = :localId")
    suspend fun updateQueueStatus(localId: Long, status: QueueStatus)

    @Query("UPDATE local_posts SET ghostId = :ghostId, queueStatus = :status WHERE localId = :localId")
    suspend fun markUploaded(localId: Long, ghostId: String, status: QueueStatus = QueueStatus.NONE)

    @Query("SELECT COUNT(*) FROM local_posts")
    suspend fun getTotalPostCount(): Int

    @Query("SELECT COUNT(*) FROM local_posts WHERE status = :status")
    suspend fun getPostCountByStatus(status: PostStatus): Int

    @Query("SELECT * FROM local_posts ORDER BY updatedAt DESC")
    suspend fun getAllPostsList(): List<LocalPost>

    @Query("UPDATE local_posts SET featured = :featured WHERE localId = :localId")
    suspend fun updateFeatured(localId: Long, featured: Boolean)

    @Query("UPDATE local_posts SET featured = :featured WHERE ghostId = :ghostId")
    suspend fun updateFeaturedByGhostId(ghostId: String, featured: Boolean)
}
