package com.swoosh.microblog.data.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.compose.runtime.Stable
import com.google.gson.annotations.SerializedName

// --- API Response/Request Models ---

data class PostsResponse(
    val posts: List<GhostPost>,
    val meta: Meta?
)

data class Meta(
    val pagination: Pagination?
)

data class Pagination(
    val page: Int,
    val limit: Int,
    val pages: Int,
    val total: Int,
    val next: Int?,
    val prev: Int?
)

data class PostWrapper(
    val posts: List<GhostPost>
)

data class GhostPost(
    val id: String? = null,
    val title: String? = null,
    val slug: String? = null,
    val url: String? = null,
    val html: String? = null,
    val plaintext: String? = null,
    val mobiledoc: String? = null,
    val status: String? = null,
    val featured: Boolean? = false,
    val feature_image: String? = null,
    val feature_image_alt: String? = null,
    val created_at: String? = null,
    val updated_at: String? = null,
    val published_at: String? = null,
    val custom_excerpt: String? = null,
    val visibility: String? = "public",
    val authors: List<Author>? = null,
    val reading_time: Int? = null,
    val tags: List<GhostTag>? = null,
    val email_only: Boolean? = null
)

data class GhostTag(
    val id: String? = null,
    val name: String,
    val slug: String? = null
)

data class Author(
    val id: String,
    val name: String?,
    val profile_image: String? = null
)

// --- Local Database Entity ---

@Entity(tableName = "local_posts")
data class LocalPost(
    @PrimaryKey(autoGenerate = true)
    val localId: Long = 0,
    val ghostId: String? = null,
    val accountId: String = "",
    val title: String = "",
    val content: String = "",
    val htmlContent: String? = null,
    val status: PostStatus = PostStatus.DRAFT,
    val featured: Boolean = false,
    val imageUri: String? = null,
    val uploadedImageUrl: String? = null,
    val imageUris: String? = null,           // JSON array of local URIs
    val uploadedImageUrls: String? = null,   // JSON array of uploaded URLs
    val linkUrl: String? = null,
    val linkTitle: String? = null,
    val linkDescription: String? = null,
    val linkImageUrl: String? = null,
    val imageAlt: String? = null,
    val scheduledAt: String? = null,
    val tags: String = "[]",
    val createdAt: Long = System.currentTimeMillis(),
    val updatedAt: Long = System.currentTimeMillis(),
    val queueStatus: QueueStatus = QueueStatus.NONE,
    // Phase 4b: email-only
    val emailOnly: Boolean = false,
    val newsletterSlug: String? = null,
    // Phase 5: media
    val videoUri: String? = null,
    val uploadedVideoUrl: String? = null,
    val audioUri: String? = null,
    val uploadedAudioUrl: String? = null,
    // Phase 6: file
    val fileUri: String? = null,
    val uploadedFileUrl: String? = null,
    val fileName: String? = null
)

enum class PostStatus {
    DRAFT,
    PUBLISHED,
    SCHEDULED,
    SENT
}

enum class QueueStatus {
    NONE,
    QUEUED_PUBLISH,
    QUEUED_SCHEDULED,
    QUEUED_EMAIL_ONLY,
    UPLOADING,
    FAILED
}

// --- UI Display Model ---

@Stable
data class FeedPost(
    val localId: Long? = null,
    val ghostId: String? = null,
    val slug: String? = null,
    val url: String? = null,
    val title: String,
    val textContent: String,
    val htmlContent: String?,
    val imageUrl: String?,
    val imageAlt: String? = null,
    val imageUrls: List<String> = emptyList(),
    val videoUrl: String? = null,
    val audioUrl: String? = null,
    val linkUrl: String?,
    val linkTitle: String?,
    val linkDescription: String?,
    val linkImageUrl: String?,
    val tags: List<String> = emptyList(),
    val status: String,
    val featured: Boolean = false,
    val publishedAt: String?,
    val createdAt: String?,
    val updatedAt: String?,
    val isLocal: Boolean = false,
    val queueStatus: QueueStatus = QueueStatus.NONE,
    val fileUrl: String? = null,
    val fileName: String? = null,
    val emailOnly: Boolean = false
)

@Stable
data class LinkPreview(
    val url: String,
    val title: String?,
    val description: String?,
    val imageUrl: String?
)

// --- Feed Filter & Sort ---

enum class PostFilter(val displayName: String, val ghostFilter: String?) {
    ALL("All", null),
    PUBLISHED("Published", "status:published"),
    DRAFT("Drafts", "status:draft"),
    SCHEDULED("Scheduled", "status:scheduled"),
    SENT("Sent", "status:sent");

    /** Returns the matching [PostStatus] for local filtering, or null for ALL. */
    fun toPostStatus(): PostStatus? = when (this) {
        ALL -> null
        PUBLISHED -> PostStatus.PUBLISHED
        DRAFT -> PostStatus.DRAFT
        SCHEDULED -> PostStatus.SCHEDULED
        SENT -> PostStatus.SENT
    }

    /** Empty-state message shown when filter yields no results. */
    fun emptyMessage(): String = when (this) {
        ALL -> "No posts yet"
        PUBLISHED -> "No published posts yet"
        DRAFT -> "No drafts yet"
        SCHEDULED -> "No scheduled posts yet"
        SENT -> "No sent newsletters yet"
    }
}

enum class SortOrder(val displayName: String, val ghostOrder: String) {
    NEWEST("Newest first", "published_at desc"),
    OLDEST("Oldest first", "published_at asc"),
    RECENTLY_UPDATED("Recently updated", "updated_at desc")
}
