package com.mvrent.carrental.model;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/** A service / repair entry in a car's maintenance history. */
@Entity
@Table(name = "maintenance_records")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MaintenanceRecord {

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

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "car_id")
    private Car car;

    @Enumerated(EnumType.STRING)
    @NotNull
    private MaintenanceType type;

    @Enumerated(EnumType.STRING)
    @NotNull
    @Builder.Default
    private MaintenanceStatus status = MaintenanceStatus.COMPLETED;

    private LocalDate serviceDate;

    /** Odometer reading at the time of service (km). */
    private Integer odometer;

    private BigDecimal cost;

    /** Garage / workshop that did the work. */
    private String garage;

    @Column(length = 1000)
    private String notes;

    private LocalDate nextServiceDate;

    private Integer nextServiceOdometer;

    @CreationTimestamp
    private LocalDateTime createdAt;

    @Transient
    public boolean isOpen() {
        return status != MaintenanceStatus.COMPLETED;
    }

    @Transient
    public boolean isDueSoon() {
        return nextServiceDate != null
                && !nextServiceDate.isBefore(LocalDate.now())
                && nextServiceDate.isBefore(LocalDate.now().plusDays(30));
    }
}
