package com.mvrent.carrental.model;

import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.*;

import java.time.LocalDate;

/**
 * A compliance document (RC, insurance, permit, …) for a car, with an expiry
 * date so the fleet can flag documents that are expired or expiring soon.
 */
@Entity
@Table(name = "car_documents")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CarDocument {

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

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

    @Enumerated(EnumType.STRING)
    @NotNull
    private CarDocumentType docType;

    /** Path or URL to the stored file (local filesystem path for v1). */
    private String fileUrl;

    private LocalDate issuedDate;

    private LocalDate expiryDate;

    @Transient
    public boolean isExpired() {
        return expiryDate != null && expiryDate.isBefore(LocalDate.now());
    }

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