package com.imprimelibros.erp.configuracion.margenes_presupuestos;

import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDateTime;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;
import com.imprimelibros.erp.presupuesto.Presupuesto.TipoEncuadernacion;
import com.imprimelibros.erp.presupuesto.Presupuesto.TipoCubierta;
import com.imprimelibros.erp.shared.validation.NoRangeOverlap;


@NoRangeOverlap(
    min = "tiradaMin",
    max = "tiradaMax",
    id = "id",
    partitionBy = {"tipoEncuadernacion","tipoCubierta"},
    deletedFlag = "deleted",            // <- si usas soft delete
    deletedActiveValue = false,         // activo cuando deleted == false
    message = "{validation.range.overlaps}",
    invalidRangeMessage = "{validation.range.invalid}"
)
@Entity
@Table(name = "margenes_presupuesto")
@SQLDelete(sql = "UPDATE margenes_presupuesto SET deleted = TRUE, deleted_at = NOW() WHERE id = ?")
@SQLRestriction("deleted = false")
public class MargenPresupuesto {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="tipo_encuadernacion", nullable = false, length = 50)
    @NotNull(message="{validation.required}")
    @Enumerated(EnumType.STRING)
    private TipoEncuadernacion tipoEncuadernacion;

    @Column(name="tipo_cubierta", nullable = false, length = 50)
    @NotNull(message="{validation.required}")
    @Enumerated(EnumType.STRING)
    private TipoCubierta tipoCubierta;

    @Column(name="tirada_min", nullable = false)
    @NotNull(message="{validation.required}")
    @Min(value=1, message="{validation.min}")
    private Integer tiradaMin;

    @Column(name="tirada_max", nullable = false)
    @NotNull(message="{validation.required}")
    @Min(value=1, message="{validation.min}")
    private Integer tiradaMax;

    @Column(name="margen_max", nullable = false)
    @NotNull(message="{validation.required}")
    @Min(value = 0, message="{validation.min}")
    @Max(value = 200, message="{validation.max}")
    private Integer margenMax;

    @Column(name = "margen_min", nullable = false)
    @NotNull(message="{validation.required}")
    @Min(value = 0, message="{validation.min}")
    @Max(value = 200, message="{validation.max}")
    private Integer margenMin;

    @Column(name="created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @Column(name="updated_at")
    private LocalDateTime updatedAt;

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

    @Column(name="deleted_at")
    private LocalDateTime deletedAt;

    public Long getId() {
        return id;
    }

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

    public TipoEncuadernacion getTipoEncuadernacion() {
        return tipoEncuadernacion;
    }

    public void setTipoEncuadernacion(TipoEncuadernacion tipoEncuadernacion) {
        this.tipoEncuadernacion = tipoEncuadernacion;
    }

    public TipoCubierta getTipoCubierta() {
        return tipoCubierta;
    }

    public void setTipoCubierta(TipoCubierta tipoCubierta) {
        this.tipoCubierta = tipoCubierta;
    }

    public Integer getTiradaMin() {
        return tiradaMin;
    }

    public void setTiradaMin(Integer tiradaMin) {
        this.tiradaMin = tiradaMin;
    }

    public Integer getTiradaMax() {
        return tiradaMax;
    }

    public void setTiradaMax(Integer tiradaMax) {
        this.tiradaMax = tiradaMax;
    }

    public Integer getMargenMax() {
        return margenMax;
    }

    public void setMargenMax(Integer margenMax) {
        this.margenMax = margenMax;
    }

    public Integer getMargenMin() {
        return margenMin;
    }

    public void setMargenMin(Integer margenMin) {
        this.margenMin = margenMin;
    }

    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 isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public LocalDateTime getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(LocalDateTime deletedAt) {
        this.deletedAt = deletedAt;
    }

    @PrePersist
    void onCreate() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = this.createdAt;
    }

    @PreUpdate
    void onUpdate() {
        this.updatedAt = LocalDateTime.now();
    }
    
}

