package cloud.nstart.terravault.ordinis.notifications.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Per-feature config (D9 — никаких magic numbers, всё через
 * {@code @ConfigurationProperties}).
 *
 * <p>Bound из Spring config (application.yml + Helm values), prefix
 * {@code ordinis.notifications}. Module enables через
 * {@link #enabled} = true (см. {@code NotificationsAutoConfiguration}
 * @ConditionalOnProperty).
 */
@ConfigurationProperties(prefix = "ordinis.notifications")
public class NotificationsProperties {

  /** Главный feature flag. False — module disabled, dispatcher не register'ится. */
  private boolean enabled = false;

  private final Dispatcher dispatcher = new Dispatcher();
  private final RateLimit rateLimit = new RateLimit();
  private final Retention retention = new Retention();
  private final PendingSweep pendingSweep = new PendingSweep();
  private final Email email = new Email();
  private final Express express = new Express();
  private final Telegram telegram = new Telegram();

  public boolean isEnabled() { return enabled; }
  public void setEnabled(boolean enabled) { this.enabled = enabled; }
  public Dispatcher getDispatcher() { return dispatcher; }
  public RateLimit getRateLimit() { return rateLimit; }
  public Retention getRetention() { return retention; }
  public PendingSweep getPendingSweep() { return pendingSweep; }
  public Email getEmail() { return email; }
  public Express getExpress() { return express; }
  public Telegram getTelegram() { return telegram; }

  public static class Dispatcher {
    private int concurrency = 2;
    private int executorPoolSize = 20;
    private int maxPollRecords = 20;
    private int maxEventAgeHours = 1;
    private long sendTimeoutMs = 10_000L;

    public int getConcurrency() { return concurrency; }
    public void setConcurrency(int v) { this.concurrency = v; }
    public int getExecutorPoolSize() { return executorPoolSize; }
    public void setExecutorPoolSize(int v) { this.executorPoolSize = v; }
    public int getMaxPollRecords() { return maxPollRecords; }
    public void setMaxPollRecords(int v) { this.maxPollRecords = v; }
    public int getMaxEventAgeHours() { return maxEventAgeHours; }
    public void setMaxEventAgeHours(int v) { this.maxEventAgeHours = v; }
    public long getSendTimeoutMs() { return sendTimeoutMs; }
    public void setSendTimeoutMs(long v) { this.sendTimeoutMs = v; }
  }

  public static class RateLimit {
    private int perHour = 10;

    public int getPerHour() { return perHour; }
    public void setPerHour(int v) { this.perHour = v; }
  }

  public static class Retention {
    private int days = 90;
    private String cron = "0 0 3 * * *";  // 03:00 UTC daily

    public int getDays() { return days; }
    public void setDays(int v) { this.days = v; }
    public String getCron() { return cron; }
    public void setCron(String v) { this.cron = v; }
  }

  public static class PendingSweep {
    private long olderThanMs = 5 * 60 * 1000L;  // 5 min
    private long runEveryMs = 5 * 60 * 1000L;

    public long getOlderThanMs() { return olderThanMs; }
    public void setOlderThanMs(long v) { this.olderThanMs = v; }
    public long getRunEveryMs() { return runEveryMs; }
    public void setRunEveryMs(long v) { this.runEveryMs = v; }
  }

  public static class Email {
    /** Used as From header. SMTP server config — через standard spring.mail.* properties. */
    private String fromAddress;
    /** Base URL для deep links в email body / unsub links. */
    private String baseUrl;

    public String getFromAddress() { return fromAddress; }
    public void setFromAddress(String v) { this.fromAddress = v; }
    public String getBaseUrl() { return baseUrl; }
    public void setBaseUrl(String v) { this.baseUrl = v; }
  }

  public static class Express {
    /** Corporate Transport Server URL — per-corp on-prem. */
    private String cts;
    /** BotX bot UUID — Vault. */
    private String botId;
    /** BotX bot HMAC base — Vault. */
    private String botSecret;
    /** Group chat UUID per env — Vault или Helm. */
    private String chatId;

    public String getCts() { return cts; }
    public void setCts(String v) { this.cts = v; }
    public String getBotId() { return botId; }
    public void setBotId(String v) { this.botId = v; }
    public String getBotSecret() { return botSecret; }
    public void setBotSecret(String v) { this.botSecret = v; }
    public String getChatId() { return chatId; }
    public void setChatId(String v) { this.chatId = v; }
  }

  public static class Telegram {
    /** Bot token — Vault. */
    private String botToken;
    private String chatId;

    public String getBotToken() { return botToken; }
    public void setBotToken(String v) { this.botToken = v; }
    public String getChatId() { return chatId; }
    public void setChatId(String v) { this.chatId = v; }
  }
}
