package com.ikon.projectmanagement.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "meetings")
public class Meeting {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(name = "id", updatable = false, nullable = false)
    private UUID id;

    @Column(name = "title")
    private String title;

    @Column(name = "project_name")
    private String projectName;

     @Column(nullable = true)
    private UUID createdBy;

    @Column(nullable = true)
    private UUID accountId;

    @Column(name = "place")
    private String place;

    @Column(name = "date")
    private LocalDate date;

    @Column(name = "time")
    private String time;

    @Column(name = "duration")
    private String duration;

    @Column(name = "called_by")
    private String calledBy;

    @ElementCollection
    @CollectionTable(name = "meeting_attendees", joinColumns = @JoinColumn(name = "meeting_id"))
    private List<Attendee> attendees;

    @ElementCollection
    @CollectionTable(name = "meeting_agenda", joinColumns = @JoinColumn(name = "meeting_id"))
    private List<AgendaItem> agenda;

    @ElementCollection
    @CollectionTable(name = "meeting_decisions", joinColumns = @JoinColumn(name = "meeting_id"))
    @Column(name = "decision", columnDefinition = "TEXT")
    private List<String> decisions;

    @ElementCollection
    @CollectionTable(name = "meeting_actions", joinColumns = @JoinColumn(name = "meeting_id"))
    private List<ActionItem> actions;

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

    @Column(name = "project_identifier", nullable = false)
private UUID projectIdentifier;

    @Column(name = "status")
    private String status;

    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Embeddable
    public static class Attendee {

        @Column(name = "name")
        private String name;

        @Column(name = "role")
        private String role;

        @Column(name = "contact")
        private String contact;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Embeddable
    public static class AgendaItem {

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

        @Column(name = "owner")
        private String owner;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Embeddable
    public static class ActionItem {

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

        @Column(name = "responsible")
        private String responsible;

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