package com.fieldlabel.collector.data.local

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.fieldlabel.collector.domain.model.RouteLog
import com.fieldlabel.collector.domain.model.RoutePoint

// ──────────────────────────────────────────────────────────────────────────────
// 移動ログ概要エンティティ
// ──────────────────────────────────────────────────────────────────────────────

@Entity(tableName = "route_logs")
data class RouteLogEntity(
    @PrimaryKey val id: String,
    val name: String,
    val startTime: Long,
    val endTime: Long?,
    val totalDistanceMeters: Double,
    val pointCount: Int
) {
    fun toDomain() = RouteLog(
        id                   = id,
        name                 = name,
        startTime            = startTime,
        endTime              = endTime,
        totalDistanceMeters  = totalDistanceMeters,
        pointCount           = pointCount
    )

    companion object {
        fun fromDomain(r: RouteLog) = RouteLogEntity(
            id                   = r.id,
            name                 = r.name,
            startTime            = r.startTime,
            endTime              = r.endTime,
            totalDistanceMeters  = r.totalDistanceMeters,
            pointCount           = r.pointCount
        )
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// 測位点エンティティ
// ──────────────────────────────────────────────────────────────────────────────

@Entity(
    tableName = "route_points",
    indices = [androidx.room.Index(value = ["routeLogId"])]
)
data class RoutePointEntity(
    @PrimaryKey val id: String,
    val routeLogId: String,
    val latitude: Double,
    val longitude: Double,
    val altitude: Double,
    val timestamp: Long,
    val accuracy: Float
) {
    fun toDomain() = RoutePoint(
        id         = id,
        routeLogId = routeLogId,
        latitude   = latitude,
        longitude  = longitude,
        altitude   = altitude,
        timestamp  = timestamp,
        accuracy   = accuracy
    )

    companion object {
        fun fromDomain(p: RoutePoint) = RoutePointEntity(
            id         = p.id,
            routeLogId = p.routeLogId,
            latitude   = p.latitude,
            longitude  = p.longitude,
            altitude   = p.altitude,
            timestamp  = p.timestamp,
            accuracy   = p.accuracy
        )
    }
}
