package com.davos.assessment.invitation;

import com.davos.assessment.project.Project;
import jakarta.persistence.*;
import java.time.Instant;

@Entity
@Table(name = "invitation_codes")
public class InvitationCode {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(nullable = false)
  private String code;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "project_id", nullable = false)
  private Project project;

  @Column(name = "max_uses", nullable = false)
  private int maxUses;

  @Column(name = "used_count", nullable = false)
  private int usedCount;

  @Column(name = "expires_at")
  private Instant expiresAt;

  @Column(nullable = false)
  private boolean enabled = true;

  @Column(name = "created_at", nullable = false)
  private Instant createdAt = Instant.now();

  public boolean canUse() {
    return enabled && usedCount < maxUses && (expiresAt == null || expiresAt.isAfter(Instant.now()));
  }

  public Long getId() { return id; }
  public Project getProject() { return project; }
  public void setProject(Project project) { this.project = project; }
  public String getCode() { return code; }
  public void setCode(String code) { this.code = code; }
  public int getMaxUses() { return maxUses; }
  public void setMaxUses(int maxUses) { this.maxUses = maxUses; }
  public int getUsedCount() { return usedCount; }
  public void setUsedCount(int usedCount) { this.usedCount = usedCount; }
  public Instant getExpiresAt() { return expiresAt; }
  public void setExpiresAt(Instant expiresAt) { this.expiresAt = expiresAt; }
  public boolean isEnabled() { return enabled; }
  public void setEnabled(boolean enabled) { this.enabled = enabled; }
  public Instant getCreatedAt() { return createdAt; }
}
