package com.ikon.projectmanagement.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import java.util.ArrayList;

@Entity
@Table(name = "projects", uniqueConstraints = @UniqueConstraint(columnNames = "project_identifier"))
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Project {

    @Id
    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "project_identifier")
    private UUID projectIdentifier;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(nullable = false)
    private UUID projectManager;

    @Column(nullable = false)
    private String projectName;

    @Column(nullable = false)
    private UUID accountId;

    @Column(nullable = true)
    private String projectStatus;

    @Column(nullable = true)
    private String projectNumber;

    @Column(nullable = true)
    private String parentProjectNo;

    @Column(nullable = true)
    private String contractNumber;

    @Column(nullable = true)
    private String projectClient;

    @Column(nullable = true)
    private String projectCity;

    @Column(nullable = true)
    private String projectCountry;

    @Column(nullable = true)
    private String currency;

    @Column(nullable = true)
    private String projectImage;

    @Column(nullable = true)
    private String contractUpload;

    @Column(nullable = true)
    private String source;

    @Column(nullable = true)
    private String productType;

    @Column(nullable = true)
    private String expenses;

    @Column(nullable = true)
    private String formattedActualRevenueIncludingVAT_deal;

    @Column(nullable = true)
    private Boolean isCompleted;

    @Column(nullable = true)
    private Boolean groupNotExist;

    @Column(nullable = true)
    private Boolean isDebtRevenue_deal;

    @Column(length = 2000, nullable = true)
    private String projectDescription;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(nullable = true)
    private UUID createdById;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(nullable = true)
    private UUID updatedBy;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(nullable = true)
    private UUID projectManagerDelegates;

    @Column(nullable = true)
    private LocalDate projectStartDate;

    @Column(nullable = true)
    private LocalDate contractedStartDate;

    @Column(nullable = true)
    private LocalDate contractedEndDate;

    @Column(nullable = true)
    private LocalDate updatedOn;

    @Column(nullable = true)
    private LocalDate createdOn;

    @ElementCollection
    @CollectionTable(name = "project_team", joinColumns = @JoinColumn(name = "project_id"))
    @Column(name = "user_id", nullable = true)
    @JdbcTypeCode(SqlTypes.UUID)
    @Builder.Default
    private List<UUID> projectTeam = new ArrayList<>();

    @ElementCollection
    @CollectionTable(name = "project_team_pm", joinColumns = @JoinColumn(name = "project_id"))
    @Column(nullable = true)
    @JdbcTypeCode(SqlTypes.UUID)
    private List<UUID> projectTeamUnderProjectManager = new ArrayList<>();

    @ElementCollection
    @CollectionTable(name = "project_team_pm_delegates", joinColumns = @JoinColumn(name = "project_id"))
    @Column(nullable = true)
    @JdbcTypeCode(SqlTypes.UUID)
    private List<UUID> projectTeamUnderProjectManagerDelegates = new ArrayList<>();

    @Column(length = 2000, nullable = true)
    private String groupAssigneesEditStr;

    @Column(length = 2000, nullable = true)
    private String groupAssigneesViewStr;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(columnDefinition = "json", nullable = true)
    private Map<String, Object> participants;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(columnDefinition = "json", nullable = true)
    private Map<String, Object> contractedProductIdentifierWiseDataObj;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "product_identifier", nullable = true)
    private ProductOfProject product;

   
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "project_identifier")
@Builder.Default
private List<Risks> risks = new ArrayList<>();


@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "project_identifier")
@Builder.Default
private List<Meeting> meetings = new ArrayList<>();
}

