package com.smartoffice.attendance;

import com.smartoffice.organization.Department;
import jakarta.persistence.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;

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

    @Column(nullable = false)
    private String ruleName;

    @Column(nullable = false)
    private LocalTime workStartTime;

    @Column(nullable = false)
    private LocalTime workEndTime;

    // Comma-separated ISO day-of-week numbers (1=Mon ... 7=Sun)
    @Column(nullable = false)
    private String workDays = "1,2,3,4,5";

    private LocalTime checkInWindowStart;

    private LocalTime checkInWindowEnd;

    private LocalTime checkOutWindowStart;

    private LocalTime checkOutWindowEnd;

    @Column(nullable = false)
    private Integer lateMinutesThreshold = 0;

    @Column(nullable = false)
    private Integer earlyLeaveMinutesThreshold = 0;

    private LocalDate effectiveFrom;

    private LocalDate effectiveTo;

    private boolean enabled = true;

    private boolean allowMakeup = false;

    private Integer maxMakeupPerMonth = 0;

    private Integer flexMinutes = 0;

    private String workCalendarCode;

    private String workCalendarName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department;

    @Column(name = "created_at")
    private Instant createdAt;

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

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

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

    public Long getId() {
        return id;
    }

    public String getRuleName() {
        return ruleName;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    public LocalTime getWorkStartTime() {
        return workStartTime;
    }

    public void setWorkStartTime(LocalTime workStartTime) {
        this.workStartTime = workStartTime;
    }

    public LocalTime getWorkEndTime() {
        return workEndTime;
    }

    public void setWorkEndTime(LocalTime workEndTime) {
        this.workEndTime = workEndTime;
    }

    public String getWorkDays() {
        return workDays;
    }

    public void setWorkDays(String workDays) {
        this.workDays = workDays;
    }

    public LocalTime getCheckInWindowStart() {
        return checkInWindowStart;
    }

    public void setCheckInWindowStart(LocalTime checkInWindowStart) {
        this.checkInWindowStart = checkInWindowStart;
    }

    public LocalTime getCheckInWindowEnd() {
        return checkInWindowEnd;
    }

    public void setCheckInWindowEnd(LocalTime checkInWindowEnd) {
        this.checkInWindowEnd = checkInWindowEnd;
    }

    public LocalTime getCheckOutWindowStart() {
        return checkOutWindowStart;
    }

    public void setCheckOutWindowStart(LocalTime checkOutWindowStart) {
        this.checkOutWindowStart = checkOutWindowStart;
    }

    public LocalTime getCheckOutWindowEnd() {
        return checkOutWindowEnd;
    }

    public void setCheckOutWindowEnd(LocalTime checkOutWindowEnd) {
        this.checkOutWindowEnd = checkOutWindowEnd;
    }

    public boolean isAllowMakeup() {
        return allowMakeup;
    }

    public void setAllowMakeup(boolean allowMakeup) {
        this.allowMakeup = allowMakeup;
    }

    public Integer getMaxMakeupPerMonth() {
        return maxMakeupPerMonth;
    }

    public void setMaxMakeupPerMonth(Integer maxMakeupPerMonth) {
        this.maxMakeupPerMonth = maxMakeupPerMonth;
    }

    public Integer getFlexMinutes() {
        return flexMinutes;
    }

    public void setFlexMinutes(Integer flexMinutes) {
        this.flexMinutes = flexMinutes;
    }

    public String getWorkCalendarCode() {
        return workCalendarCode;
    }

    public void setWorkCalendarCode(String workCalendarCode) {
        this.workCalendarCode = workCalendarCode;
    }

    public String getWorkCalendarName() {
        return workCalendarName;
    }

    public void setWorkCalendarName(String workCalendarName) {
        this.workCalendarName = workCalendarName;
    }

    public Integer getLateMinutesThreshold() {
        return lateMinutesThreshold;
    }

    public void setLateMinutesThreshold(Integer lateMinutesThreshold) {
        this.lateMinutesThreshold = lateMinutesThreshold;
    }

    public Integer getEarlyLeaveMinutesThreshold() {
        return earlyLeaveMinutesThreshold;
    }

    public void setEarlyLeaveMinutesThreshold(Integer earlyLeaveMinutesThreshold) {
        this.earlyLeaveMinutesThreshold = earlyLeaveMinutesThreshold;
    }

    public LocalDate getEffectiveFrom() {
        return effectiveFrom;
    }

    public void setEffectiveFrom(LocalDate effectiveFrom) {
        this.effectiveFrom = effectiveFrom;
    }

    public LocalDate getEffectiveTo() {
        return effectiveTo;
    }

    public void setEffectiveTo(LocalDate effectiveTo) {
        this.effectiveTo = effectiveTo;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Instant getCreatedAt() {
        return createdAt;
    }

    public Instant getUpdatedAt() {
        return updatedAt;
    }
}
