package com.prodhack.moscow2025.data.data_providers.local_db.entities

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.prodhack.moscow2025.domain.models.ExperienceType
import com.prodhack.moscow2025.domain.models.ResumeModel

@Entity(tableName = "resume_history")
data class ResumeHistoryEntity(
	@PrimaryKey(autoGenerate = false)
	@ColumnInfo("resume_id")
	val resumeId: String,
	@ColumnInfo("experience_type")
	val experienceType: String,
	@ColumnInfo("about_me")
	val aboutMe: String,
	@ColumnInfo("key_skills")
	val keySkills: String,
	val position: String,
	@ColumnInfo("from_salary")
	val fromSalary: Float?,
	@ColumnInfo("to_salary")
	val toSalary: Float?,
	@ColumnInfo("recommended_skills")
	val recommendedSkills: String,
	val city: String,
	val experience: String,
	val education: String,
	val projects: String
) {
	fun mapToDomain(): ResumeModel = ResumeModel(
		id = resumeId,
		position = position,
		about = aboutMe,
		experienceType = ExperienceType.valueOf(experienceType),
		skills = keySkills.split("|"),
		prediction = Pair(fromSalary, toSalary),
		recommendedSkills = if (recommendedSkills.isBlank()) emptyList() else recommendedSkills.split("|").filter { it.isNotBlank() },
		city = city,
		experience = JsonTypeConverters.toWorkExperienceList(experience),
		education = JsonTypeConverters.toEducationList(education),
		projects = JsonTypeConverters.toProjectList(projects)
	)

	companion object {
		fun fromDomain(model: ResumeModel): ResumeHistoryEntity = ResumeHistoryEntity(
			resumeId = model.id,
			experienceType = model.experienceType.name,
			aboutMe = model.about,
			keySkills = model.skills.joinToString("|"),
			position = model.position,
			fromSalary = model.prediction?.first,
			toSalary = model.prediction?.second,
			recommendedSkills = model.recommendedSkills?.joinToString("|") ?: "",
			city = model.city,
			experience = JsonTypeConverters.fromWorkExperienceList(model.experience),
			education = JsonTypeConverters.fromEducationList(model.education),
			projects = JsonTypeConverters.fromProjectList(model.projects)
		)
	}
}
