using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Domain.Entities.Members;
using Domain.Entities.Payments.Danal.ValueObject;

namespace Domain.Entities.Payments.Danal;

/// <summary>
/// 다날 결제 에러 / 실패 / 가상계좌 웹훅 통합 로그
/// </summary>
public class DanalLog
{
    [ForeignKey(nameof(MemberID))]
    public virtual Member? Member { get; private set; }

    [Key]
    public int ID { get; private set; }

    public int MemberID { get; private set; }

    /// <summary>로그 유형 (Failed, Error, Webhook)</summary>
    public DanalLogType LogType { get; private set; }

    /// <summary>결과/에러 코드</summary>
    public string Code { get; private set; } = default!;

    /// <summary>결과/에러 메시지</summary>
    public string Message { get; private set; } = default!;

    /// <summary>다날 거래번호 (있는 경우)</summary>
    public string? TransactionID { get; private set; }

    /// <summary>가맹점 주문번호 (있는 경우)</summary>
    public string? OrderID { get; private set; }

    /// <summary>추가 데이터 (JSON 등)</summary>
    public string? ExtraData { get; private set; }

    public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;

    private DanalLog() { }

    public static DanalLog Create(
        int memberID,
        DanalLogType logType,
        string code,
        string message,
        string? transactionID = null,
        string? orderID = null,
        string? extraData = null
    ) {
        return new DanalLog
        {
            MemberID = memberID,
            LogType = logType,
            Code = code,
            Message = message,
            TransactionID = transactionID,
            OrderID = orderID,
            ExtraData = extraData
        };
    }
}