import Foundation
import SwiftData

@Model
final class CachedShelf {
    @Attribute(.unique) var id: Int
    var name: String
    var slug: String
    var shelfDescription: String
    var coverURL: String?
    var lastFetched: Date

    init(id: Int, name: String, slug: String, shelfDescription: String, coverURL: String? = nil) {
        self.id = id
        self.name = name
        self.slug = slug
        self.shelfDescription = shelfDescription
        self.coverURL = coverURL
        self.lastFetched = Date()
    }

    convenience init(from dto: ShelfDTO) {
        self.init(
            id: dto.id,
            name: dto.name,
            slug: dto.slug,
            shelfDescription: dto.description,
            coverURL: dto.cover?.url
        )
    }
}

@Model
final class CachedBook {
    @Attribute(.unique) var id: Int
    var name: String
    var slug: String
    var bookDescription: String
    var coverURL: String?
    var lastFetched: Date

    init(id: Int, name: String, slug: String, bookDescription: String, coverURL: String? = nil) {
        self.id = id
        self.name = name
        self.slug = slug
        self.bookDescription = bookDescription
        self.coverURL = coverURL
        self.lastFetched = Date()
    }

    convenience init(from dto: BookDTO) {
        self.init(
            id: dto.id,
            name: dto.name,
            slug: dto.slug,
            bookDescription: dto.description,
            coverURL: dto.cover?.url
        )
    }
}

@Model
final class CachedPage {
    @Attribute(.unique) var id: Int
    var bookId: Int
    var chapterId: Int?
    var name: String
    var slug: String
    var html: String?
    var markdown: String?
    var lastFetched: Date

    init(id: Int, bookId: Int, chapterId: Int? = nil, name: String, slug: String, html: String? = nil, markdown: String? = nil) {
        self.id = id
        self.bookId = bookId
        self.chapterId = chapterId
        self.name = name
        self.slug = slug
        self.html = html
        self.markdown = markdown
        self.lastFetched = Date()
    }

    convenience init(from dto: PageDTO) {
        self.init(
            id: dto.id,
            bookId: dto.bookId,
            chapterId: dto.chapterId,
            name: dto.name,
            slug: dto.slug,
            html: dto.html,
            markdown: dto.markdown
        )
    }
}
