package com.unicorn.hgzero.meeting.infra.gateway.entity;

import com.unicorn.hgzero.common.entity.BaseTimeEntity;
import com.unicorn.hgzero.meeting.biz.domain.Todo;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Todo Entity
 */
@Entity
@Table(name = "todos")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TodoEntity extends BaseTimeEntity {

    @Id
    @Column(name = "todo_id", length = 50)
    private String todoId;

    @Column(name = "minutes_id", length = 50)
    private String minutesId;

    @Column(name = "meeting_id", length = 50, nullable = false)
    private String meetingId;

    @Column(name = "title", length = 200, nullable = false)
    private String title;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "assignee_id", length = 50, nullable = false)
    private String assigneeId;

    @Column(name = "due_date")
    private LocalDate dueDate;

    @Column(name = "status", length = 20, nullable = false)
    @Builder.Default
    private String status = "PENDING";

    @Column(name = "priority", length = 20)
    @Builder.Default
    private String priority = "MEDIUM";

    @Column(name = "completed_at")
    private LocalDateTime completedAt;

    public Todo toDomain() {
        return Todo.builder()
                .todoId(this.todoId)
                .minutesId(this.minutesId)
                .meetingId(this.meetingId)
                .title(this.title)
                .description(this.description)
                .assigneeId(this.assigneeId)
                .dueDate(this.dueDate)
                .status(this.status)
                .priority(this.priority)
                .completedAt(this.completedAt)
                .build();
    }

    public static TodoEntity fromDomain(Todo todo) {
        return TodoEntity.builder()
                .todoId(todo.getTodoId())
                .minutesId(todo.getMinutesId())
                .meetingId(todo.getMeetingId())
                .title(todo.getTitle())
                .description(todo.getDescription())
                .assigneeId(todo.getAssigneeId())
                .dueDate(todo.getDueDate())
                .status(todo.getStatus())
                .priority(todo.getPriority())
                .completedAt(todo.getCompletedAt())
                .build();
    }

    public void complete() {
        this.status = "COMPLETED";
        this.completedAt = LocalDateTime.now();
    }
}
