package com.mzh.library.entity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class BorrowRecord {
    private static final DateTimeFormatter DISPLAY_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    private long id;
    private long readerId;
    private String readerIdentifier;
    private String readerName;
    private long bookId;
    private String bookIdentifier;
    private String bookTitle;
    private LocalDateTime borrowedAt;
    private LocalDateTime dueAt;
    private LocalDateTime returnedAt;
    private int renewalCount;
    private BorrowRecordStatus status;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getReaderId() {
        return readerId;
    }

    public void setReaderId(long readerId) {
        this.readerId = readerId;
    }

    public String getReaderIdentifier() {
        return readerIdentifier;
    }

    public void setReaderIdentifier(String readerIdentifier) {
        this.readerIdentifier = readerIdentifier;
    }

    public String getReaderName() {
        return readerName;
    }

    public void setReaderName(String readerName) {
        this.readerName = readerName;
    }

    public long getBookId() {
        return bookId;
    }

    public void setBookId(long bookId) {
        this.bookId = bookId;
    }

    public String getBookIdentifier() {
        return bookIdentifier;
    }

    public void setBookIdentifier(String bookIdentifier) {
        this.bookIdentifier = bookIdentifier;
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }

    public LocalDateTime getBorrowedAt() {
        return borrowedAt;
    }

    public void setBorrowedAt(LocalDateTime borrowedAt) {
        this.borrowedAt = borrowedAt;
    }

    public LocalDateTime getDueAt() {
        return dueAt;
    }

    public void setDueAt(LocalDateTime dueAt) {
        this.dueAt = dueAt;
    }

    public LocalDateTime getReturnedAt() {
        return returnedAt;
    }

    public void setReturnedAt(LocalDateTime returnedAt) {
        this.returnedAt = returnedAt;
    }

    public int getRenewalCount() {
        return renewalCount;
    }

    public void setRenewalCount(int renewalCount) {
        this.renewalCount = renewalCount;
    }

    public BorrowRecordStatus getStatus() {
        return status;
    }

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

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public boolean isOverdue() {
        return BorrowRecordStatus.ACTIVE == status
            && returnedAt == null
            && dueAt != null
            && dueAt.isBefore(LocalDateTime.now());
    }

    public String getDisplayStatusCode() {
        return isOverdue() ? BorrowRecordSearchCriteria.OVERDUE_STATUS : status.getCode();
    }

    public String getDisplayStatusName() {
        return isOverdue() ? "逾期" : status.getDisplayName();
    }

    public String getBorrowedAtText() {
        return format(borrowedAt);
    }

    public String getDueAtText() {
        return format(dueAt);
    }

    public String getReturnedAtText() {
        return format(returnedAt);
    }

    private String format(LocalDateTime value) {
        return value == null ? "" : DISPLAY_FORMAT.format(value);
    }
}
