export const emptySettings = {
  competitorGroups: [""],
  ourGroup: "",
  minIntervalMinutes: 5,
  maxIntervalMinutes: 10,
  dailyLimit: 100,
  historyLimit: 200,
  accountMaxGroups: 10,
  accountDailyLimit: 50,
  floodCooldownMinutes: 1440,
  queueTtlHours: 24
};

export const emptyTaskForm = {
  id: null,
  name: "",
  ourGroup: "",
  minIntervalMinutes: 1,
  maxIntervalMinutes: 3,
  dailyLimit: 15,
  historyLimit: 35,
  maxInvitesPerCycle: 1,
  maxCompetitorBots: 1,
  maxOurBots: 1,
  randomAccounts: false,
  multiAccountsPerRun: false,
  retryOnFail: true,
  autoJoinCompetitors: true,
  autoJoinOurGroup: true,
  separateBotRoles: false,
  requireSameBotInBoth: true,
  parseParticipants: false,
  inviteViaAdmins: false,
  inviteAdminMasterId: 0,
  inviteAdminAllowFlood: false,
  inviteAdminAnonymous: true,
  separateConfirmRoles: false,
  maxConfirmBots: 1,
  useWatcherInviteNoUsername: true,
  warmupEnabled: true,
  warmupStartLimit: 3,
  warmupDailyIncrease: 2,
  cycleCompetitors: false,
  competitorCursor: 0,
  inviteLinkOnFail: false,
  rolesMode: "manual",
  stopOnBlocked: true,
  stopBlockedPercent: 25,
  notes: "",
  enabled: true,
  autoAssignAccounts: true,
  allowStartWithoutInviteRights: true
};

export const normalizeIntervals = (form) => {
  const minValue = Number(form.minIntervalMinutes);
  const maxValue = Number(form.maxIntervalMinutes);
  const min = Number.isFinite(minValue) && minValue > 0 ? minValue : 1;
  let max = Number.isFinite(maxValue) && maxValue > 0 ? maxValue : 1;
  if (max < min) max = min;
  return { ...form, minIntervalMinutes: min, maxIntervalMinutes: max };
};

export const sanitizeTaskForm = (form) => {
  let normalized = { ...form };
  normalized = normalizeIntervals(normalized);
  if (normalized.requireSameBotInBoth) {
    normalized.separateBotRoles = false;
    normalized.maxOurBots = normalized.maxCompetitorBots;
  } else {
    normalized.separateBotRoles = true;
  }
  if (!normalized.separateBotRoles) {
    normalized.separateConfirmRoles = false;
  }
  return normalized;
};

export const normalizeTask = (row) => ({
  id: row.id,
  name: row.name || "",
  ourGroup: row.our_group || "",
  minIntervalMinutes: Number(row.min_interval_minutes || 1),
  maxIntervalMinutes: Number(row.max_interval_minutes || 3),
  dailyLimit: Number(row.daily_limit || 15),
  historyLimit: Number(row.history_limit || 35),
  maxInvitesPerCycle: Number(row.max_invites_per_cycle || 1),
  maxCompetitorBots: Number(row.max_competitor_bots || 1),
  maxOurBots: Number(row.max_our_bots || 1),
  randomAccounts: Boolean(row.random_accounts),
  multiAccountsPerRun: Boolean(row.multi_accounts_per_run),
  retryOnFail: Boolean(row.retry_on_fail),
  autoJoinCompetitors: Boolean(row.auto_join_competitors),
  autoJoinOurGroup: Boolean(row.auto_join_our_group),
  separateBotRoles: Boolean(row.separate_bot_roles),
  requireSameBotInBoth: Boolean(row.require_same_bot_in_both),
  parseParticipants: Boolean(row.parse_participants),
  inviteViaAdmins: Boolean(row.invite_via_admins),
  inviteAdminMasterId: Number(row.invite_admin_master_id || 0),
  inviteAdminAllowFlood: Boolean(row.invite_admin_allow_flood),
  inviteAdminAnonymous: row.invite_admin_anonymous == null ? true : Boolean(row.invite_admin_anonymous),
  separateConfirmRoles: Boolean(row.separate_confirm_roles),
  maxConfirmBots: Number(row.max_confirm_bots || 1),
  useWatcherInviteNoUsername: row.use_watcher_invite_no_username == null ? true : Boolean(row.use_watcher_invite_no_username),
  warmupEnabled: row.warmup_enabled == null ? true : Boolean(row.warmup_enabled),
  warmupStartLimit: Number(row.warmup_start_limit || 3),
  warmupDailyIncrease: Number(row.warmup_daily_increase || 2),
  cycleCompetitors: Boolean(row.cycle_competitors),
  competitorCursor: Number(row.competitor_cursor || 0),
  inviteLinkOnFail: Boolean(row.invite_link_on_fail),
  rolesMode: row.role_mode || "manual",
  stopOnBlocked: Boolean(row.stop_on_blocked),
  stopBlockedPercent: Number(row.stop_blocked_percent || 25),
  notes: row.notes || "",
  enabled: Boolean(row.enabled),
  allowStartWithoutInviteRights: row.allow_start_without_invite_rights == null ? true : Boolean(row.allow_start_without_invite_rights),
  autoAssignAccounts: true
});
