package pl.polskalokalnie.report;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "listing_reports")
public class ListingReport {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private Long listingId;

    @Column(nullable = false, length = 180)
    private String listingTitle;

    @Column(nullable = false, length = 120)
    private String listingCity;

    @Column(length = 180)
    private String listingOwnerEmail;

    @Column(nullable = false, length = 80)
    private String reasonId;

    @Column(nullable = false, length = 180)
    private String reasonTitle;

    @Column(length = 1000)
    private String details;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "listing_report_attachments", joinColumns = @JoinColumn(name = "report_id"))
    private List<ListingReportAttachment> attachments = new ArrayList<>();

    @Column(nullable = false, length = 180)
    private String reporterEmail;

    @Column(length = 120)
    private String reporterName;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private ListingReportStatus status = ListingReportStatus.OPEN;

    @Column(nullable = false)
    private boolean listingDeleted = false;

    @Column(nullable = false, updatable = false)
    private Instant createdAt;

    private Instant resolvedAt;

    @Column(length = 180)
    private String resolvedByEmail;

    @Column(length = 1000)
    private String resolutionNote;

    @PrePersist
    void onCreate() {
        if (createdAt == null) {
            createdAt = Instant.now();
        }
    }

    public Long getId() {
        return id;
    }

    public Long getListingId() {
        return listingId;
    }

    public void setListingId(Long listingId) {
        this.listingId = listingId;
    }

    public String getListingTitle() {
        return listingTitle;
    }

    public void setListingTitle(String listingTitle) {
        this.listingTitle = listingTitle;
    }

    public String getListingCity() {
        return listingCity;
    }

    public void setListingCity(String listingCity) {
        this.listingCity = listingCity;
    }

    public String getListingOwnerEmail() {
        return listingOwnerEmail;
    }

    public void setListingOwnerEmail(String listingOwnerEmail) {
        this.listingOwnerEmail = listingOwnerEmail;
    }

    public String getReasonId() {
        return reasonId;
    }

    public void setReasonId(String reasonId) {
        this.reasonId = reasonId;
    }

    public String getReasonTitle() {
        return reasonTitle;
    }

    public void setReasonTitle(String reasonTitle) {
        this.reasonTitle = reasonTitle;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public List<ListingReportAttachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(List<ListingReportAttachment> attachments) {
        this.attachments = attachments == null ? new ArrayList<>() : new ArrayList<>(attachments);
    }

    public String getReporterEmail() {
        return reporterEmail;
    }

    public void setReporterEmail(String reporterEmail) {
        this.reporterEmail = reporterEmail;
    }

    public String getReporterName() {
        return reporterName;
    }

    public void setReporterName(String reporterName) {
        this.reporterName = reporterName;
    }

    public ListingReportStatus getStatus() {
        return status;
    }

    public void setStatus(ListingReportStatus status) {
        this.status = status;
    }

    public boolean isListingDeleted() {
        return listingDeleted;
    }

    public void setListingDeleted(boolean listingDeleted) {
        this.listingDeleted = listingDeleted;
    }

    public Instant getCreatedAt() {
        return createdAt;
    }

    public Instant getResolvedAt() {
        return resolvedAt;
    }

    public void setResolvedAt(Instant resolvedAt) {
        this.resolvedAt = resolvedAt;
    }

    public String getResolvedByEmail() {
        return resolvedByEmail;
    }

    public void setResolvedByEmail(String resolvedByEmail) {
        this.resolvedByEmail = resolvedByEmail;
    }

    public String getResolutionNote() {
        return resolutionNote;
    }

    public void setResolutionNote(String resolutionNote) {
        this.resolutionNote = resolutionNote;
    }
}
