package com.mosquito.project.persistence.entity;

import jakarta.persistence.*;
import java.time.OffsetDateTime;
import java.util.Map;

@Entity
@Table(name = "link_clicks", indexes = {
    @Index(name = "idx_link_clicks_code", columnList = "code"),
    @Index(name = "idx_link_clicks_activity", columnList = "activity_id"),
    @Index(name = "idx_link_clicks_created_at", columnList = "created_at")
})
public class LinkClickEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 32)
    private String code;

    @Column(name = "activity_id")
    private Long activityId;

    @Column(name = "inviter_user_id")
    private Long inviterUserId;

    @Column(length = 64)
    private String ip;

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

    @Column(length = 1024)
    private String referer;

    @Column(columnDefinition = "TEXT")
    private String params;

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

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
    public Long getActivityId() { return activityId; }
    public void setActivityId(Long activityId) { this.activityId = activityId; }
    public Long getInviterUserId() { return inviterUserId; }
    public void setInviterUserId(Long inviterUserId) { this.inviterUserId = inviterUserId; }
    public String getIp() { return ip; }
    public void setIp(String ip) { this.ip = ip; }
    public String getUserAgent() { return userAgent; }
    public void setUserAgent(String userAgent) { this.userAgent = userAgent; }
    public String getReferer() { return referer; }
    public void setReferer(String referer) { this.referer = referer; }
    public Map<String, String> getParams() {
        if (params == null || params.isBlank()) {
            return null;
        }
        try {
            com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
            return mapper.readValue(params, java.util.Map.class);
        } catch (Exception e) {
            return null;
        }
    }
    public void setParams(Map<String, String> paramsMap) {
        if (paramsMap == null) {
            this.params = null;
        } else {
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                this.params = mapper.writeValueAsString(paramsMap);
            } catch (Exception e) {
                this.params = null;
            }
        }
    }
    public OffsetDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(OffsetDateTime createdAt) { this.createdAt = createdAt; }
}

