using System.Text.Json;
using Tiku.Domain.Common;

namespace Tiku.Domain.Platform;

public enum PlatformApprovalRequestStatus
{
    Pending,
    Approved,
    Rejected,
    Executing,
    Succeeded,
    Failed,
    Cancelled,
    Expired
}

public sealed class PlatformApprovalPolicy : AuditableEntity
{
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string RequiredPermission { get; set; } = string.Empty;
    public bool Enabled { get; set; } = true;
    public bool AlwaysRequireApproval { get; set; }
    public int? AmountThresholdCents { get; set; }
    public int Version { get; set; } = 1;
    public int ExpiresAfterHours { get; set; } = 24;
    public JsonElement Conditions { get; set; } = JsonDefaults.Object();
}

public sealed class PlatformApprovalRequest : AuditableEntity
{
    public string RequestNo { get; set; } = string.Empty;
    public string PolicyCode { get; set; } = string.Empty;
    public int PolicyVersion { get; set; }
    public PlatformApprovalRequestStatus Status { get; set; } = PlatformApprovalRequestStatus.Pending;
    public Guid RequestedBy { get; set; }
    public Guid? DecidedBy { get; set; }
    public string RequiredPermission { get; set; } = string.Empty;
    public string CommandType { get; set; } = string.Empty;
    public string TargetType { get; set; } = string.Empty;
    public string TargetId { get; set; } = string.Empty;
    public int? AmountCents { get; set; }
    public string IdempotencyKey { get; set; } = string.Empty;
    public string RequestHash { get; set; } = string.Empty;
    public JsonElement RequestSnapshot { get; set; } = JsonDefaults.Object();
    public JsonElement? ResultSnapshot { get; set; }
    public string? RequestReason { get; set; }
    public string? DecisionReason { get; set; }
    public string? Error { get; set; }
    public DateTimeOffset ExpiresAt { get; set; }
    public DateTimeOffset? DecidedAt { get; set; }
    public DateTimeOffset? ExecutedAt { get; set; }
    public Guid ConcurrencyStamp { get; set; } = Guid.NewGuid();
}

public enum PlatformConfigurationValueType
{
    String,
    Number,
    Boolean,
    Json,
    SecretReference
}

public enum PlatformConfigurationVersionStatus
{
    Draft,
    Published,
    Retired
}

public sealed class PlatformConfigurationDefinition : AuditableEntity
{
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Category { get; set; } = string.Empty;
    public PlatformConfigurationValueType ValueType { get; set; }
    public bool AllowRuntimeManagement { get; set; } = true;
    public bool IsSensitive { get; set; }
    public string? Description { get; set; }
    public JsonElement ValidationSchema { get; set; } = JsonDefaults.Object();
}

public sealed class PlatformConfigurationVersion : AuditableEntity
{
    public Guid DefinitionId { get; set; }
    public string Environment { get; set; } = "all";
    public int Version { get; set; }
    public PlatformConfigurationVersionStatus Status { get; set; } = PlatformConfigurationVersionStatus.Draft;
    public JsonElement Value { get; set; } = JsonDefaults.Object();
    public string? SecretRef { get; set; }
    public Guid CreatedBy { get; set; }
    public Guid? PublishedBy { get; set; }
    public Guid? RolledBackFromVersionId { get; set; }
    public string Reason { get; set; } = string.Empty;
    public DateTimeOffset? PublishedAt { get; set; }
}

public enum PlatformNotificationChannel
{
    InApp,
    Sms,
    Email
}

public enum PlatformNotificationDeliveryStatus
{
    Pending,
    Processing,
    Sent,
    Failed,
    Cancelled
}

public sealed class PlatformNotificationTemplate : AuditableEntity
{
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public PlatformNotificationChannel Channel { get; set; } = PlatformNotificationChannel.InApp;
    public string SubjectTemplate { get; set; } = string.Empty;
    public string BodyTemplate { get; set; } = string.Empty;
    public bool Enabled { get; set; } = true;
    public JsonElement Variables { get; set; } = JsonDefaults.Array();
}

public sealed class PlatformNotificationDelivery : AuditableEntity
{
    public Guid TemplateId { get; set; }
    public Guid RecipientUserId { get; set; }
    public string RecipientRoleCode { get; set; } = string.Empty;
    public PlatformNotificationChannel Channel { get; set; }
    public PlatformNotificationDeliveryStatus Status { get; set; } = PlatformNotificationDeliveryStatus.Pending;
    public string Subject { get; set; } = string.Empty;
    public string Body { get; set; } = string.Empty;
    public string IdempotencyKey { get; set; } = string.Empty;
    public int Attempts { get; set; }
    public string? LastError { get; set; }
    public DateTimeOffset? SentAt { get; set; }
    public Guid CreatedBy { get; set; }
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}