package com.prodhack.moscow2025.domain.usecase.resumes

import com.prodhack.moscow2025.domain.models.ExperienceType
import com.prodhack.moscow2025.domain.models.ResumeModel

data class ResumeDiff(
	val changedFields: List<String>,
	val changes: List<ChangeModel>
)

data class ChangeModel(
	val title: String,
	val body: String
)

class CalculateResumeDiffUseCase {
	operator fun invoke(previous: ResumeModel?, current: ResumeModel): ResumeDiff {
		if (previous == null) return ResumeDiff(
			changedFields = emptyList(),
			changes = emptyList()
		)

		val changedFields = mutableListOf<String>()
		val changes = mutableListOf<ChangeModel>()

		if (previous.position != current.position) {
			changedFields += "должность"
			changes.add(
				ChangeModel(
					title = "Должность",
					body = "${previous.position}  ->\n${current.position}"
				)
			)
		}
		if (previous.about != current.about) {
			changedFields += "описание"
			changes.add(
				ChangeModel(
					title = "Новое описание",
					body = current.about
				)
			)
		}
		if (previous.skills != current.skills) {
			changedFields += "навыки"
			val added = current.skills.toSet() - previous.skills.toSet()

			if (added.isNotEmpty()) {
				changes.add(
					ChangeModel(
						title = "Добавлены навыки",
						body = added.joinToString("\n")
					)
				)
			}
			val removed = previous.skills.toSet() - current.skills.toSet()

			if (removed.isNotEmpty()) {
				changes.add(
					ChangeModel(
						title = "Удалены навыки",
						body = removed.joinToString("\n")
					)
				)
			}

		}
		if (previous.city != current.city) {
			changedFields += "город"
			changes.add(
				ChangeModel(
					"Город",
					"${previous.city}  ->\n${current.city}"
				)
			)
		}
		if (previous.experienceType != current.experienceType) {
			changedFields += "опыт"
			changes.add(
				ChangeModel(
					"Опыт",
					"${previous.experienceType.toReadable()}  ->\n${current.experienceType.toReadable()}"
				)
			)
		}
		if (previous.experience != current.experience) {
			changedFields += "опыт работы"

			val added = current.experience.toSet() - previous.experience.toSet()

			if (added.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Добавлен опыт",
						added.joinToString("\n") { it.place }
					)
				)
			}
			val removed = previous.experience.toSet() - current.experience.toSet()

			if (removed.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Удален опыт",
						removed.joinToString("\n") { it.place }
					)
				)
			}
		}
		if (previous.education != current.education) {
			changedFields += "образование"
			val added = current.education.toSet() - previous.education.toSet()

			if (added.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Добавлено образование",
						added.joinToString("\n") { it.place }
					)
				)
			}
			val removed = previous.education.toSet() - current.education.toSet()

			if (removed.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Удалено образование",
						removed.joinToString("\n") { it.place }
					)
				)
			}
		}
		if (previous.projects != current.projects) {
			changedFields += "проекты"
			val added = current.projects.toSet() - previous.projects.toSet()

			if (added.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Добавлен проект",
						added.joinToString("\n") { it.name }
					)
				)
			}
			val removed = previous.projects.toSet() - current.projects.toSet()

			if (removed.isNotEmpty()) {
				changes.add(
					ChangeModel(
						"Удален проект",
						removed.joinToString("\n") { it.name }
					)
				)
			}
		}

		return ResumeDiff(
			changedFields = changedFields,
			changes = changes
		)
	}
}

private fun ExperienceType.toReadable(): String = when (this) {
	ExperienceType.NoExperience -> "Нет опыта"
	ExperienceType.Between1And3 -> "1-3 года"
	ExperienceType.Between3And6 -> "3-6 лет"
	ExperienceType.MoreThan6 -> "Более 6 лет"
}
