package com.erp.systemtool.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * Operation Log Entity
 * 
 * Records all user operations for audit and tracking.
 * Migrations from PHP OperationLog model.
 */
@Data
@Entity
@Table(name = "operation_logs", indexes = {
    @Index(name = "idx_user_id", columnList = "user_id"),
    @Index(name = "idx_module", columnList = "module"),
    @Index(name = "idx_created_at", columnList = "created_at"),
    @Index(name = "idx_user_module_action", columnList = "user_id, module, action")
})
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OperationLog {

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

    @Column(name = "tenant_id")
    private Integer tenantId;

    @Column(name = "user_id")
    private Integer userId;

    @Column(name = "user_name", length = 64)
    private String userName;

    @Column(name = "module", length = 64)
    private String module;

    @Column(name = "action", length = 64)
    private String action;

    @Column(name = "method", length = 10)
    private String method;

    @Column(name = "path", length = 255)
    private String path;

    @Column(name = "ip", length = 45)
    private String ip;

    @Column(name = "user_agent", length = 512)
    private String userAgent;

    @Column(name = "request_params", columnDefinition = "TEXT")
    private String requestParams;

    @Column(name = "response_code")
    private Integer responseCode;

    @Column(name = "execution_time")
    private Integer executionTime;

    @Column(name = "remark", length = 512)
    private String remark;

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

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