package com.ikon.projectmanagement.entity;

import jakarta.persistence.*;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

@Entity
@Table(name = "issues")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Issue {

    @Id
    @GeneratedValue
    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "issue_id", nullable = false)
    private UUID issueId;

    @Column(name = "issue_title", nullable = false)
    private String issueTitle;

    @Column(name = "issue_probability")
    private Integer issueProbability;

    @Column(name = "gross_issue_value")
    private BigDecimal grossIssueValue;

    @Column(name = "probable_issue_value")
    private BigDecimal probableIssueValue;

    @Column(name = "probable_issue_value_usd")
    private BigDecimal probableIssueValueInUSD;

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

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

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

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

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

    @Column(name = "financial_issue")
    private Boolean financialIssue;

    @Column(name = "issue_created_date")
    private LocalDateTime issueCreatedDate;

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

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

    @Column(name = "issue_age")
    private Integer issueAge;

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

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

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

    @PrePersist
    protected void onCreate() {
        this.createdAt = LocalDateTime.now();
        if (this.issueCreatedDate == null) {
            this.issueCreatedDate = LocalDateTime.now();
        }
    }

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