package de.oaa.xxx.games.chastity.common;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import de.oaa.xxx.games.chastity.lockcontroll.LockControllType;
import de.oaa.xxx.games.chastity.tasks.Task;
import de.oaa.xxx.games.chastity.tasks.TaskListConverter;
import de.oaa.xxx.games.chastity.tasks.TaskMode;
import de.oaa.xxx.games.chastity.unlock.TempOpeningReason;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "active_lock")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "lock_type", discriminatorType = DiscriminatorType.STRING)
public class BaseLockEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.UUID)
	@Column
	private UUID lockId;
	@Column(nullable = false)
	private String name;
	// --- Gemeinsame Settings ---
	@Column(nullable = false)
	private UUID lockee;
	@Column
	private UUID keyholder;
	@Column(nullable = false)
	private boolean testLock;
	@Column
	private boolean requiresVerification;
	@Column
	private Integer unlockCodeLength;
	@Column
	private String unlockCode;
	@Enumerated(EnumType.STRING)
	@Column(length = 20)
	private LockControllType controllType;

	// --- Timing & Hygiene ---
	@Column
	private LocalDateTime startTime;
	@Column
	private LocalDateTime unlockTime;
	@Column
	private LocalDateTime lastHygineOpening;
	@Column
	private Integer hygineOpeningDurationMinutes;
	@Column
	private Integer hygineOpeningEveryMinites;
	@Column
	private LocalDateTime tempOpeningTime; // If null, not while hygine opening
	@Column
	private Integer tempOpeningDuration;
	@Column
	private TempOpeningReason tempOpeningReason;
	@Column
	private LocalDateTime frozenUntil;

	// --- Aufgaben-System (Basis) ---
	@Convert(converter = TaskListConverter.class)
	@Column(columnDefinition = "TEXT")
	private List<Task> tasks;
	@Column
	private String currentTask;
	@Column(columnDefinition = "TEXT")
	private String currentTaskDescription;
	@Column
	private LocalDateTime taskUntil;
	@Enumerated(EnumType.STRING)
	@Column(nullable = false)
	private TaskMode taskMode = TaskMode.RANDOM;

	// --- Notfall- & Keyholder-Status ---
	@Column(nullable = false)
	private boolean keyholderRequestedUnlock = false;
	@Column
	private LocalDateTime emergencyUnlockRequestedAt;
	@Column(nullable = false)
	private boolean emergencyAutoUnlocked = false;
	
	// Getter & Setter
	public TaskMode getTaskMode() {
		return taskMode != null ? taskMode : TaskMode.RANDOM;
	}
}
