import Foundation
import SwiftData

@Model
final class StudySession {
    @Attribute(.unique) var id: String
    var title: String
    var modeRaw: String
    var videoId: String?
    var video2Id: String?
    var audioId: String?
    var pdfId: String?
    var pdf2Id: String?
    var mediaTime: Double
    var pdfPage: Int
    var splitRatio: Double
    var orientationRaw: String
    var playerExpanded: Bool
    var playerPosX: Double
    var playerPosY: Double
    var parentBarPosX: Double
    var parentBarPosY: Double
    var updatedAt: Date
    var albumId: String?
    var albumIndex: Int?
    var courseId: String?
    var itemIndex: Int?
    var isActive: Bool

    var mode: StudyMode {
        get { StudyMode(rawValue: modeRaw) ?? .singleVideo }
        set { modeRaw = newValue.rawValue }
    }

    var orientation: SplitOrientation {
        get { SplitOrientation(rawValue: orientationRaw) ?? .horizontal }
        set { orientationRaw = newValue.rawValue }
    }

    var resourceIds: SessionResources {
        get {
            SessionResources(
                video: videoId,
                video2: video2Id,
                audio: audioId,
                pdf: pdfId,
                pdf2: pdf2Id
            )
        }
        set {
            videoId = newValue.video
            video2Id = newValue.video2
            audioId = newValue.audio
            pdfId = newValue.pdf
            pdf2Id = newValue.pdf2
        }
    }

    init(
        id: String = StudySession.makeID(),
        title: String,
        mode: StudyMode,
        resourceIds: SessionResources,
        mediaTime: Double = 0,
        pdfPage: Int = 1,
        splitRatio: Double = 0.55,
        orientation: SplitOrientation = .horizontal,
        playerExpanded: Bool = false,
        playerPosX: Double = 24,
        playerPosY: Double = -1,
        parentBarPosX: Double = 16,
        parentBarPosY: Double = 16,
        updatedAt: Date = Date(),
        albumId: String? = nil,
        albumIndex: Int? = nil,
        courseId: String? = nil,
        itemIndex: Int? = nil,
        isActive: Bool = false
    ) {
        self.id = id
        self.title = title
        self.modeRaw = mode.rawValue
        self.videoId = resourceIds.video
        self.video2Id = resourceIds.video2
        self.audioId = resourceIds.audio
        self.pdfId = resourceIds.pdf
        self.pdf2Id = resourceIds.pdf2
        self.mediaTime = mediaTime
        self.pdfPage = pdfPage
        self.splitRatio = splitRatio
        self.orientationRaw = orientation.rawValue
        self.playerExpanded = playerExpanded
        self.playerPosX = playerPosX
        self.playerPosY = playerPosY
        self.parentBarPosX = parentBarPosX
        self.parentBarPosY = parentBarPosY
        self.updatedAt = updatedAt
        self.albumId = albumId
        self.albumIndex = albumIndex
        self.courseId = courseId
        self.itemIndex = itemIndex
        self.isActive = isActive
    }

    static func makeID() -> String {
        "ses_\(Int(Date().timeIntervalSince1970 * 1_000_000))"
    }
}
